[排列组合]做题记录-古代猪文

这就是传说中的数论全家桶么……

用到了Lucas定理,中国剩余定理,欧拉定理

大体而言可以拆数发现999911658=2*3*4679*35617

然后跑四遍CRT即可

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#define WR WinterRain
using namespace std;
const long long WR=10010000,mod=999911659;
long long tot,bs,val;
long long g[WR],b[5]={0,2,3,4679,35617},a[5];
long long read(){
    long long s=0,w=1;
    char ch=getchar();
    while(ch>'9'||ch<'0'){
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        s=(s<<3)+(s<<1)+ch-48;
        ch=getchar();
    }
    return s*w;
}
long long quick_pow(long long a,long long b,long long md){
    long long ans=1,base=a;
    while(b>0){
        if(b&1) ans=ans*base%md;
        base=base*base%md;
        b>>=1;
    }
    return ans;
}
long long comb(long long n,long long m,long long md){
    if(n<m) return 0;
    return g[n]*quick_pow(g[m],md-2,md)*quick_pow(g[n-m],md-2,md);
}
long long Lucas(long long n,long long m,long long md){
    if(!n) return 1;
    if(n<m) return 0;
    return comb(n%md,m%md,md)*Lucas(n/md,m/md,md)%md;
}
void CRT(long long n,long long md){
    for(long long i=1;i<=n;i++){
        val=(val+a[i]*(md/b[i])%md*quick_pow(md/b[i],b[i]-2,b[i])%md)%md;
    }
}
int main(){
    tot=read(),bs=read();
    g[0]=1;
    /*long long tmp=mod-1;
    for(long long i=2;i<=sqrt(tmp);i++){
        if(tmp%i==0){
            bool f=true;
            for(long long j=2;j<=sqrt(i);j++){
                if(i%j==0){
                    f=false;
                    break;
                }
            }
            if(f){
                prlong longf("%d\n",i);
                tmp/=i;
            }
        }
    }
    printf("%lld",tmp);*/
    for(long long i=1;i<=4;i++){
        for(long long j=1;j<=b[i];j++) g[j]=g[j-1]*j%b[i];
        for(long long j=1;j<=sqrt(tot);j++){
            if(tot%j==0){
                a[i]=(a[i]+Lucas(tot,j,b[i]))%b[i];
                if(j*j!=tot) a[i]=(a[i]+Lucas(tot,tot/j,b[i]))%b[i];
            }
        }
    }
    CRT(4,mod-1);
    printf("%lld",quick_pow(bs,val,mod));
    return 0;
}

 

posted @ 2022-05-09 07:36  冬天丶的雨  阅读(26)  评论(0编辑  收藏  举报
Live2D