poj3090--欧拉函数

#include<iostream>
using namespace std;
//欧拉函数
int eular(int n){
    int res=1,i;
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            n/=i;res*=i-1;
            while(n%i==0){
                n/=i;res*=i;
            }
        }
    }
    if(n>1)
    res*=n-1;
    return res;
}
int main(){
    int n,m,sum;
    cin>>n;
    for(int i=1;i<=n;i++){
        sum=3;
        cin>>m;
        for(int j=2;j<=m;j++)
        sum+=eular(j)*2;//除(1,1)外,其余的点都有对称点
        cout<<i<<" "<<m<<" "<<sum<<endl;
    }
    return 0;
}

此题与Poj2407相似,不过有对称点需考虑。

我们仔细观察题目会发现符合条件的点的坐标x,y都是互质的,1与1互质这是个特例,因为它是唯一一个符合没有对称点的点,需特殊考虑,此外还有(0,1),(1,0)

两点,故sum起始为3,其余加上的是欧拉函数x2

时间: 2024-08-01 22:34:41

poj3090--欧拉函数的相关文章

poj3090欧拉函数求和

E - (例题)欧拉函数求和 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0

算法复习——欧拉函数(poj3090)

题目: Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For exa

POJ3090(SummerTrainingDay04-M 欧拉函数)

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7450   Accepted: 4536 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr

poj3090 Visible Lattice Points [欧拉函数]

Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For example

欧拉函数总结【数论】【欧拉函数】

欧拉函数的定义:euler(k)=([1,n-1]中与n互质的整数个数). eg:euler(8)=4.由于1,3,5,7均和8互质. 能够推出下面公式: euler(k)=(p1-1)(p2-1)--(pi-1)*(p1^(a1-1))(p2^(a2-1))--(pi^(ai-1)) =k*(p1-1)(p2-1)--(pi-1)/(p1*p2*--pi); =k*(1-1/p1)*(1-1/p2)....(1-1/pk)  故euler函数表达通式:euler(x)=x(1-1/p1)(1-

欧拉函数

void Euler_Sieve_Method(int * euler, int n) { euler[1] = 1; for (int i = 2; i < n; i++) { euler[i] = i; } for (int i = 2; i < n; i++) { if (euler[i] == i) { for (int j = i; j < n; j += i) { euler[j] = euler[j] / i * (i - 1); } } } } void Euler_Si

hdu1695(莫比乌斯)或欧拉函数+容斥

题意:求1-b和1-d之内各选一个数组成数对,问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个可以简化成1-b/k 和1-d/k 的互质有序数对的个数.假设b=b/k,d=d/k,b<=d.欧拉函数可以算出1-b与1-b之内的互质对数,然后在b+1到d的数i,求每个i在1-b之间有多少互质的数.解法是容斥,getans函数参数的意义:1-tool中含有rem位置之后的i的质因子的数的个数. 在 for(int j=rem;j<=factor[i

欧拉函数常用性质

欧拉函数定义:设n 为正整数,则1,2......,n中与n互质的整数个数记作f(n). 1.1 若n为素数,f(n)=n-1; 1.2 整数n=p*q,p,q为不同素数,则f(n)=f(p)*f(q)=(p-1)*(q-1) 1.3 n=p^a*q^b,f(n)=f(p^a)*f(q^b)=n*(1-1/p)*(1-1/q) 1.4 分解质因子相乘,f(n)=n*(1-1/p1)*(1-1/p2)*.......*(1-1/pk). f(100)=f(2^2*5^2)=100*1/2*4/5=

POJ2478(SummerTrainingDay04-E 欧拉函数)

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16927   Accepted: 6764 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b)

POJ 2478 欧拉函数(欧拉筛法) HDU 1576 逆元求法

相关逆元求法,我之前有写过,还有欧拉函数的求法,欧拉函数与逆元的关系  点击 POJ 2478 又是一个打表的题目,一眼看出结果就是前n个欧拉函数值的和. 这里直接计算欧拉函数值求和会超时,看见多组数据. 然后就是计算欧拉函数,打表就好了. #include <stdio.h> #include <string.h> #include <iostream> using namespace std; typedef long long LL; const int N =