题目描述
题解
写这道题各种傻逼
根据欧拉定理,
G∑k|nCkn%P=G∑k|nCkn%φ(P)%P
模数是质数,
φ(P)=P−1
,然而我刚开始还想什么根n求phi
n的所有的因数都可以在
n−−√
求出来
然后把
φ
因数分解了之后可以发现P=2*3*4679*35617
全是质数直接上lucas不就行了
然而我刚开始写了个扩展lucas结果T飞了
结果用中国剩余定理合并
不过这题有一个特殊数据就是
(G,P)≠1
,不能用欧拉定理
防止它直接搞成1所以变成
G∑k|nCkn%φ(P)+φ(P)%P
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long
const LL MOD=999911659;
const LL phi=MOD-1;
LL n,G,p[10],ans,mul[5][36000];
int cnt;
LL fast_pow(LL a,LL p,LL Mod)
{
LL ans=1LL;
for (;p;p>>=1,a=a*a%Mod)
if (p&1)
ans=ans*a%Mod;
return ans;
}
void exgcd(LL a,LL b,LL &x,LL &y)
{
if (!b) x=1LL,y=0LL;
else exgcd(b,a%b,y,x),y-=a/b*x;
}
LL inv(LL A,LL Mod)
{
LL a=A,b=Mod,x=0LL,y=0LL;
exgcd(a,b,x,y);
x=(x%b+b)%b;
if (!x) x+=b;
return x;
}
LL C(LL n,LL m,int i)
{
if (m>n) return 0LL;
return mul[i][n]*inv(mul[i][m]*mul[i][n-m]%p[i],p[i])%p[i];
}
LL lucas(LL n,LL m,int i)
{
if (m>n) return 0LL;
LL ans=1LL;
for (;m;n/=p[i],m/=p[i])
ans=ans*C(n%p[i],m%p[i],i)%p[i];
return ans;
}
LL calc(LL n,LL m)
{
LL now,ans=0LL;
for (int i=1;i<=cnt;++i)
{
now=lucas(n,m,i);
ans=(ans+now*(phi/p[i])%phi*inv(phi/p[i],p[i])%phi)%phi;
}
return ans;
}
void get(LL x)
{
for (LL i=2;x>1&&i*i<=x;++i)
if (x%i==0)
{
p[++cnt]=i;
while (x%i==0) x/=i;
}
if (x>1) p[++cnt]=x;
for (int i=1;i<=cnt;++i)
{
mul[i][0]=1LL;
for (LL j=1;j<=35617LL;++j) mul[i][j]=mul[i][j-1]*j%p[i];
}
}
int main()
{
get(phi);
scanf("%lld%lld",&n,&G);
for (LL i=1;i*i<=n;++i)
if (n%i==0)
{
ans=(ans+calc(n,i))%phi;
if (n/i!=i) ans=(ans+calc(n,n/i))%phi;
}
ans=fast_pow(G%MOD,ans+phi,MOD);
printf("%lld\n",ans);
}
sb扩展(tle)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long
const LL MOD=999911659;
const LL phi=MOD-1;
LL n,G,pi[10],pk[10],ans;
int cnt;
LL fast_pow(LL a,LL p,LL Mod)
{
LL ans=1LL;
for (;p;p>>=1,a=a*a%Mod)
if (p&1)
ans=ans*a%Mod;
return ans;
}
void exgcd(LL a,LL b,LL &x,LL &y)
{
if (!b) x=1LL,y=0LL;
else exgcd(b,a%b,y,x),y-=a/b*x;
}
LL inv(LL A,LL Mod)
{
LL a=A,b=Mod,x=0LL,y=0LL;
exgcd(a,b,x,y);
x=(x%b+b)%b;
if (!x) x+=b;
return x;
}
LL Mul(LL n,LL pi,LL pk)
{
if (!n) return 1LL;
LL ans=1LL;
for (LL i=2;i<=pk;++i)
if (i%pk) ans=ans*i%pk;
ans=fast_pow(ans,n/pk,pk);
for (LL i=2;i<=n%pk;++i)
if (i%pk) ans=ans*i%pk;
return ans*Mul(n/pk,pi,pk)%pk;
}
LL C(LL n,LL m,LL Mod,LL pi,LL pk)
{
if (m>n) return 0LL;
LL a=Mul(n,pi,pk),b=Mul(m,pi,pk),c=Mul(n-m,pi,pk);
LL k=0LL,ans;
for (LL i=n;i;i/=pi) k+=i/pi;
for (LL i=m;i;i/=pi) k-=i/pi;
for (LL i=n-m;i;i/=pi) k-=i/pi;
ans=a*inv(b,pk)%pk*inv(c,pk)%pk*fast_pow(pi,k,pk)%pk;
return ans*(Mod/pk)%Mod*inv(Mod/pk,pk)%Mod;
}
LL calc(LL n,LL m)
{
LL ans=0LL;
for (int i=1;i<=cnt;++i)
ans=(ans+C(n,m,phi,pi[i],pk[i]))%phi;
return ans;
}
void get(LL x)
{
for (LL i=2;x>1&&i*i<=x;++i)
if (x%i==0)
{
pi[++cnt]=i;
pk[cnt]=1LL;
while (x%i==0) pk[cnt]*=i,x/=i;
}
if (x>1) pi[++cnt]=pk[cnt]=x;
}
int main()
{
get(phi);
scanf("%lld%lld",&n,&G);
for (LL i=1;i*i<=n;++i)
if (n%i==0)
{
ans=(ans+calc(n,i))%phi;
if (n/i!=i) ans=(ans+calc(n,n/i))%phi;
}
ans=fast_pow(G%MOD,ans+phi,MOD);
printf("%lld\n",ans);
}