Woo Ah ah ah, choose the wrong problem, the original thought very simple optimization + pruning can be involved in the results of a pile of number theory knowledge. My mistake, put on my code (has been optimized as far as possible)
1 Constmaxn=999911659;2 varN,g,i,j,ans:longint;3 Tem:int64;4 function Pow (g,x:longint): Longint;5 var6 Now,tem,i,t:int64;7 begin8tem:=1; now:=G;9 ifg=1Then Exit (1);Ten whileNow<maxn Do One begin Anow:=now*Now ; -tem:=tem*2; - end; thenow:=Now mod maxn; - if(now=0) and (x Div tem>=1) Then exit (0); - ifX Div tem>=1Then t:=Pow (now,x div tem) - Elset:=1; +now:=g; x:=x-tem*(x div tem); - whileX<>0 Do + begin A if(x MoD2)=1Then begin atT:= (t*Now ) mod maxn; - end; -Now:= (now*Now ) mod maxn; -X:=x Div2; - end; - exit (t mod maxn); in end; - function C (x:longint): Longint; to varI:longint; + Tem:int64; - begin thetem:=G; * fori:=1to X Do $Tem:=pow (tem,n-i+1) mod maxn;Panax Notoginseng fori:=1to X DoTEM:=TRUNC (exp (ln (TEM)/i)); - exit (TEM); the end; + begin A readln (n,g); thetem:=1; + fori:=1To Trunc (sqrt (n)) Do - ifN MoD i=0 Then $ begin $ ifI*i<>n then tem:=tem* ((c (i) mod maxn) *(c (n div i) mod maxn) mod maxn) mod maxn - ElseTem:= (tem*C (i)) mod maxn; - end; the writeln (TEM mod MAXN); -End.
But the right thing is.
#include <cstring>#include<iostream>#include<cmath>#include<algorithm>#include<cstdio>using namespacestd;#defineMAXN 35620typedefLong LongLL;#defineMOD 999911659#defineM 999911658LL w[4]={2,3,4679,35617}; LL a[4]; LL fac[4][MAXN]; ll Pow (ll a,ll b,ll MoD)//Fast Power{LL ans=1; while(b) {if(b&1) ans= (ans*a)%MoD; A= (a*a)%MoD; b>>=1; } returnans;}voidInit ()//preprocessing factorial{ for(intI=0;i<4; i++) {fac[i][0]=1; for(intj=1; j<=w[i];j++) {Fac[i][j]= (fac[i][j-1]*J)%W[i]; } }}/*The Fermat theorem of ******************************** combination number taking modulus *********************************/ll C (ll n,ll m,intX//modulus of combination number{ if(N < m)return 0; return(Fac[x][n] * Pow ((fac[x][n-m]*fac[x][m]), w[x]-2, W[x]))%w[x];}/******************************** Lucas dealing with large combined number taking die ********************************/ll Lucas (ll n,ll m,intX//Lucas theorem{ if(m==0)return 1; return(Lucas (n/w[x],m/w[x],x) *c (n%w[x],m%w[x],x))%w[x];}/***************************** extension Euclid to multiply inverse element *****************************/ll EXGCD (ll A,ll b,ll&x,ll &y)//multiplication Inverse element{ if(!B) {x =1; y =0;returnA;} LL ans= EXGCD (b,a%b,x,y); LL T= X;x = Y;y = t-a/b*y; returnans;}/**************************************************************************************** China remainder theorem: * x = b1% m 1 * x = b2% m2* x = b3% m3*. * GCD (m1,m2,m3,...) = 1;* M = M1 * m2 * m3 * ... * M1 = m2 * m3 * ...., M2 = m1 * m3 * ...., M3 = M1 * m2 * M4 * .........; * M1 * m ( -1) = 1 M, M2 * M2 ( -1) = 1 m;* res = (M1 ( -1) *b1 + M2 ( -1) *b2+ ...) %m;res is the value to be evaluated * NOTE: If the value of the modulo is the same: All is M1 then the value of bn can be added and calculated; * Slightly dick ... ******************************************************************************************/LL CRT ()//calculate the number of combinations and the values after the modulo{LL I,d,x0,y0,ans=0; for(i =0; I <4; i++)//Chinese remainder theorem{d=m/W[i]; EXGCD (D,W[I],X0,Y0); Ans= (Ans+d*x0*a[i])%M; } if(Ans <=0) ans + =M; returnans;}intMain () {init (); LL G,n; while(cin>>n>>g) {memset (A,0,sizeof(a)); for(intI=1; i*i<=n;i++) { if(n%i==0) {LL tmp=n/i; for(intj=0;j<4; j + +) { if(tmp!=i) a[j]= (A[j]+lucas (n,i,j))%W[j]; A[J]= (A[j]+lucas (n,tmp,j))%W[j]; } }} cout<<pow (G%MOD,CRT (), MOD) <<Endl; } return 0;}
I don't want to say more =-=.
P1951: [Sdoi2010] Ancient pig wen