题目:http://poj.org/problem?id=1845
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 16348 | Accepted: 4075 |
Description
Input
Output
Sample Input
2 3
Sample Output
15
Hint
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int mod=9901; //mod是一个质数--》乘法逆元 typedef long long LL; LL factor[7500][2], sum; //factor的一维长度不要太大,否则会MLE void resolve(LL x){ sum=0; memset(factor,0,sizeof(factor)); for(int i=2;i*i<=x;i++){ if(x%i==0){ factor[sum][0]=i; while(x%i==0){ factor[sum][1]++; x/=i; } sum++; } } if(x>1){ factor[sum][0]=x; factor[sum++][1]=1; } } LL quick_mod(LL a,LL b){ LL ans=1,temp=a%mod; while(b){ if(b&1) ans=ans*temp%mod; temp=temp*temp%mod; b>>=1; } return ans; } LL Extend_Eulid(LL b,LL a) //逆元 d mod { LL x1,x2,x3,y1,y2,y3 ; x1=1,x2=0,x3=a,y1=0,y2=1,y3=b ; while(y3 && y3!=1) { LL q=x3/y3 ; LL t1,t2,t3 ; t1=x1-q*y1,t2=x2-q*y2,t3=x3-q*y3 ; x1=y1,x2=y2,x3=y3 ; y1=t1,y2=t2,y3=t3 ; } if(!y3)return -1 ; while(y2<0) y2+=mod; // 保证是非负数 return y2 ; } int main() { //freopen("cin.txt","r",stdin); LL a,b; while(cin>>a>>b){ if(b==0){ puts("1"); continue; } if(a==0){ puts("0"); continue; } resolve(a); LL ans=1; for(LL i=0;i<sum;i++){ factor[i][1]*=b; factor[i][1]++; LL pow=factor[i][1]; //%(mod-1); LL m=(quick_mod(factor[i][0],pow)-1+mod)%mod; LL ni=Extend_Eulid(factor[i][0]-1,mod); ans=(ans*m%mod)*ni%mod; } printf("%lld\n",ans); } return 0; }
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int mod=9901; typedef long long LL; LL factor[7500][2], sum; void resolve(LL x){ sum=0; memset(factor,0,sizeof(factor)); for(int i=2;i*i<=x;i++){ if(x%i==0){ factor[sum][0]=i; while(x%i==0){ factor[sum][1]++; x/=i; } sum++; } } if(x>1){ factor[sum][0]=x; factor[sum++][1]=1; } } LL power(LL a,LL b){ LL ans=1,temp=a%mod; while(b){ if(b&1) ans=ans*temp%mod; temp=temp*temp%mod; b>>=1; } return ans; } LL cal(int p,int n){ if(n==0) return 1; if(n&1){//(1+p+p^2+....+p^(n/2))*(1+p^(n/2+1)); return (1+power(p,n/2+1))*cal(p,n/2)%mod; } else { //(1+p+p^2+....+p^(n/2-1))*(1+p^(n/2+1))+p^(n/2); return (power(p,n/2)+(1+power(p,n/2+1))*cal(p,n/2-1))%mod; } } int main() { //freopen("cin.txt","r",stdin); LL a,b; while(cin>>a>>b){ if(b==0){ puts("1"); continue; } //0^0=1 if(a==0){ puts("0"); continue; } resolve(a); LL ans=1; for(LL i=0;i<sum;i++){ factor[i][1]*=b; ans=ans*cal(factor[i][0],factor[i][1])%mod; } printf("%lld\n",ans); } return 0; }