【欧拉函数表】POJ2478-Farey Sequence

【题目大意】

求∑φ(i)(1<=i<=N)。

【思路】

欧拉函数具有如下的重要推论:

当b是素数时

性质①若b|a,有φ(ab)=φ(a)*b;

性质②若b不|a,有φ(ab)=φ(a)*(b-1)。

由此可以得出递推求欧拉函数表的方法:

对于当前φ(i),若未被修改过,这说明它是素数,加入素数表。

对于每个i,枚举小于它的所有素数j。利用性质1和性质2求出φ(ij)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 const int MAXN=1000000+50;
 8 typedef long long ll;
 9 int n,maxn;
10 int phi[MAXN],p[MAXN],input[MAXN];
11 ll s[MAXN];
12 int t=0;
13
14 void eular_table()
15 {
16     memset(phi,0,sizeof(phi));
17     memset(p,0,sizeof(p));
18     p[0]=1;p[1]=2;phi[2]=1;
19     for (int i=2;i<=maxn;i++)
20     {
21         if (phi[i]==0)
22         {
23             p[++p[0]]=i;
24             phi[i]=i-1;
25         }
26         for (int j=1;j<=p[0];j++)
27         {
28             if (i*p[j]<=maxn) phi[i*p[j]]=(i%p[j]==0)? phi[i]*p[j] : phi[i]*(p[j]-1);
29             //注意一定要保证i*p[j]没有超出数组上界,否则RE
30                 else break;
31         }
32     }
33 }
34
35 void printans()
36 {
37     s[0]=0;
38     for (int i=1;i<=maxn;i++)
39         s[i]=s[i-1]+phi[i];
40     for (int i=0;i<t;i++)
41         printf("%lld\n",s[input[i]]);
42 }
43
44 int main()
45 {
46     while (~scanf("%d",&n) && n!=0)
47     {
48         input[t++]=n;
49         maxn=max(maxn,n);
50     }
51     eular_table();
52     printans();
53     return 0;
54 }
时间: 2024-08-27 01:35:59

【欧拉函数表】POJ2478-Farey Sequence的相关文章

欧拉函数知识点总结及代码模板及欧拉函数表

概念梳理: 欧拉函数是少于或等于n的数中与n互质的数的数目. 欧拉函数的性质:它在整数n上的值等于对n进行素因子分解后,所有的素数幂上的欧拉函数之积. 欧拉函数的值 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数.φ(1)=1(唯一和1互质的数(小于等     于1)就是1本身). (注意:每种质因数只一个.比如12=2*2*3那么φ(12)=12*(1-1/2)*(1-1/3)

POJ2478 Farey Sequence

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K       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) = 1 arranged in increasing order. Th

poj-2478 Farey Sequence(dp,欧拉函数)

题目链接: Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14230   Accepted: 5624 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

筛法求欧拉函数(poj2478

求1-n的欧拉函数的值 #include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long lo

poj 2478 Farey Sequence(欧拉函数)

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13204   Accepted: 5181 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 3090 Visible Lattice Points ( 法雷数列 + 欧拉函数 )

#include <cstdio> #include <cstring> using namespace std; #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MAXN 1010 int phi[ MAXN ], farey[ MAXN ], n, t; void get_euler() //打欧拉函数表 { CLR( phi, 0 ); phi[1] = 1; for( int i = 2; i < MAXN;

欧拉函数模板

//直接求解欧拉函数int euler(int n){ //返回euler(n)      int res=n,a=n;     for(int i=2;i*i<=a;i++){         if(a%i==0){             res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出              while(a%i==0) a/=i;         }     }     if(a>1) res=res/a*(a-1);     return re

O(N)的素数筛选法和欧拉函数

首先,在谈到素数筛选法时,先涉及几个小知识点. 1.一个数是否为质数的判定. 质数,只有1和其本身才是其约数,所以我们判定一个数是否为质数,只需要判定2~(N - 1)中是否存在其约数即可,此种方法的时间复杂度为O(N),随着N的增加,效率依然很慢.这里有个O()的方法:对于一个合数,其必用一个约数(除1外)小于等于其平方根(可用反证法证明),所以我们只需要判断2-之间的数即可. bool is_prime(int num) { const int border = sqrt(num); for

看破欧拉函数的奥秘

注意以下三个特殊性质 编程实现   利用欧拉函数和它本身不同质因数的关系,用筛法计算出某个范围内所有数的欧拉函数值. 1 //直接求解欧拉函数 2 #include<cstdio> 3 int euler(int n){ //返回euler(n) 4 int res=n,a=n; 5 for(int i=2;i*i<=a;i++){//从小到大尝试n的质因数 6 if(a%i==0){//如果i是n的质因数 7 res=res/i*(i-1);//提了一个1/i出来,先进行除法是为了防止