You casually write out, found Polya principle of a lot of GCD is the same, gcd (n,i) =k can be changed to write gcd (n/k,i/k) = 1, that is, the index of the number of K items is Phi (n/k), it is good to beg, the last except the N directly put on the index can, There is no need to use the inverse element.
Import Java.util.*;import java.io.*;p ublic class Main {public static int phi (int n) {int ans=n;for (int i=2;i*i<=n;++i) { if (n%i==0) {ans=ans/i* (i-1); while (n%i==0) {n/=i;}}} if (n>1) {ans=ans/n* (n-1);} return ans;} public static int Quick_pow (int x,int p,int MOD) {if (p==0) {return 1;} int Ans=quick_pow (X,P>>1,MOD); ans= (Ans*ans)%mod;if ((p&1) ==1) {ans= (X%mod*ans)%mod;} return ans;} public static void Main (string[] argc) {int t,n,p; Scanner sc = new Scanner (new Bufferedinputstream (system.in)); T=sc.nextint (); for (int zu=1;zu<=t;++zu) {int ans=0;n=sc.nextint (); P=sc.nextint (); for (int i=1;i*i<=n;++i) {if (n%i==0) {ans= (ans+ ((Phi (n/i)%P) *quick_pow (n,i-1,p))%P)%p;// System.out.printf ("test:%d\n", ans), if (i*i!=n) {ans= (ans+ ((Phi (i)%P) *quick_pow (n,n/i-1,p))%P)%p;// System.out.printf ("test:%d\n", ans);}} System.out.println (ans);} Sc.close (); }}
"Polya theorem" "Enumeration approximate" "Euler function" "Java" poj2154 Color