poj2154(Polya+欧拉函数优化模版)
作者:互联网
#include <cstdio>
#include <cstring>
#include<iostream>
using namespace std;
const int maxx=45000;
int p;
int vis[maxx];
bool primes[maxx];
int k=0;
void prime(){//素数筛
int i,j;
memset(primes,true,sizeof(primes));
for(i=2;i<maxx;i++){
if(primes[i]){
vis[k++]=i;
for(j=2*i;j<maxx;j+=i){
primes[j]=false;
}
}
}
}
int Euler_1(int n){//欧拉函数
int res=n;
for(int i=0;vis[i]*vis[i]<=n;i++){
if(n%vis[i]==0){
res=res/vis[i]*(vis[i]-1);
while(n%vis[i]==0) n/=vis[i];
}
}
if(n>1)
res=res/n*(n-1);
return res%p;
}
int pows(int a,int b){//快速幂
int ans=1;
a%=p;
while(b){
if(b&1){
ans=(ans*a)%p;
}
b>>=1;
a=(a*a)%p;
}
return ans;
}
int main(){
int i,t,n,tot;
prime();
cin>>t;
while(t--){
tot=0;
cin>>n>>p;
for(int i=1;i*i<=n;i++){
if(i*i==n){
tot=tot+pows(n,i-1)*Euler_1(i)%p;
}else if(n%i==0){
tot=(tot+pows(n,i-1)*Euler_1(n/i)+pows(n,n/i-1)*Euler_1(i))%p;
}
}
cout<<tot%p<<endl;
}
return 0;
}
标签:vis,poj2154,模版,ans,tot,int,res,Polya,primes 来源: https://blog.csdn.net/Keep_Trying_Go/article/details/113842479