HDUOJ 5728 PowMod

题目链接:http://acm.hdu.edu.cn/status.php?user=PerfectPan16

题意:略。

思路:欧拉函数的性质的充分利用。。。具体看这里

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=10000010;
const int INF=0x3f3f3f3f;
const int MOD=1000000007;
const double PI=acos(-1.0);
template<typename T> inline T gcd(T&a,T&b){return b==0?a:gcd(b,a%b);}
template<typename T> inline T lcm(T&a,T&b){return a/gcd(a,b)*b;}
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
int n,m,p;
bool vis[maxn];
int prime[maxn],phi[maxn],sum[maxn];
void init(){
    phi[0]=phi[1]=1;
    for (int i=2;i<maxn;i++){
        if (!vis[i]){
            prime[++prime[0]]=i;
            phi[i]=i-1;
        }
        for (int j=1;j<=prime[0] && 1LL*i*prime[j]<maxn;j++){
            if (i%prime[j]==0){
                phi[i*prime[j]]=phi[i]*prime[j];
                vis[i*prime[j]]=true;
                break;
            }
            phi[i*prime[j]]=phi[i]*(prime[j]-1);
            vis[i*prime[j]]=true;
        }
    }
    sum[0]=0;
    for (int i=1;i<maxn;i++){
        sum[i]=1LL*(sum[i-1]+phi[i])%MOD;
    }
}
int s(int n,int m){
    if (n==1) return sum[m];
    if (m==0) return 0;
    for (int i=1;i<=prime[0] && prime[i]<=n;i++){
        if (n%prime[i]==0){
            int p=prime[i];
            return (1LL*(p-1)*s(n/p,m)%MOD+s(n,m/p)%MOD)%MOD;
        }
    }
}
ll powMod(ll a,int k,int m){
    ll res=1;
    while (k){
        if (k&1LL) res=res*a%m;
        k>>=1;
        a=a*a%m;
    }
    return res;
}
int A(int k,int p){
    if (p==1) return 0;
    if (k==1) return 1;
    return powMod(1LL*k%p,phi[p]+A(k,phi[p]),p);
}
int main(){
    init();
    while (~scanf("%d%d%d",&n,&m,&p)){
        int k=s(n,m);
        printf("%d\n",A(k,p));
    }
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注