欧拉函数

来源:互联网 发布:nfc世界网络银行商城 编辑:程序博客网 时间:2024/05/10 00:20

Counting fractions

TimeLimit: 2000MS  MemoryLimit: 65536 Kb

Totalsubmit: 26   Accepted: 4  

Description

Consider the fraction, n/d, where n and d are positive integers. If <= d and HCF(n,d)=1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for d <= 8 in ascending order of size, we get:

1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 7/8

It can be seen that there are 21 elements in this set.

How many elements would be contained in the set of reduced proper fractions for d <= x?

Input

The input will consist of a series of x, 1 < x < 1000001.

Output

For each input integer x, you should output the number of elements contained in the set of reduced proper fractions for d <= x in one line.

Sample Input

3

8

Sample Output

3
21

//题目大意是求前n个数的所有与其的个数,即可以分为求1,2,3。。。到n的与其互质的个数 //如果n为素数的话,那么与n互质的个数为n-1 //如果n不为素数的话,那么n的质因子与n不互质,并且小于n的质因子的倍数也与n不互质,//假设a为n的质因子的话,那么关于a的倍数与n互质的个数为n/a;//所以题意转换为分别求n的质因子,然后用n-n/a #include<iostream> #include<string>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<queue>#include<stack>#include<vector>#include<climits>using namespace std; #define rep(i,n) for(i=0; i<(n); i++)#define repf(i,n,m) for(i=(n); i<=(m); i++)//正循环的#define repd(i,n,m) for(i=(n); i>=(m); i--) //负循环的 #define fab(a) (a)>0?(a):0-(a)#define max(a,b) (a)>(b)?(a):(b)#define min(a,b) (a)<(b)?(a):(b)#define ll __int64#define arc(a) (a)*(a)#define inf 1000000   //最大值的#define exp 0.0000001     //浮点型的#define N 1000005    //记录开的数组bool vis[N];int prime[N]; ll num[N];int main(){    int i,j;    memset(vis,true,sizeof(vis));    repf(i,1,inf)     num[i]=i;    repf(i,2,inf)     {        if(vis[i]==true)//为质因子,找到质因子什么东西的         {          num[i]--;        for(j=i+i; j<=inf; j+=i)        {         vis[j]=false;           num[j]-=num[j]/i;        }    }     }      printf("%d\n",num[8]);    repf(i,3,inf)    {      num[i]+=num[i-1];    }    int n;    while(~scanf("%d",&n))     cout<<num[n]<<endl;    return 0;}