Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  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:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator