参考资料:cz_xuyixuan
要求这么一类问题
[egin{aligned}
f(a,b,c,n)
&=sum_{i=0}^n leftlfloor{ai+bover c}
ight
floor
end{aligned}
]
开始颓柿子
- (Case 1:a=0)
[egin{aligned}
f(a,b,c,n)
&=(n+1)leftlfloor{bover c}
ight
floor\
end{aligned}
]
- (Case 2:ageq c)或(bgeq c)
[egin{aligned}
f(a,b,c,n)
&=sum_{i=0}^n leftlfloor{ai+bover c}
ight
floor\
&=sum_{i=0}^n leftlfloor{(amod c)i+(bmod c)over c}
ight
floor+{n(n+1)over 2}leftlfloor{aover c}
ight
floor+(n+1)leftlfloor{bover c}
ight
floor\
&=f(amod c,bmod c,n)+{n(n+1)over 2}leftlfloor{aover c}
ight
floor+(n+1)leftlfloor{bover c}
ight
floor\
end{aligned}
]
- (Case 3:Otherwise)
令(m=leftlfloor{an+bover c} ight floor),显然有(mleq n)
[egin{aligned}
f(a,b,c,n)
&=sum_{i=0}^n leftlfloor{ai+bover c}
ight
floor\
&=sum_{i=0}^n sum_{j=1}^mleft[leftlfloor{ai+bover c}
ight
floorgeq j
ight]\
&=sum_{i=0}^n sum_{j=0}^{m-1}left[leftlfloor{ai+bover c}
ight
floorgeq j+1
ight]\
&=sum_{i=0}^n sum_{j=0}^{m-1}left[ai>cj+c-b-1
ight]\
&=sum_{i=0}^n sum_{j=0}^{m-1}left[i>leftlfloor{cj+c-b-1over a}
ight
floor
ight]\
&=sum_{j=0}^{m-1}n-leftlfloor{cj+c-b-1over a}
ight
floor\
&=nm-f(c,c-b-1,a,m-1)\
end{aligned}
]
发现这个过程中(a,c)的迭代类似(gcd),所以这个算法的复杂度也是(O(log n))的
另外的两个扩展不学了……
2019.11.4 upd:被jz姐姐欺负了之后只好去学一下扩展
我们需要计算
[egin{aligned}
f(n,a,b,c)
&=sum_{i=0}^n i^{k_1}leftlfloor{ai+bover c}
ight
floor^{k_2}
end{aligned}
]
- (Case 1:a=0)或(leftlfloor{an+bover c} ight floor=0)
此时(leftlfloor{ai+bover c} ight floor=0)的值始终相等,则
[egin{aligned}
f(n,a,b,c)
&=leftlfloor{ai+bover c}
ight
floor^{k_2}sum_{i=0}^n i^{k_1}
end{aligned}
]
后面那个可以插值计算
- (Case 2:ageq c)
记(q=leftlfloor{aover c} ight floor,r=a-qc),则
[egin{aligned}
f(n,a,b,c)
&=sum_{i=0}^n i^{k_1}left(qi+leftlfloor{ri+bover c}
ight
floor
ight)^{k_2}\
&=sum_{i=0}^n i^{k_1}sum_{j=0}^{k_2}{k_2choose j}(qi)^{k_2}leftlfloor{ri+bover c}
ight
floor^{k_2-j}\
&=sum_{j=0}^{k_2}{k_2choose j}q^{k_2}sum_{i=0}^n i^{k_1+j}leftlfloor{ri+bover c}
ight
floor^{k_2-j}\
end{aligned}
]
递归算一下(f(n,r,b,c))即可
- (Case 3:bgeq c)
和上面类似,记(q=leftlfloor{bover c} ight floor,r=b-qc),则
[egin{aligned}
f(n,a,b,c)
&=sum_{i=0}^n i^{k_1}left(q+leftlfloor{ri+bover c}
ight
floor
ight)^{k_2}\
&=sum_{i=0}^n i^{k_1}sum_{j=0}^{k_2}{k_2choose j}q^{k_2}leftlfloor{ri+bover c}
ight
floor^{k_2-j}\
&=sum_{j=0}^{k_2}{k_2choose j}q^{k_2}sum_{i=0}^n i^{k_1}leftlfloor{ri+bover c}
ight
floor^{k_2-j}\
end{aligned}
]
递归算一下(f(n,a,r,c))即可
- (Case 4:Otherwise)
我们令(m=leftlfloor{an+bover c} ight floor),则此时有(m<n)
考虑将(leftlfloor{ai+bover c} ight floor^{k_2})变形,规定(0^0=0),则有
[egin{aligned}
leftlfloor{ai+bover c}
ight
floor^{k_2}
&=sum_{j=0}^{leftlfloor{ai+bover c}
ight
floor-1}left((j+1)^{k_2}-j^{k_2}
ight)
end{aligned}
]
则原式可变为
[egin{aligned}
f(n,a,b,c)
&=sum_{i=0}^n i^{k_1}sum_{j=0}^{leftlfloor{ai+bover c}
ight
floor-1}left((j+1)^{k_2}-j^{k_2}
ight)\
&=sum_{j=0}^{m-1}left((j+1)^{k_2}-j^{k_2}
ight)sum_{i=0}^n i^{k_1}left[leftlfloor{ai+bover c}
ight
floorgeq j+1
ight]\
&=sum_{j=0}^{m-1}left((j+1)^{k_2}-j^{k_2}
ight)sum_{i=0}^n i^{k_1}left[i>leftlfloor{cj+c-b-1over a}
ight
floor
ight]\
&=sum_{j=0}^{m-1}left((j+1)^{k_2}-j^{k_2}
ight)sum_{i=0}^n i^{k_1}-sum_{j=0}^{m-1}left((j+1)^{k_2}-j^{k_2}
ight)sum_{i=0}^{leftlfloor{cj+c-b-1over a}
ight
floor} i^{k_1}\
end{aligned}
]
前面的可以直接插值计算,现在考虑后面
由于(sum_{i=0}^{leftlfloor{cj+c-b-1over a} ight floor} i^{k_1})是关于(leftlfloor{cj+c-b-1over a} ight floor)的一个(k_1+1)次多项式,我们假设这个多项式为(A(x)),则可以推得
[egin{aligned}
sum_{j=0}^{m-1}left((j+1)^{k_2}-j^{k_2}
ight)sum_{i=0}^{leftlfloor{cj+c-b-1over a}
ight
floor} i^{k_1}
&=sum_{j=0}^{m-1}left((j+1)^{k_2}-j^{k_2}
ight)sum_{k=0}^{k_1+1} B_k{leftlfloor{cj+c-b-1over a}
ight
floor}^k
end{aligned}
]
再把前面(left((j+1)^{k_2}-j^{k_2} ight))的二项式展开,有
[egin{aligned}
sum_{j=0}^{m-1}left((j+1)^{k_2}-j^{k_2}
ight)sum_{k=0}^{k_1+1} B_k{leftlfloor{cj+c-b-1over a}
ight
floor}^k
&=sum_{j=0}^{k_2-1}{k_2choose j}sum_{k=0}^{k_1+1}B_ksum_{i=0}^{m-1}i^j{leftlfloor{cj+c-b-1over a}
ight
floor}^k
end{aligned}
]
递归计算(f(m-1,c,c-b-1,a))即可
不难发现递归层数是(O(log n))级别的
总复杂度为(O(k^4log 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
",func(n,a,b,c)[k1][k2]);
}
return 0;
}