kari492 歐拉函數

時間限制 1000 ms 內存限制 65536 KB

    They give you a lot of pairs of numbers, denoted by K,N. for every input, you should tell them the sum of k^gcd(i,n), for i = 1~n.
    For the answer is so large, you can give them the answer mod 23333.

輸入格式

Given T, the number of cases. Then, for next T lines, there are two numbers K,N.(1 <= k <= 10^4, 1 <= n <= 10^8)

 輸出格式

For every case, print one line, containing the answer mod 23333.

 輸入樣例

3
3 4
2 6
100 50000000

輸出樣例

96
84
3014
分析:10的8次方,直接算肯定超時。我們考慮與n的gcd相同的數的個數,gcd(i,n)=x————gcd(i/x,n/x)=1————即小於等於n/x且與x互質的數的個數,也就是歐拉函數。phi=n(1-1/p1)(1-1/p2)……(1-1/pn).
計算phi[n],直觀感覺打表比每個暴力算要快,但是實際上我們只需要計算n的約數的phi值,個數遠小於n本身(尤其是n很大時)。所以枚舉n的約數,再逐個計算更快。而且打表爲O(nlogn),10的8次方超時。
代碼:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define MOD 23333
using namespace std;
long long  phi[1000001];
long long  power[60];
void pp(long long k)
{
    for(int i=1;i<=32;i++)
    {
        power[i]=k;
        k=k*k%MOD;
    }
}
long long  mod_power(long long k,long long n)
{
    long long res=1;
    int i=1;
    while(n)
    {
        if(n&1)
            res=res*power[i]%MOD;
        n>>=1;
        i++;
    }
    return res;
}
//O(根n)的複雜度
int euler_phi(int n)
{
    int m=(int)sqrt(n+0.5);
    int ans=n;
    for(int i=2;i<=m;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;
}
int main()
{
    long long t,n,i,j,k,sum;
    scanf("%lld",&t);
    while(t--)
    {
        sum=0;
        scanf("%lld %lld",&k,&n);
        pp(k);
        int m=(int)sqrt(n+0.5);
        for(i=1;i<=m;i++)
        {
            if(n%i==0)
            {
                sum=(sum+mod_power(k,n/i)*euler_phi(i)%MOD)%MOD;
                if(n/i!=i)
                    sum=(sum+mod_power(k,i)*euler_phi(n/i)%MOD)%MOD;
            }
        }
        printf("%lld\n",sum);
    }
    return 0;
}

發佈了52 篇原創文章 · 獲贊 9 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章