洛谷 P1593 因子和

https://www.luogu.org/problemnew/show/P1593#sub

利用约数和定理:可以去看一下公式第13条

然后这个题目的话,要求$a^b$,那么我们首先可以先将a分解然后给指数乘上$b$.

然后我们就需要计算$(1+p+p^2+....p^k)$因为k可能特别大,所以直接计算是不可能了。

费马小定理可以解决这个问题:公式第14条

$$a^x \equiv a^{\mu(x)}mod p,\mu(x)=x-1 $$

因为模数比较小那么在我们计算的时候显然会有循环节的出现,那么我们只需要计算这个循环节就好了。

然后将每一个质因数的答案想乘就可以得到答案啦。

注意开$long long$

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
#define mod 9901
#define LL long long
LL a,b,x;
LL pri[1000006],cnt[1000006],ans,tot,pw[1000006];
int main()
{
    cin>>a>>b;
    x=a;
    for(int i=2;i*i<=a;i++)
    {
        if(x%i==0)
        {
            pri[++tot]=i;
            while(x%i==0)
            {
                cnt[tot]++;
                x/=i;
            }
        }
    }
    if(x!=1)
    {
        pri[++tot]=x;
        cnt[tot]=1;
    }
    ans=1;
    for(int i=1;i<=tot;i++)cnt[i]*=b;
    for(int i=tot;i>=1;i--)
    {
        pw[0]=1;
        LL s=1,as=1;
        for(int j=1;j<=9899&&j<=cnt[i];j++)
        {
            pw[j]=pw[j-1]*pri[i]%mod;
            (s=s+pw[j])%=mod;
            if(cnt[i]%9900==j)as=s;
        }
        ans=(ans*((cnt[i]/9900)*s+as)%mod)%mod;
    }
    cout<<ans;
}

猜你喜欢

转载自www.cnblogs.com/rmy020718/p/9828621.html
今日推荐