Poj 2154 color Euler's function-optimized ploya count

Source: Internet
Author: User

Enumeration displacement definitely times out. What we need for a displacement I is the number of cycles, that is, the number of gcd (I, n) and gcd (I, n, because it is equivalent to the number of N approx.

Therefore, we enumerate n's approx. For An approx K, that is, the number of loops such as N/K, there are Phi [k] types, proving that there are many online. So the answer is Phi [k] * (POW (N, N/K) (k is all the approx. N)

Since the number of arguments is large, you cannot create a table, but you can only calculate it for one.

Since we divide the last time by N, we cannot directly take the modulo if we do division. Therefore, when we calculate every POW (N, N/K), we will not multiply by one N, this is equivalent to Division.


#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>using namespace std;const int N=1000000;int quickpow(int m,int n,int k){    int ans=1;    while(n)    {        if(n&1) ans=(ans*m)%k;        n=(n>>1);        m=(m*m)%k;    }    return ans;}bool a[N];int prim[N];int pp[N];void Prime(){    memset(a, 0, sizeof(a));    int num = 0, i, j;    pp[1]=1;    for(i = 2; i < N; ++i)    {        if(!(a[i])) prim[num++]=pp[i]=i;        for(j = 0; (j<num && i*prim[j]<N); ++j)        {            pp[i*prim[j]]=prim[j];            a[i*prim[j]] = 1;            if(!(i%prim[j])) break;        }    }}int phi(int x){    int i,j;    int num = x;    for(i = 0; prim[i]*prim[i] <= x; i++)    {        if(x % prim[i] == 0)        {            num = (num/prim[i])*(prim[i]-1);            while(x % prim[i] == 0)            {                x = x / prim[i];            }        }    }    if(x != 1) num = (num/x)*(x-1);    return num;}int main(){    Prime();    int cas,n,p;    scanf("%d",&cas);    while(cas--)    {        int ans=0;        scanf("%d%d",&n,&p);        for(int l=1;l*l<=n;l++)        {            if(n%l==0)            {                if(l*l==n)                {                    ans+=phi(l)%p*quickpow(n%p,l-1,p);                    ans%=p;                    break;                }                ans+=phi(l)%p*quickpow(n%p,n/l-1,p);                ans+=phi(n/l)%p*quickpow(n%p,l-1,p);                ans%=p;            }        }        printf("%d\n",ans);    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.