Title: http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2165&cid=1431
Fast power.
#include <iostream>#include<string.h>#include<stdlib.h>#include<stdio.h>#include<queue>#include<vector>#include<algorithm>#include<queue>#defineINF 0x3f3f3f3ftypedefLong Longll;using namespacestd;inttt,n,cn[ the];intqq[100010],we;structnode{intW,id;} q[ the];#defineMoD 997intErintLfintRfintkey) { intL=Lf,mid; intR=RF; while(l<=r) {Mid= L + (r-l)/2; if(q[mid].w==key)returnmid; Else if(q[mid].w>key) {R=mid-1; } Else{L=mid+1; } } return-1;}intcmpConst void*a,Const void*b) { structNode *aa= (structNode *) A; structNode *bb= (structNode *) b; returnAa->w-bb->W;}intMult_mod (intAintb//calculates (a*b)%c.{a%=mod;//reduce the time of multiplication by using two-point ideab%=MoD; LL ret=0; while(b) {if(b&1) {ret+=A; RET%=MoD; } A<<=1; if(A>=mod) a%=MoD; b>>=1; } returnret;}intPow_mod (intXintN//x^n%n{ if(n==1)returnX%MoD; X%=MoD; inttmp=x; intret=1; while(n) {if(n&1) ret=Mult_mod (ret,tmp); TMP=Mult_mod (tmp,tmp); N>>=1; } returnret;}voidinit () {memset (CN,0,sizeof(CN)); TT=0; for(intI= -; i<= $; i++) {q[tt].id=i; Q[tt++].w=Pow_mod (i,n); } for(intI= $; i<= -; i++) {q[tt].id=i; Q[tt++].w=Pow_mod (i,n); } for(intI= the; i<=122; i++) {q[tt].id=i; Q[tt++].w=Pow_mod (i,n); } qsort (Q,tt,sizeof(q[0]), CMP); cn[0]=1; for(intI=1; i<tt;i++) { if(q[i].w==q[i-1].w) {Cn[i]=2; Cn[i-1]=2; } Elsecn[i]=1; }}Chars[1000010];intMain () {intT,da; BOOLFF; scanf ("%d",&T); for(intz=1; z<=t;z++) {FF=true; scanf ("%d",&N); Init (); We=0; scanf ("%s", s); intL=strlen (s); for(intI=0; i<l;i=i+3) {da=s[i]-'0'; for(intj=i+1; j<=i+2; j + +) {da=da*Ten+ (s[j]-'0'); } intT=er (0, tt-1, DA); if(t==-1|| Cn[t]>1) {printf ("No solution\n"); FF=false; Break; } Else{qq[we++]=q[t].id; } } if(FF) { for(intI=0; i<we;i++) printf ("%c", Qq[i]); printf ("\ n"); } //printf ("\ n"); } return 0;}
Sdut2165:crack mathmen (fast power)