题目:http://poj.org/problem?id=2154
今天学了个高端的东西,Polya定理...
此题就是模板,然而还是写了好久好久...
具体看这个博客吧:https://blog.csdn.net/wsniyufang/article/details/6671122
代码如下:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int const maxn=1e6+5; int X,n,p,pri[maxn],cnt,ans; bool vis[maxn]; int phi(int n) {int ret=n;for(int i=2;i*i<=n;i++){if(n%i)continue; // (ret-=ret/i)%=p;// 有除法,不可随时取模 ret-=ret/i;while(n%i==0)n/=i;} // if(n!=1)(ret-=ret/n)%=p;// 有除法,不可随时取模 if(n!=1)ret-=ret/n;return ret%p; } int pw(int a,int b) {int ret=1; a%=p;//!!!for(;b;b>>=1,(a*=a)%=p)if(b&1)(ret*=a)%=p;return ret; } int main() {scanf("%d",&X);while(X--){scanf("%d%d",&n,&p);ans=0;for(int i=1;i*i<=n;i++){if(n%i)continue;(ans+=phi(n/i)*pw(n,i-1))%=p;if(i*i!=n)(ans+=phi(i)*pw(n,n/i-1))%=p;}printf("%d\n",ans);} }