标签:因此 适应 编写 gre 简单的 未来 简化 http open
10%的数据中,1 <= N <= 50;
20%的数据中,1 <= N <= 1000;
40%的数据中,1 <= N <= 100000;
100%的数据中,1 <= G <= 1000000000,1 <= N <= 1000000000。
#include <bits/stdc++.h> #define ll long long #define inf 1e9+10 using namespace std; inline ll read(){ ll x=0;int f=1;char ch=getchar(); while(!isdigit(ch)) {if(ch==‘-‘) f=-1;ch=getchar();} while(isdigit(ch)) {x=x*10+ch-‘0‘;ch=getchar();} return x*f; } ll p[4]={2,3,4679,35617}; ll fac[4][36000],ans[5],n,g,q[3000010],top; inline ll fpow(ll x,ll y,ll mod){ ll res=1; while(y){ if(y&1) res=(res*x)%mod; x=(x*x)%mod; y>>=1; } return res; } inline ll extend_gcd(ll a,ll b,ll &x,ll &y){ if(!b){ x=1;y=0;return a; } ll d=extend_gcd(b,a%b,y,x); y-=(a/b)*x; return d; } inline ll lucas(int i,ll n,ll m){ ll res=1; while(n&&m){ ll nn=n%p[i];ll mm=m%p[i]; if(nn<mm) return 0; //cout<<res<<‘ ‘<<p[i]<<‘ ‘<<fac[i][nn]<<‘ ‘<<fac[i][nn-mm]<<‘ ‘<<fac[i][mm]<<endl; res=res*fac[i][nn]%p[i]*fpow(fac[i][nn-mm]*fac[i][mm]%p[i],p[i]-2,p[i])%p[i]; //cout<<i<<‘ ‘<<nn<<‘ ‘<<mm<<‘ ‘<<res<<endl; n/=p[i];m/=p[i]; } //cout<<res<<endl; return res; } int main(){ //freopen("All.in","r",stdin); //freopen("zh.out","w",stdout); n=read();g=read(); if(g==999911659){ cout<<0<<endl;return 0; } for(int i=0;i<4;i++) fac[i][0]=1; for(int j=0;j<4;j++){ for(int i=1;i<=p[j];i++){ fac[j][i]=fac[j][i-1]*i%p[j]; } } for(int i=1;i*i<=n;i++){ if(n%i==0){ if(i*i==n) q[++top]=i; else q[++top]=i,q[++top]=n/i; } } for(int i=1;i<=top;i++){ for(int j=0;j<4;j++){ ans[j]+=lucas(j,n,q[i]),ans[j]%=p[j]; } } for(int i=0;i<3;i++){ ll k1,k2,p1=p[i],p2=p[i+1]; p[i+1]*=p[i]; extend_gcd(p1,p2,k1,k2); k1=(k1%p2)*(ans[i+1]-ans[i]); ans[i+1]=(ans[i]+k1*p1)%p[i+1]; } cout<<fpow(g,(ans[3]+p[3])%p[3],999911659); return 0; }
标签:因此 适应 编写 gre 简单的 未来 简化 http open
原文地址:https://www.cnblogs.com/something-for-nothing/p/9026883.html