https://www.luogu.com.cn/problem/P17064
,这是从 次数的差值考虑的,然后做一下偏移就没有负数次幂了。
考虑 。
那么分解 为 进制 ,则可以表示为 。
考虑 ,设 选择了 这一项进行贡献,那么有 。要求 。
考虑上式在 意义下是 。发现根据 的范围,可以得到 。
那么可以同时去掉最低位然后归纳证明。于是 。
可以预处理一个表 表示 时的方案数,则 可以通过对 分解质因数后得到是 。
考虑 ,其中 表示所有 11 进制分解后数位是 的个数乘上 之和。
对于每个 ,拆位处理,那么每一位是类似求很多个组合数上指标区间和。。
每个都是形如 ,注意是在 意义下,需要 CRT 合并。
根据卢卡斯定理,显然有循环节,且是 级别的。
复杂度大概是 ,可以通过。
code
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long LL;
const int N=11,M=2e5+10,P=11;
LL T,n,c,C[N][N],f[N],crt[2][5],zsw[M];
LL lqs(LL a,LL b,const LL &P)
{
if(P==2) return (a&b)==b;
if(~zsw[a]) return zsw[a];
LL res=1,A=a;
while(a&&b) res=res*C[a%P][b%P]%P,a/=P,b/=P;
return zsw[A]=res;
}
struct state { LL n,x,cir; };
map<state,LL> mem;
bool operator <(state a,state b)
{
if(a.cir!=b.cir) return a.cir<b.cir;
if(a.x!=b.x) return a.x<b.x;
return a.n<b.n;
}
LL gcd(LL a,LL b) { return !b?a:gcd(b,a%b); }
LL zxz(LL n,LL x,const LL &P)
{
for(int i=0;i<P;i++)
for(int j=0;j<=i;j++)
C[i][j]=(!j||j==i?1:(C[i-1][j-1]+C[i-1][j])%P);
LL cir=1;
while(cir<=c) cir*=P;
LL ful=0,res=0;
state X={n%gcd(x,cir),gcd(x,cir),cir};
if(mem.find(X)==mem.end())
{
for(int i=0;i<cir;i++) ful=(ful+lqs(n%cir-(x%cir)*i%cir+cir+c,c,P))%P;
mem[X]=ful;
}
else ful=mem[X];
res=ful*(n/x/cir)%P;
n=n%((__int128)cir*x);
for(int i=0;i<=n/x;i++)
res=(res+lqs(n%cir-(x%cir)*i%cir+cir+c,c,P))%P;
return res;
}
LL comb(LL n,LL x) { return crt[zxz(n,x,2)][zxz(n,x,5)]; }
LL cnt(LL d)
{
LL res=0;
for(LL i=0,p=1;p<=n;p*=11,i++)
{
if(n>=d*p) res=(res+comb(n-d*p,11*p))%10;
if(n>=(d+1)*p) res=(res-comb(n-(d+1)*p,11*p)+10)%10;
}
return res;
}
void solve()
{
memset(zsw,-1,sizeof(zsw));
mem.clear();
scanf("%llu%llu",&n,&c);
LL ans=1;
for(int i=0;i<P;i++)
{
LL pw=cnt(i);
while(pw--) ans=ans*f[i]%P;
}
printf("%llu\n",ans);
}
int main()
{
for(int i=0;i<P;i++) crt[i%2][i%5]=i;
for(int i=0;i<N;i++)
for(int j=0;j<=i;j++)
C[i][j]=(!j||j==i?1:(C[i-1][j-1]+C[i-1][j])%P);
for(int i=0;i<N;i++)
{
for(int j=0;j<=i/2;j++) f[i]+=C[i][j]*C[i-j][j];
f[i]%=P;
}
scanf("%llu",&T);
while(T--) solve();
return 0;
}