三个式子
$f(a,b,c,n)=\sum_{i=0}^{n}\lfloor \frac{ai+b}{c} \rfloor$
$g(a,b,c,n)=\sum_{i=0}^{n}\lfloor \frac{ai+b}{c} \rfloor^2$
$h(a,b,c,n)=\sum_{i=0}^{n}i\lfloor \frac{ai+b}{c} \rfloor$
它的几何意义也比较明显
$a=0$
$f(a,b,c,n)=(n+1)\times \lfloor \frac{b}{c} \rfloor$
$a>c$或者$b>c$
$f(a,b,c,n)=\sum_{i=0}^{n}\lfloor \frac{ai+b}{c} \rfloor$
$f(a,b,c,n)=\sum_{i=0}^{n}(\lfloor \frac{(a\mod c)i+(b\mod c)}{c} \rfloor +i\lfloor \frac{a}{c} \rfloor+\lfloor\frac{b}{c} \rfloor)$
$f(a,b,c,n)=\sum_{i=0}^{n}(\lfloor \frac{(a\mod c)i+(b\mod c)}{c} \rfloor )+\frac{n\times(n+1)}{2}\lfloor \frac{a}{c} \rfloor+(n+1) \times\lfloor \frac{b}{c} \rfloor$
$f(a,b,c,n)=f(a \mod \ c,b \mod \ c,c,n)+\frac{n\times(n+1)}{2}\lfloor \frac{a}{c} \rfloor+(n+1) \times\lfloor \frac{b}{c} \rfloor$
当$a<c$且$b<c$时
设$M=\lfloor \frac{an+b}{c}\rfloor$
$f(a,b,c,n)=\sum_{i=0}^{n}\lfloor \frac{ai+b}{c} \rfloor$
$f(a,b,c,n)=\sum_{i=0}^{n}\sum_{j=0}^{M}[j<=\lfloor \frac{ai+b}{c} \rfloor]$
$f(a,b,c,n)=\sum_{i=0}^{n}\sum_{j=0}^{M-1}[jc+c<ai+b+1]$
$f(a,b,c,n)=\sum_{i=0}^{n}\sum_{j=0}^{M-1}[jc+c<ai+b+1]$
$f(a,b,c,n)=\sum_{j=0}^{M-1}\sum_{i=0}^{n}[jc+c-b-1<ai]$
$f(a,b,c,n)=\sum_{j=0}^{M-1}\sum_{i=0}^{n}[\lfloor\frac{jc+c-b-1}{a}\rfloor<i]$
$f(a,b,c,n)=\sum_{j=0}^{M-1}n-\lfloor\frac{jc+c-b-1}{a}\rfloor$
$f(a,b,c,n)=nM-f(c,c-b-1,a,M-1)$
然后$f$推导完毕
$g,h:$
$g(a,b,c,n)=\sum_{i=0}^{n}\lfloor \frac{ai+b}{c} \rfloor^2$
$h(a,b,c,n)=\sum_{i=0}^{n}i\lfloor \frac{ai+b}{c} \rfloor$
$a=0$
$g(a,b,c,n)=(n+1)\times\lfloor\frac{b}{c}\rfloor^2$
$h(a,b,c,n)=\frac{n\times(n+1)}{2}\times\frac{b}{c}$
$a>c$或$b>c$
$g(a,b,c,n)=\sum_{i=0}^{n} (\lfloor\frac{(a\mod c)i+(b\mod c)}{c} \rfloor+i\lfloor\frac{a}{c}\rfloor+\lfloor\frac{b}{c}\rfloor)^2$
$g(a,b,c,n)=\sum_{i=0}^{n} (\lfloor\frac{(a\mod c)i+(b\mod c)}{c} \rfloor+(i\lfloor\frac{a}{c}\rfloor+\lfloor\frac{b}{c}\rfloor))^2$
$g(a,b,c,n)=\sum_{i=0}^{n} (\lfloor\frac{(a\mod c)i+(b\mod c)}{c} \rfloor)^2+(i\lfloor\frac{a}{c}\rfloor+\lfloor\frac{b}{c}\rfloor)^2+2\times(\lfloor\frac{(a\mod c)i+(b\mod c)}{c} \rfloor)\times(i\lfloor\frac{a}{c}\rfloor+\lfloor\frac{b}{c}\rfloor)$
$g(a,b,c,n)=g(a\mod c,b\mod c,c,n)+2\times \lfloor\frac{a}{c}\rfloor h(a\mod c,b\mod c,c,n)+2\times \lfloor\frac{b}{c}\rfloor f(a\mod c,b\mod c,c,n)+\sum_{i=0}^{n}(\lfloor \frac{a}{c}\rfloor^2i^2+\lfloor\frac{b}{c}\rfloor^2+2\times\lfloor\frac{a}{c}\rfloor\lfloor\frac{b}{c}\rfloor i)$
$g(a,b,c,n)=g(a\mod c,b\mod c,c,n)+2\times \lfloor\frac{a}{c}\rfloor h(a\mod c,b\mod c,c,n)+2\times \lfloor\frac{b}{c}\rfloor f(a\mod c,b\mod c,c,n)+\lfloor\frac{a}{c}\rfloor^2\frac{n\times (n+1)\times(2\times n+1)}{6}+\frac{n\times(n+1)}{1}\lfloor\frac{a}{c}\rfloor\lfloor\frac{b}{c}\rfloor+(n+1)\lfloor \frac{b}{c}\rfloor^2$
写$Latex$好累$......$
$h(a,b,c,n)=\sum_{i=0}^{n}(i\lfloor \frac{(a\mod c)i+(b\mod c)}{c}\rfloor+i^2 \lfloor\frac{a}{c}\rfloor+i\lfloor\frac{b}{c}\rfloor)$
$h(a,b,c,n)=\sum_{i=0}^{n}(i\lfloor \frac{(a\mod c)i+(b\mod c)}{c}\rfloor)+\frac{n\times (n+1)\times(2\times n+1)}{6}\lfloor\frac{a}{c}\rfloor+\frac{n\times(n+1)}{2}\lfloor\frac{b}{c}\rfloor$
$h(a,b,c,n)=h(a\mod c,b\mod c,c,n)+\frac{n\times (n+1)\times(2\times n+1)}{6}\lfloor\frac{a}{c}\rfloor+\frac{n\times(n+1)}{2}\lfloor\frac{b}{c}\rfloor$
当$a<c$且$b<c$时
设$M=\lfloor\frac{an+b}{c}\rfloor$
$g(a,b,c,n)=\sum_{i=0}^n\lfloor\frac{ai+b}{c}\rfloor^2$
$n^2=2\frac{n\times(n+1)}{2}-n$
$n^2=(2\sum_{i=0}^{n}i)-n$
$g(a,b,c,n)=\sum_{i=0}^{n}(2\sum_{j=1}^{\frac{ai+b}{c}}j-\frac{ai+b}{c})$
$g(a,b,c,n)=2\sum_{i=0}^{n}\sum_{j=1}^{\frac{ai+b}{c}}j-n\frac{ai+b}{c}$
$g(a,b,c,n)=-f(a,b,c,n)+2\sum_{i=0}^{n}\sum_{j=1}^{M}j[j<=\lfloor\frac{ai+b}{c}\rfloor]$
$g(a,b,c,n)=-f(a,b,c,n)+2\sum_{i=0}^{n}\sum_{j=0}^{M-1}(j+1)[jc+c<=ai+b+1]$
$g(a,b,c,n)=-f(a,b,c,n)+2\sum_{j=0}^{M-1}(j+1)\sum_{i=0}^{n}[jc+c<=ai+b+1]$
$g(a,b,c,n)=-f(a,b,cn)+2\sum_{j=0}^{M-1}(j+1)\sum_{i=0}^{n}[i>\lfloor \frac{jc+c-b-1}{a}\rfloor]$
$g(a,b,c,n)=-f(a,b,c,n)+2\sum_{j=0}^{M-1}(j+1)(n-\lfloor \frac{jc+c-b-1}{a}\rfloor)$
$g(a,b,c,n)=-f(a,b,c,n)+2\sum_{j=0}^{M-1}(j+1)n-(j+1)\lfloor \frac{jc+c-b-1}{a}\rfloor$
$g(a,b,c,n)=-f(a,b,c,n)+nM(M+1)-\sum_{j=0}^{M-1}(j+1)\lfloor \frac{jc+c-b-1}{a}\rfloor$
$g(a,b,c,n)=-f(a,b,c,n)+nM(M+1)-\sum_{j=0}^{M-1}j\lfloor \frac{jc+c-b-1}{a}\rfloor-\sum_{j=0}^{M-1}\lfloor \frac{jc+c-b-1}{a}\rfloor$
$g(a,b,c,n)=nM(M+1)-f(a,b,c,n)-2h(c,c-b-1,a,M-1)-2f(c,c-b-1,a,M-1)$
$h(a,b,c,n)=\sum_{i=0}^{n}i\lfloor\frac{ai+b}{c}\rfloor$
$h(a,b,c,n)=\sum_{i=0}^{n}i\sum_{j=1}^{M}[j<=\lfloor\frac{ai+b}{c}\rfloor]$
$h(a,b,c,n)=\sum_{j=0}^{M-1}\sum_{i=0}^{N}i[i>\lfloor \frac{jc+c-b-1}{a}\rfloor]$
$h(a,b,c,n)=\sum_{j=0}^{M-1}(\frac{n\times(n+1)}{2}-\sum_{i=0}^{n}i[i<=\lfloor\frac{jc+c-b-1}{a}\rfloor])$
$h(a,b,c,n)=\sum_{j=0}^{M-1}(\frac{n\times(n+1)}{2}-\frac{\lfloor\frac{jc+c-b-1}{a}\rfloor\times(\lfloor\frac{jc+c-b-1}{a}\rfloor+1)}{2})$
$h(a,b,c,n)=\frac{1}{2}[Mn(n+1)-g(c,c-b-1,a,M-1)-f(c,c-b-1,a,M-1)]$
#include<bits/stdc++.h> #define int long long #define mod 998244353 using namespace std; int inv2,inv6; struct node { int f,g,h; }; int my_pow(int a,int b) { int res=1; while(b) { if(b&1) { res=(res*a)%mod; } a=(a*a)%mod; b>>=1; } return res; } void Init() { inv2=my_pow(2,mod-2); inv6=my_pow(6,mod-2); } node sol(int a,int b,int c,int n) { int m=(a*n+b)/c,ac=a/c,bc=b/c,n1=n+1,n2=n*2+1; node Mid; if(a==0) { Mid.f=n1*bc%mod; Mid.g=n1*bc%mod*bc%mod; Mid.h=n*n1%mod*inv2%mod*bc%mod; } else if(a>=c||b>=c) { Mid.f=n*n1%mod*inv2%mod*ac%mod+n1*bc%mod; Mid.f%=mod; Mid.g=ac*ac%mod*n%mod*n1%mod*n2%mod*inv6%mod+n*n1%mod*ac%mod*bc%mod+n1*bc%mod*bc%mod; Mid.g%mod; Mid.h=n*n1%mod*n2%mod*inv6%mod*ac%mod+n*n1%mod*inv2%mod*bc%mod; Mid.h%=mod; node nxt=sol(a%c,b%c,c,n); Mid.f=(Mid.f+nxt.f)%mod; Mid.g=(Mid.g+nxt.g+2*ac%mod*nxt.h%mod+2*bc%mod*nxt.f%mod)%mod; Mid.h=(Mid.h+nxt.h)%mod; } else { Mid.f=n*m%mod; Mid.g=n*m%mod*(m+1)%mod; Mid.h=inv2*m%mod*n%mod*n1%mod; node nxt=sol(c,c-b-1,a,m-1); Mid.f=(Mid.f+mod-nxt.f)%mod; Mid.g=(Mid.g+mod-Mid.f+mod-2*nxt.h%mod+mod-2*nxt.f%mod)%mod; Mid.h=(Mid.h+mod-inv2*nxt.g%mod+mod-inv2*nxt.f%mod)%mod; } return Mid; } int T,n,a,b,c; signed main() { scanf("%lld",&T); Init(); while(T--) { scanf("%lld%lld%lld%lld",&n,&a,&b,&c); node Ans=sol(a,b,c,n); printf("%lld %lld %lld\n",Ans.f,Ans.g,Ans.h); } }