标签:
题目链接:
Time Limit: 3000/1500 MS (Java/Others)
Memory Limit: 262144/262144 K (Java/Others)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar()); for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + ‘0‘); putchar(‘\n‘); } const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=1e9; const int N=1e7+10; const int maxn=500+10; const double eps=1e-8; int phi[N],vis[N],prime[N],cnt; LL sum[N],a[100]; inline void Init() { cnt=0; sum[1]=1; phi[1]=1; For(i,2,N-1) { if(!vis[i]) { for(int j=2*i;j<N;j+=i) { if(!vis[j])phi[j]=j; vis[j]=1; phi[j]=phi[j]/i*(i-1); } phi[i]=i-1; prime[++cnt]=i; } sum[i]=(sum[i-1]+phi[i])%mod; } } LL pow_mod(LL x,LL y,LL mo) { LL s=1,base=x; while(y) { if(y&1)s=s*base%mo; base=base*base%mo; y>>=1; } return s; } LL work(LL a,LL b) { if(b==1)return 0; LL sum=work(a,phi[b]); sum=sum+phi[b]; LL ans=pow_mod(a,sum,b); return ans; } LL dfs(int pos,LL n,LL m) { if(n==1)return sum[m]; if(m==0)return 0; return ((a[pos]-1)*dfs(pos-1,n/a[pos],m)%mod+dfs(pos,n,m/a[pos]))%mod; } inline LL solve(LL n,LL m) { int num=0; LL temp=n; if(!vis[n])a[++num]=n; else { for(int i=1;i<=cnt;i++) { if(n<prime[i])break; if(n%prime[i]==0) { a[++num]=prime[i]; n/=prime[i]; } } } return dfs(num,temp,m); } int main() { Init(); LL n,m,p; while(cin>>n>>m>>p) { LL k=solve(n,m); LL ans=work(k,p); print(ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5702940.html