跳到正文

个人记录

组合数第 n 行第模 k 余 d 列求和

wukaichen888约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前
C++
//  save me

#pragma target("avx512f,sse2,sse3,sse4,sse4.2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=100005,mod=1e9+7;
ll t,n,k,a[N],p;
struct qwq{ll w[105];}T0,T1,p1[64],res;
inline void add(ll &x,ll y){x+=y;(x>=mod)?(x-=mod):1;}
inline int get(int x){return (x>=k)?(x-k):x;}
inline qwq mul(qwq x,qwq y){
	qwq z=T0;
	for(int i=0;i<k;i++)
		for(int j=0;j<k;j++)
			add(z.w[get(i+j)],x.w[i]*y.w[j]%mod);
	return z;
}
ll power(ll x,ll y){
	ll res=1ll;
	while(y){
		if(y&1ll) res=res*x%mod;
		x=x*x%mod;
		y>>=1ll;
	}
	return res;
}
signed main(){
	T1.w[0]=1;
	p1[0].w[0]=p1[0].w[1]=1;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&k);
		if(k==1){
			printf("%lld\n",(power(2,n)-1+mod)%mod);
			continue;
		}
		if(k==2){
			printf("%lld\n",power(2,n-1));
			continue;
		}
		for(int i=1;(1<<i)<=n;i++) p1[i]=mul(p1[i-1],p1[i-1]);
		res=T1;
		for(int i=29;i>=0;i--)
			if((n>>i)&1)
				res=mul(res,p1[i]);
		printf("%lld\n",res.w[1]);
	}
	return 0;
}
原文