Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## RE?为什么？

Posted by 2009550934 at 2011-08-11 10:34:23 on Problem 2154
```#include <iostream>
#include<cstdio>
#include<cstring>
#include<fstream>
using namespace std;
typedef __int64 II;
II prime[36000],flag[36000];

void prim()                    //筛法求素数
{
II i,j,k;
for(i =0 ;i < 3600;i++)
flag[i] =1;
for(i = 2,k = 0 ;i < 3600;i++)
{
if(flag[i])
{
prime[k++] = i;
for(j = i + i;j < 3600;j += i)
flag[j] = 0;
}
}
}

II gcd(II a,II b)
{
if(b== 0)return a;
return gcd(b,a % b);
}

II pow(II a, II b , II c)
{
if(b ==1 )
return a % c;
if(b ==0)
return 1;
if(b % 2)
{
return (pow(a,b-1,c)*a)%c;
}
else
{
int temp = pow(a,b/2,c);
return (temp * temp)%c;
}
}
II eular(II n)
{
II i = 0,ans =1;
for(i =0 ;prime[i]*prime[i]<= n;i++)
{
if(n%prime[i]!= 0)continue;
ans *= prime[i]-1;
n/= prime[i];
while(n%prime[i]==0)
{
ans *= prime[i];
n/= prime[i];
}
}
if(n>1)
ans*= n-1;
return ans;
}

int main()
{
freopen("a.txt","r",stdin);
freopen("b.txt","w",stdout);
int t;
prim();
II n , mo;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d",&n,&mo);
II ans = 0,mod;
mod = mo;
for(II i =1;i*i <= n;i++)
{
if(i*i == n)
ans = (ans+(pow(n,i-1,mod)*(eular(i)%mod))%mod)%mod;
else if(n%i==0)
ans = (ans +(pow(n,i-1,mod)*(eular(n/i)%mod))%mod+(pow(n,n/i-1,mod)*(eular(i)%mod))%mod)%mod;
}
//for(__int64 i= 0;i < n;i++ )
//ans = (ans + pow(n,gcd(n,i),mod))%mod;
printf("%I64d\n",ans%mo);
}
return 0;
}
```

Followed by: