求a^b的約數對mod取模
阿新 • • 發佈:2018-12-08
const int maxn=30000+5; int prime[maxn]; void marktable(int n){ memset(prime,0,sizeof(prime)); for(int i=2;i<=n;i++){ if(!prime[i]) prime[++prime[0]]=i; for(int j=1;j<=prime[0]&&prime[j]<=n/i;j++){ prime[prime[j]*i]=1; if(i%prime[j]==0) break; } } }long long factor[100][2]; int fatCnt; int getFactors(long long x){ fatCnt=0; long long tmp=x; for(int i=1;prime[i]<=tmp/prime[i];i++){ factor[fatCnt][1]=0; if(tmp%prime[i]==0){ factor[fatCnt][0]=prime[i]; while(tmp%prime[i]==0){ factor[fatCnt][1]++; tmp/=prime[i]; } fatCnt++; } } if(tmp!=1) { factor[fatCnt][0]=tmp; factor[fatCnt++][1]=1; } return fatCnt; } int mod,A,B; template<class T,class T1> T fast_mod(T a,T b,T1 Mod){ a%=mod; if(b==0) return 1; T ans=1,base=a; while(b!=0){ if(b&1)ans=(ans*base)%Mod; base=(base*base)%Mod; b>>=1; } return ans; } long long sum(long long p,long long n){ if(p==0) return 0; if(n==0) return 1; if(n&1) return ((1+fast_mod(p,n/2+1,mod))%mod*sum(p,n/2)%mod)%mod; else return ((1+fast_mod(p,n/2+1,mod))%mod*sum(p,n/2-1)+fast_mod(p,n/2,mod)%mod)%mod; } long long solve(long long A,long long B){ getFactors(A); long long ans=1; for(int i=0;i<fatCnt;i++){ ans*=sum(factor[i][0],B*factor[i][1])%mod; ans%=mod; } return ans; }