閱讀461 返回首頁    go 阿裏雲 go 技術社區[雲棲]


POJ 3090 歐拉函數遞推

題意:為多少個點與原點連線不經過其他點。

首先ans[1]=3,不經過其他點也就是(x,y)點的gcd(x,y)=1,也就是x,y,互素。所以就是每一行上的個數為phi(n)*2因為行和列都算上就是兩倍關係。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 1000008
long long phi[maxn];
void getphi()
{
    for(int i=1; i<maxn; i++) phi[i]=i;
    for(int i=2; i<maxn; i+=2) phi[i]>>=1;
    for(int i=3; i<maxn; i+=2)
        if(phi[i]==i)
        {
            for(int j=i; j<maxn; j+=i)
                phi[j]=phi[j]/i*(i-1);
        }
    phi[1]=3;
    for(int i=2; i<maxn; i++)
        phi[i]=phi[i-1]+2*phi[i];
}
int main()
{
    getphi();
    int n,ca=0,t;
    scanf("%d",&t);
    while(t--)
        scanf("%d",&n),
        printf("%d %d %lld\n",++ca,n,phi[n]);
    return 0;
}


最後更新:2017-04-03 16:48:45

  上一篇:go 此文對你人生會有莫大好處的,建議永久保存
  下一篇:go 360輸在“斯大林格勒”