类欧几里得

参考资料:cz_xuyixuan

要求这么一类问题

\[\begin{aligned} f(a,b,c,n) &=\sum_{i=0}^n \left\lfloor{ai+b\over c}\right\rfloor \end{aligned} \]

开始颓柿子

  • \(Case 1:a=0\)

\[\begin{aligned} f(a,b,c,n) &=(n+1)\left\lfloor{b\over c}\right\rfloor\\ \end{aligned} \]

  • \(Case 2:a\geq c\)\(b\geq c\)

\[\begin{aligned} f(a,b,c,n) &=\sum_{i=0}^n \left\lfloor{ai+b\over c}\right\rfloor\\ &=\sum_{i=0}^n \left\lfloor{(a\bmod c)i+(b\bmod c)\over c}\right\rfloor+{n(n+1)\over 2}\left\lfloor{a\over c}\right\rfloor+(n+1)\left\lfloor{b\over c}\right\rfloor\\ &=f(a\bmod c,b\bmod c,n)+{n(n+1)\over 2}\left\lfloor{a\over c}\right\rfloor+(n+1)\left\lfloor{b\over c}\right\rfloor\\ \end{aligned} \]

  • \(Case 3:Otherwise\)

\(m=\left\lfloor{an+b\over c}\right\rfloor\),显然有\(m\leq n\)

\[\begin{aligned} f(a,b,c,n) &=\sum_{i=0}^n \left\lfloor{ai+b\over c}\right\rfloor\\ &=\sum_{i=0}^n \sum_{j=1}^m\left[\left\lfloor{ai+b\over c}\right\rfloor\geq j\right]\\ &=\sum_{i=0}^n \sum_{j=0}^{m-1}\left[\left\lfloor{ai+b\over c}\right\rfloor\geq j+1\right]\\ &=\sum_{i=0}^n \sum_{j=0}^{m-1}\left[ai>cj+c-b-1\right]\\ &=\sum_{i=0}^n \sum_{j=0}^{m-1}\left[i>\left\lfloor{cj+c-b-1\over a}\right\rfloor\right]\\ &=\sum_{j=0}^{m-1}n-\left\lfloor{cj+c-b-1\over a}\right\rfloor\\ &=nm-f(c,c-b-1,a,m-1)\\ \end{aligned} \]

发现这个过程中\(a,c\)的迭代类似\(gcd\),所以这个算法的复杂度也是\(O(\log n)\)

另外的两个扩展不学了……

2019.11.4 upd:被jz姐姐欺负了之后只好去学一下扩展

我们需要计算

\[\begin{aligned} f(n,a,b,c) &=\sum_{i=0}^n i^{k_1}\left\lfloor{ai+b\over c}\right\rfloor^{k_2} \end{aligned} \]

  • \(Case 1:a=0\)\(\left\lfloor{an+b\over c}\right\rfloor=0\)

此时\(\left\lfloor{ai+b\over c}\right\rfloor=0\)的值始终相等,则

\[\begin{aligned} f(n,a,b,c) &=\left\lfloor{ai+b\over c}\right\rfloor^{k_2}\sum_{i=0}^n i^{k_1} \end{aligned} \]

后面那个可以插值计算

  • \(Case 2:a\geq c\)

\(q=\left\lfloor{a\over c}\right\rfloor,r=a-qc\),则

\[\begin{aligned} f(n,a,b,c) &=\sum_{i=0}^n i^{k_1}\left(qi+\left\lfloor{ri+b\over c}\right\rfloor\right)^{k_2}\\ &=\sum_{i=0}^n i^{k_1}\sum_{j=0}^{k_2}{k_2\choose j}(qi)^{k_2}\left\lfloor{ri+b\over c}\right\rfloor^{k_2-j}\\ &=\sum_{j=0}^{k_2}{k_2\choose j}q^{k_2}\sum_{i=0}^n i^{k_1+j}\left\lfloor{ri+b\over c}\right\rfloor^{k_2-j}\\ \end{aligned} \]

递归算一下\(f(n,r,b,c)\)即可

  • \(Case 3:b\geq c\)

和上面类似,记\(q=\left\lfloor{b\over c}\right\rfloor,r=b-qc\),则

\[\begin{aligned} f(n,a,b,c) &=\sum_{i=0}^n i^{k_1}\left(q+\left\lfloor{ri+b\over c}\right\rfloor\right)^{k_2}\\ &=\sum_{i=0}^n i^{k_1}\sum_{j=0}^{k_2}{k_2\choose j}q^{k_2}\left\lfloor{ri+b\over c}\right\rfloor^{k_2-j}\\ &=\sum_{j=0}^{k_2}{k_2\choose j}q^{k_2}\sum_{i=0}^n i^{k_1}\left\lfloor{ri+b\over c}\right\rfloor^{k_2-j}\\ \end{aligned} \]

递归算一下\(f(n,a,r,c)\)即可

  • \(Case 4:Otherwise\)

我们令\(m=\left\lfloor{an+b\over c}\right\rfloor\),则此时有\(m<n\)

考虑将\(\left\lfloor{ai+b\over c}\right\rfloor^{k_2}\)变形,规定\(0^0=0\),则有

\[\begin{aligned} \left\lfloor{ai+b\over c}\right\rfloor^{k_2} &=\sum_{j=0}^{\left\lfloor{ai+b\over c}\right\rfloor-1}\left((j+1)^{k_2}-j^{k_2}\right) \end{aligned} \]

则原式可变为

\[\begin{aligned} f(n,a,b,c) &=\sum_{i=0}^n i^{k_1}\sum_{j=0}^{\left\lfloor{ai+b\over c}\right\rfloor-1}\left((j+1)^{k_2}-j^{k_2}\right)\\ &=\sum_{j=0}^{m-1}\left((j+1)^{k_2}-j^{k_2}\right)\sum_{i=0}^n i^{k_1}\left[\left\lfloor{ai+b\over c}\right\rfloor\geq j+1\right]\\ &=\sum_{j=0}^{m-1}\left((j+1)^{k_2}-j^{k_2}\right)\sum_{i=0}^n i^{k_1}\left[i>\left\lfloor{cj+c-b-1\over a}\right\rfloor\right]\\ &=\sum_{j=0}^{m-1}\left((j+1)^{k_2}-j^{k_2}\right)\sum_{i=0}^n i^{k_1}-\sum_{j=0}^{m-1}\left((j+1)^{k_2}-j^{k_2}\right)\sum_{i=0}^{\left\lfloor{cj+c-b-1\over a}\right\rfloor} i^{k_1}\\ \end{aligned} \]

前面的可以直接插值计算,现在考虑后面

由于\(\sum_{i=0}^{\left\lfloor{cj+c-b-1\over a}\right\rfloor} i^{k_1}\)是关于\(\left\lfloor{cj+c-b-1\over a}\right\rfloor\)的一个\(k_1+1\)次多项式,我们假设这个多项式为\(A(x)\),则可以推得

\[\begin{aligned} \sum_{j=0}^{m-1}\left((j+1)^{k_2}-j^{k_2}\right)\sum_{i=0}^{\left\lfloor{cj+c-b-1\over a}\right\rfloor} i^{k_1} &=\sum_{j=0}^{m-1}\left((j+1)^{k_2}-j^{k_2}\right)\sum_{k=0}^{k_1+1} B_k{\left\lfloor{cj+c-b-1\over a}\right\rfloor}^k \end{aligned} \]

再把前面\(\left((j+1)^{k_2}-j^{k_2}\right)\)的二项式展开,有

\[\begin{aligned} \sum_{j=0}^{m-1}\left((j+1)^{k_2}-j^{k_2}\right)\sum_{k=0}^{k_1+1} B_k{\left\lfloor{cj+c-b-1\over a}\right\rfloor}^k &=\sum_{j=0}^{k_2-1}{k_2\choose j}\sum_{k=0}^{k_1+1}B_k\sum_{i=0}^{m-1}i^j{\left\lfloor{cj+c-b-1\over a}\right\rfloor}^k \end{aligned} \]

递归计算\(f(m-1,c,c-b-1,a)\)即可

不难发现递归层数是\(O(\log n)\)级别的

总复杂度为\(O(k^4\log n)\)

传送门

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int P=1e9+7;
inline void upd(R int &x,R int y){(x+=y)>=P?x-=P:0;}
inline int inc(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
	R int res=1;
	for(;y;y>>=1,x=mul(x,x))(y&1)?res=mul(res,x):0;
	return res;
}
const int N=25;
int bn[N][N],val[N],g[N],h[N],f[N];
struct node{
	int sz,v[N];
	void init(int *f){
		memset(g,0,sizeof(g));
		g[0]=1;
		int i,j;
		for(i=0;i<21;++i){
			for(j=i+1;j;--j) 
			 g[j]=dec(g[j-1],1ll*g[j]*i%P);
			g[0]=(P-1ll*g[0]*i%P)%P;
		}
		fp(i,0,20){
			memcpy(h,g,sizeof(h));
			fd(j,21,1)upd(h[j-1],mul(h[j],i));
			R int res=1;
			fp(j,0,i-1)res=mul(res,i-j);
			fp(j,i+1,20)res=mul(res,P+i-j);
			res=mul(f[i],ksm(res,P-2));
			fp(j,0,20)upd(v[j],mul(res,h[j+1]));
		}
		for(sz=21;sz&&!v[sz];--sz);
	}
	inline int calc(R int x){
		R int res=0,pw=1;
		for(R int i=0;i<=sz;++i,pw=mul(pw,x))upd(res,mul(pw,v[i]));
		return res;
	}
}L[N];
struct Base{
	int a[11][11];
	inline Base(){memset(a,0,sizeof(a));}
	inline int* operator [](const int &x){return a[x];}
};
Base func(int n,int a,int b,int c){
	Base ans;
	if(!a||1ll*a*n+b<c){
		fp(k1,0,10){
			R int res=L[k1].calc(n),pw=(1ll*a*n+b)/c;
			fp(k2,0,10-k1)ans[k1][k2]=res,res=mul(res,pw);
		}
		return ans;
	}
	if(a>=c){
		Base las=func(n,a%c,b,c);
		fp(k1,0,10)fp(k2,0,10-k1){
			R int res=1,pw=a/c;
			fp(i,0,k2){
				upd(ans[k1][k2],1ll*bn[k2][i]*res%P*las[k1+i][k2-i]%P);
				res=mul(res,pw);
			}
		}
		return ans;
	}
	if(b>=c){
		Base las=func(n,a,b%c,c);
		fp(k1,0,10)fp(k2,0,10-k1){
			R int res=1,pw=b/c;
			fp(i,0,k2){
				upd(ans[k1][k2],1ll*bn[k2][i]*res%P*las[k1][k2-i]%P);
				res=mul(res,pw);
			}
		}
		return ans;
	}
	int m=(1ll*a*n+b)/c;
	Base las=func(m-1,c,c-b-1,a);
	fp(k1,0,10){
		R int res=L[k1].calc(n);
		fp(k2,0,10-k1){
			ans[k1][k2]=mul(ksm(m,k2),res);
			fp(i,0,k2-1)fp(j,0,k1+1){
				R int coef=mul(bn[k2][i],L[k1].v[j]);
				upd(ans[k1][k2],P-mul(coef,las[i][j]));
			}
		}
	}
	return ans;
}
void init(){
	fp(i,0,20){
		bn[i][0]=1;
		fp(j,1,i)bn[i][j]=inc(bn[i-1][j],bn[i-1][j-1]);
	}
	fp(i,0,10){
		R int res=0;
		fp(j,0,20)upd(res,ksm(j,i)),f[j]=res;
		L[i].init(f);
	}
}
int main(){
//	freopen("testdata.in","r",stdin);
	init();
	int T,n,a,b,c,k1,k2;
	for(scanf("%d",&T);T;--T){
		scanf("%d%d%d%d%d%d",&n,&a,&b,&c,&k1,&k2);
		printf("%d\n",func(n,a,b,c)[k1][k2]);
	}
	return 0;
}
posted @ 2019-11-01 15:13  源曲明  阅读(189)  评论(2编辑  收藏  举报