Polya theorem
l=1/| g|* (M^c (p1) +m^c (p2) +...+m^c (PK))
G is the permutation group size
M is the number of colors
C (PI) indicates the number of cyclic sections of the first permutation
such as replacement (123) (45) (6) The number of cycles is 3
-------------------------------------------------------------------------------------------
poj1286&poj2409
is simply a matter of handling beads.
There are 3 different types of permutations hidden in the title.
1. Rotate
Note that it is not necessary to separate clockwise and counterclockwise because the clockwise rotation K-bit is equivalent to the counterclockwise rotation (n-k) bit.
In addition, when the K-bit rotation of the number of cycles is gcd (k,n) This slightly brain repair can also be derived.
2. Flip (symmetrical)
You need to divide the parity of N to discuss
When n is an odd number
There is only one symmetry, that is, the axis of the line where each bead is connected to the center. This time the number of cycles is (n+1)/2.
When n is an even number
There are two kinds of symmetry, one in the same odd situation, but at this point the opposite of the two beads is the same line
So the number of cycles is n/2+1, and the amount of the situation is reduced by half.
The other is the symmetry of the line between the midpoint of the two beads and the center of the wire. The number of cyclic sections in this case is N/2, and the situation is also N/2 species.
The data range of the two problems is very small, so it is possible to deal with this directly.
Program poj1286;varI,n:longint; Tot,sum:int64; w:array[-1.. -]of int64;function gcd (x,y:longint): Longint;beginify=0Then exit (x)Elseexit (gcd (y,x mod y)); End;begin w[0]:=1; fori:=1To at Dow[i]:=w[i-1]*3; READLN (n); whilen<>-1 Dobeginifn=0Then begin Writeln (0); READLN (n); Continue; End Tot:=1; sum:=W[n]; fori:=1to n1 DoBegin Inc (TOT); Inc (SUM,W[GCD (I,n)); End ifodd (N) THEN BEGIN Inc (TOT,N); Inc (Sum,w[(n+1) >>1]*N); EndElseBegin Inc (Tot,n>>1); Inc (Sum,w[n>>1+1]*n >>1); Inc (Tot,n>>1); Inc (Sum,w[n>>1]*n >>1); End Writeln (sum div tot); READLN (n); End;end.
Program poj2409;varM,n,tot,sum:int64; I:longint;function W (x:longint): Int64;vari:longint;begin W:=1; fori:=1to X Dow:=w*m;end;function gcd (x,y:longint): Longint;beginify=0Then exit (x)Elseexit (gcd (y,x mod y)); End;begin readln (M,n); while(m<>0) or (n<>0) DoBegin tot:=0; sum:=0; fori:=1to n DoBegin Inc (TOT); Inc (Sum,w (GCD (i,n)); End ifodd (N) THEN BEGIN Inc (TOT,N); Inc (SUM,W (n+1) >>1)*N); EndElseBegin Inc (Tot,n>>1); Inc (SUM,W (n>>1) * N >>1); Inc (Tot,n>>1); Inc (SUM,W (n>>1+1) * N >>1); End Writeln (sum div tot); READLN (M,n); End;end.
----------------------------------------------------------------------------------
POJ2154
Feel very pit ah ...
First of all think this problem is very good ... Very large data range
Because of the statistics of GCD, I naturally think of Euler's function.
Then set it up.
But why did I do it so long ...
There are a few places to be aware of
1. Finally divided by | G| here is the step of n because of the meaning in the modulus of error, but it is amazing to find that the base of the fast Power is also N (probably this is also the title of the same length and color of the intention of the same), as long as the number of 1 can be.
2. Directly this will be tle, Euler's function of the solution also need to be optimized with the European pull line sieve. That is, using existing quality factors to seek phi. The process is simple enough to mention.
It is worth mentioning that the last time the tle-to-AC leap is simply due to the type of a variable. That is, the ANS variable can be changed to Longint.
Once again, the "space will affect time" mentioned by Mr. Zhang a few years ago.
In addition, in this problem, it happened that the first few days of the Euler function and the European cable sieve are used up, feeling very good.
Program poj2154;ConstMaxn=trunc (sqrt (1000000000));varT,test,n,tt:longint; vis:array[-1.. Maxn]of Boolean; p:array[-1.. Maxn]of longint;procedure build;varI,j:longint;begin Fillchar (Vis,sizeof(VIS),true); p[0]:=0; fori:=2To MAXN DobeginifVis[i] THEN BEGIN Inc. (p[0]);p [p[0]]:=i; End forj:=1To p[0] DobeginifI*P[J]>MAXN Then Break; Vis[i*p[j]]:=false; ifI mod p[j]=0Then Break; End End;end;function Phi (X:longint): Longint;vari,ans,tem:longint;begin ans:=x;tem:=x; fori:=1To p[0] Do iftem>=p[i]*P[i] Then//just beginning to write a tem>=p[i] it loses the meaning of optimization tatbeginifX mod p[i]=0Then Ans:=ans div p[i]* (p[i]-1); //in the case that ANS is longint, it will explode after the first ride. whileX mod p[i]=0 Dox:=x Div p[i]; EndElse Break; ifX<>1Then Ans:=ans div x* (x1); //This also involves the question of the order of operationsexit (ANS mod tt); end;function Mul (a,b:longint): Longint;varans,w:int64;begin ans:=1; w:=a mod TT; whileB<>0 DobeginifB and1=1Then ans:= (ans*W) mod tt; W:= (w*W) mod tt; B:=b >>1; End Exit (ANS); end;function solve:longint;varI:longint; Ans:int64;begin ans:=0; fori:=1To Trunc (sqrt (n)) Do ifN MoD i=0Then begin ans:= (Ans+phi (n div i) *mul (n,i-1) ) mod TT; ifI*i<>n then ans:= (Ans+phi (i) *mul (n,n div i1) ) mod TT; End Exit (ANS); End;begin readln (test); Build fort:=1To test Dobegin READLN (N,TT); Writeln (solve); End;end.
[poj1286&poj2154&poj2409] Polya theorem