[P5170] 类欧几里得算法
Posted nosta
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[P5170] 类欧几里得算法相关的知识,希望对你有一定的参考价值。
のすたの“类欧几里得算法”第二题 P5170
【题意】已知(n,a,b,c),求
[
egin{aligned}
f_{1}(a,b,c,n)&=sum_{i=0}^nlfloordfrac{ai+b}{c}
floorf_{2}(a,b,c,n)&=sum_{i=0}^nlfloordfrac{ai+b}{c}
floor^2f_{3}(a,b,c,n)&=sum_{i=0}^nlfloordfrac{ai+b}{c}
floor*i\end{aligned}
]
【预备】
设(m=lfloordfrac{a imes n+b}{c} floor, t_{1}=lfloordfrac{a}{c} floor, t_{2}=lfloordfrac{b}{c} floor)。
定义([ ext{expression}])为真值表达式。
简单的引理,当(a,b,cin Z?)时
- (alelfloordfrac{b}{c} floor Rightarrow acle b ?)。
- (a< bc Rightarrow t_{1}<b)。
【限界】a=0时直接计算。
【式一】对原式变形
[
f_{1}(a,b,c,n)
=sum_{i=0}^nt_{1} imes i+t_{2}+lfloordfrac{(amod c) imes i+(bmod c)}{c}
floor=t_{1} imesdfrac{n(n+1)}{2}+t_{2} imes(n+1)+ f_{1}(amod c,bmod c,c,n)
]
当(t1=t2=0)即(a<c)且(b<c)时,
[
f_{1}(a,b,c,n)=
sum_{i=0}^nsum_{j=1}^m [jledfrac{ai+b}{c}]
=sum_{i=0}^nsum_{j=1}^m [jlelfloordfrac{ai+b}{c}
floor]=sum_{j=1}^msum_{i=0}^n[jlelfloordfrac{ai+b}{c}
floor]
=sum_{j=1}^msum_{i=0}^n[cj-ble ai]
=sum_{j=1}^msum_{i=0}^n[cj-b-1< ai]=sum_{j=1}^msum_{i=0}^n[lfloordfrac{cj-b-1}{a}
floor< i]
=sum_{j=1}^m(n-lfloordfrac{cj-b-1}{a}
floor)=mn-sum_{i=1}^mlfloordfrac{ci-b-1}{a}
floor
=mn-sum_{i=0}^{m-1}lfloordfrac{ci+c-b-1}{a}
floor=mn-f_{1}(c,c-b-1,a,m-1)
]
【式二】对原式变形
[
f_{2}(a,b,c,n)
=sum_{i=0}^n(t_{1} imes i+t_{2}+lfloordfrac{(amod c) imes i+(bmod c)}{c}
floor)^2=sum_{i=0}^n egin{cases}
(t_{1} imes i)^2+t_{2}^2+lfloordfrac{(amod c) imes i+(bmod c)}{c}
floor^2 +2t_{1}t_{2}*i +2t_{1}ilfloordfrac{(amod c) imes i+(bmod c)}{c}
floor +2t_{2}lfloordfrac{(amod c) imes i+(bmod c)}{c}
floor
end{cases}=egin{cases}
t_{1}^2sum_{i=0}^ni^2+t_{2}^2*(n+1)+f_{2}(amod c,bmod c,c,n) +2t_{1}t_{2}sum_{i=0}^n i +2t_{1}f_{3}(amod c,b mod c,c,n) +2t_{2}f_{1}(amod c,b mod c,c,n)
end{cases}\]
当(t1=t2=0)即(a<c)且(b<c?)时,
[
f_{2}(a,b,c,d)=
sum_{i=0}^nlfloordfrac{ai+b}{c}
floor
=sum_{i=0}^nsum_{j=1}^msum_{k=1}^m[lfloordfrac{cj-b-1}{a}
floor< i ext{ and }lfloordfrac{ck-b-1}{a}
floor< i]=sum_{i=0}^nsum_{j=1}^msum_{k=1}^m [max(lfloordfrac{cj-b-1}{a}
floor,lfloordfrac{ck-b-1}{a}
floor)< i]=sum_{j=1}^msum_{k=1}^m sum_{i=0}^n[max(lfloordfrac{cj-b-1}{a}
floor,lfloordfrac{ck-b-1}{a}
floor)< i]=sum_{j=1}^msum_{k=1}^m n-max(lfloordfrac{cj-b-1}{a}
floor,lfloordfrac{ck-b-1}{a}
floor)=nm^2-sum_{j=1}^msum_{k=1}^mmax(lfloordfrac{cj-b-1}{a}
floor,lfloordfrac{ck-b-1}{a}
floor)=nm^2-2*sum_{j=1}^mlfloordfrac{cj-b-1}{a}
floor*(j-1)-sum_{j=1}^m lfloordfrac{cj-b-1}{a}
floor=nm^2-sum_{j=0}^{m-1} lfloordfrac{cj+c-b-1}{a}
floor*j-sum_{j=0}^{m-1} lfloordfrac{cj+c-b-1}{a}
floor=nm^2-f_{1}(c,c-b-1,a,m-1)-2*f_{3}(c,c-b-1,a,m-1)
]
【式三】对原式变形
[
f_{3}(a,b,c,n)=sum_{i=0}^nlfloordfrac{ai+b}{c}
floor*i
=sum_{i=0}^n (t_{1} imes i+t_{2}+lfloordfrac{(amod c) imes i+(bmod c)}{c}
floor)*i=sum_{i=0}^n t_{1} imes i^2+t_{2} imes i+lfloordfrac{(amod c) imes i+(bmod c)}{c}
floor imes i=t_{1}sum_{i=0}^ni^2+t_{2}sum_{i=0}^ni+f_{3}(amod c,bmod c,c,n)
]
当(t1=t2=0)即(a<c)且(b<c)时,定义(p(j)=lfloordfrac{cj-b-1}{a}
floor)。
[
f3(a,b,c,d)
=sum_{i=0}^nsum_{j=1}^m [jlelfloordfrac{ai+b}{c}
floor]*i
=sum_{j=1}^msum_{i=0}^n[lfloordfrac{cj-b-1}{a}
floor< i]*i=sum_{j=1}^msum_{i=p(j)+1}^ni
=sum_{j=1}^m dfrac{1}{2}(p(j)+1+n)(n-p(j))=sum_{j=1}^m dfrac{1}{2}(n imes p(j)-p^2(j)+n-p(j)+n^2-n imes p(j))=sum_{j=1}^m dfrac{1}{2}(-p^2(j)+n-p(j)+n^2)=dfrac{-f_{2}(c,c-b-1,a,m-1)-f_{1}(c,c-b-1,a,m-1)+nm+n^2m}{2}
]
【时间复杂度】如果每个都单独搜索的话,大概因该会炸吧。。考虑到三个函数的递归模式都很**,干脆用一个结构体存下三个值。再参考第一题的分析,状态数目是(log)级别的。
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const LL mod=998244353;
const LL I2=499122177;
const LL I6=166374059;
inline LL s1(LL n) {return I2*n%mod*(n+1)%mod;}
inline LL s2(LL n) {return I6*n%mod*(n+1)%mod*(n+n+1)%mod;}
struct node {
LL f1,f2,f3;
node(LL f1=0,LL f2=0,LL f3=0):f1(f1),f2(f2),f3(f3){
// assert(0<=f1 && 0<=f2 && 0<=f3);
// assert(f1<mod && f2<mod && f3<mod);
}
};
node dfs(LL a,LL b,LL c,LL n) {
if(!a||!n) return node(
(b/c)*(n+1)%mod,
(b/c)*(b/c)%mod*(n+1)%mod,
(b/c)*s1(n)%mod
);
if(a>=c || b>=c) {
LL t1=a/c, t2=b/c;
node tmp=dfs(a%c,b%c,c,n);
return node(
(t1*s1(n)%mod+t2*(n+1)%mod+tmp.f1)%mod,
(t1*t1%mod*s2(n)%mod
+t2*t2%mod*(n+1)%mod
+tmp.f2
+2*t1%mod*t2%mod*s1(n)%mod
+2*t1%mod*tmp.f3%mod
+2*t2%mod*tmp.f1%mod
)%mod,
(t1*s2(n)%mod+t2*s1(n)%mod+tmp.f3)%mod
);
} else {
LL m=(a*n+b)/c;
node tmp=dfs(c,c-b-1,a,m-1);
return node(
(n*m%mod-tmp.f1+mod)%mod,
(n*m%mod*m%mod-tmp.f1-2*tmp.f3%mod+mod+mod)%mod,
(n*m%mod+n*n%mod*m%mod-tmp.f1-tmp.f2+mod+mod)%mod*I2%mod
);
}
}
int main() {
int T,a,b,c,n;
scanf("%d",&T);
while(T--) {
scanf("%d%d%d%d",&n,&a,&b,&c);
node tmp=dfs(a,b,c,n);
printf("%lld %lld %lld
",tmp.f1,tmp.f2,tmp.f3) ;
}
return 0;
}
以上是关于[P5170] 类欧几里得算法的主要内容,如果未能解决你的问题,请参考以下文章