poj2407

来源:互联网 发布:淘宝买猛将气输入什么 编辑:程序博客网 时间:2024/06/07 04:13

题目描述:

就是求小于它的互质的个数.但是是广泛定义.1相对于1也算有一个质数.

题解:

重点:

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <ctype.h>#include <limits.h>#include <cstdlib>#include <algorithm>#include <vector>#include <queue>#include <map>#include <stack>#include <set>#include <bitset>#define CLR(a) memset(a, 0, sizeof(a))#define REP(i, a, b) for(int i = a;i < b;i++)#define REP_D(i, a, b) for(int i = a;i <= b;i++)typedef long long ll;using namespace std;const int maxn = 1e5 + 100;int p[maxn], pn;int vis[maxn];void getP(){    int key = 1e5 + 0.1;    CLR(vis);    pn = 0;    for(int i = 2;i <= key;i++)    {        if(vis[i]==0)        {            p[pn++] = i;        }        for(int j = 0;j<pn&&p[j]*i<=key;j++)        {            int tmp = i*p[j];            if(i%p[j]==0)            {                break;            }        }    }}int phi(int x){    int ans = x;    int key = sqrt(x)+1e-10;    for(int i = 0;p[i]<=key;i++)    {        if(x%p[i]==0)        {            ans = ans/p[i]*(p[i]-1);            while(x%p[i]==0)            {                x /= p[i];            }        }    }    if(x!=1)    {        ans = ans/x*(x-1);    }    return ans;}int main(){   // freopen("6Fin.txt", "r", stdin);    //freopen("6Fout.txt", "w", stdout);    int n;    getP();    while(scanf("%d", &n)&&n!=0)    {        if(n != 1)            printf("%d\n", phi(n));        else            printf("1\n");    }    return 0;}
0 0