Live2d Test Env

HDU - 5728:PowMod (欧拉函数&指数循环节)

Declare:
k=∑ i=φ(in) mod 1000000007 k=∑i=1mφ(i∗n) mod 1000000007

n is a square-free number.

φ φ is the Euler's totient function.

find:
ans=..k      mod ans=kkkk...k mod p

There are infinite number of k

InputMultiple test cases(test cases 100 ≤100 ), one line per case.

Each line contains three integers, n,n,m and p .

1n,m,p10 7  1≤n,m,p≤107
OutputFor each case, output a single line with one integer, ans.Sample Input

1 2 6
1 100 9

Sample Output

4
7

题意:k=∑ φ(i∗n)%1000000007;求K^(K^(K^....))%P;

思路:第二部分用欧拉降幂一层一层的降。第一部分可以看这里。

就是左边部分 φ(p)=p-1,但是有的部分由于没有i含有p因数,实际是p而不是p-1,所以要把这部分加进去,所以就有了右边部分。  

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=10000010;
int phi[maxn],p[maxn],vis[maxn],cnt;
vector<int>G[maxn];
void prime()
{
    phi[1]=1; for(int i=2;i<maxn;i++){
        if(!vis[i]) p[++cnt]=i,phi[i]=i-1;
        for(int j=1;j<=cnt&&p[j]*i<maxn;j++){
            vis[p[j]*i]=1; phi[i*p[j]]=phi[i]*phi[p[j]];
            if(i%p[j]==0){ phi[i*p[j]]=phi[i]*p[j]; break;}
        }
    }
}
int qpow(int a,int x,int P){
    int res=1; while(x){
        if(x&1) res=(ll)res*a%P;
        a=(ll)a*a%P; x>>=1;
    } return res;
}
int get(int K,int P)
{
    if(P==1) return 0;
    return qpow(K,get(K,phi[P])+phi[P],P);
}
int main()
{
    prime();
    int P,ans,T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&P);
        ans=get(2,P);
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2018-10-24 16:15  nimphy  阅读(313)  评论(0编辑  收藏  举报