"Polya theorem" "Enumeration approximate" "Euler function" "Java" poj2154 Color

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.