#88pts WA求条-NOIP2024最后一天

P1593 因子和

AKCSPS @ 2024-11-29 14:42:20

#include<stdio.h>
#include<cstring>
using namespace std;
#define ll long long
const int N=5e7;
const int mod=9901;
ll a,b;
bool isprime[N+5];
int prime[N+5],len;
ll ans=1;
struct Node{
    ll p;
    ll r;
}t[N+5];
int n;
void DoPrime(ll a){
    memset(isprime,1,sizeof(isprime));
    isprime[1]=0;
    isprime[0]=0;
    for(ll i=2;i*i<=a;i++)
        if(isprime[i]){
            prime[++len]=i;
            for(ll j=i+i;j*j<=a;j+=i)
                isprime[j]=0;
        }
    return ;
}
ll FastPow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1)ans=(ans*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return ans;
}
ll cal(ll p,ll r){
    ll up=(FastPow(p,r+1)+mod-1)%mod;
    ll down=FastPow(p-1,mod-2);
    //printf("%d^%d => up=%d down=%d => ans=%d\n",p,r,up,down,(up*down)%mod);
    return (up*down)%mod;
//  ll up=FastPow(p,r+1);
//  ll down=FastPow(p-1,mod-2);
//  return ((up*down)%mod-down+mod)%mod;
}
int main(){
    scanf("%lld %lld",&a,&b);
    DoPrime(a);
    int i=1;
    while(a!=1&&i<=len){
        if(a%prime[i]==0){
            t[++n].p=prime[i];
            while(a%prime[i]==0){
                t[n].r++;
                a/=prime[i];
            }
        }
        i++;
    }
    if(a!=1){
        t[++n].p=a;
        t[n].r=1;
    }
    for(int i=1;i<=n;i++){
        //printf("%d^%d\n",t[i].p,t[i].r);
        ans=(ans*cal(t[i].p,t[i].r*b))%mod;
    }
    printf("%lld",ans);
    return 0;
}

by _XHY20180718_ @ 2024-11-29 21:59:48

@AKCSPS

逆元不存在的情况没考虑


|