类欧几里得
设三个函数f(a,b,c,n)=∑i=0na×i+bc,g(a,b,c,n)=∑i=0ni×a×i+bc,h(a,b,c,n)=∑i=0n(a×i+bc)2f(a, b, c, n) = \sum\limits_{i = 0} ^{n} \frac{a \times i + b}{c}, g(a, b, c, n) = \sum\limits_{i = 0} ^{n} i \times \frac{a \times i + b}{c}, h(a, b, c, n) = \sum\limits_{i = 0} ^{n} \left(\frac{a \times i + b}{c} \right) ^ 2f(a,b,c,n)=i=0∑nca×i+b,g(a,b,c,n)=i=0∑ni×ca×i+b,h(a,b,c,n)=i=0∑n(ca×i+b)2。
f(a,b,c,n)=∑i=0n⌊a×i+bc⌋,(0≤a,b,c,n)f(a, b, c, n) = \sum\limits_{i = 0} ^{n} \lfloor \frac{a \times i + b}{c} \rfloor, (0 \le a, b, c, n)f(a,b,c,n)=i=0∑n⌊ca×i+b⌋,(0≤a,b,c,n)
下面除法均为向下取整:
b≥cb \ge cb≥c
设b′=b−bc×cb' = b - \frac{b}{c} \times cb′=b−cb×c:
f(a,b,c,n)=∑i=0na×i+b′c+bc=bc×n+∑i=0na×i+b′c=bc×n+f(a,b′,c,n)f(a, b, c, n) = \sum_{i = 0} ^{n} \frac{a \times i + b'}{c} + \frac{b}{c}\\ = \frac{b}{c} \times n + \sum_{i = 0} ^{n} \frac{a \times i + b'}{c}\\ = \frac{b}{c} \times n + f(a, b', c, n) f(a,b,c,n)=i=0∑nca×i+b′+cb=cb×n+i=0∑nca×i+b′=cb×n+f(a,b′,c,n)
a≥ca \ge ca≥c
设a′=a−ac×ca' = a - \frac{a}{c} \times ca′=a−ca×c:
f(a,b,c,n)=∑i=0na′×i+bc+ac×i=ac×n×(n+1)2+∑i=0na′×i+bc=ac×n×(n+1)2+f(a′,b,c,n)f(a, b, c, n) = \sum_{i = 0} ^{n} \frac{a' \times i + b}{c} + \frac{a}{c} \times i\\ = \frac{a}{c} \times \frac{n \times (n + 1)}{2} + \sum_{i = 0} ^{n} \frac{a' \times i + b}{c}\\ = \frac{a}{c} \times \frac{n \times (n + 1)}{2} + f(a', b, c, n)\\ f(a,b,c,n)=i=0∑nca′×i+b+ca×i=ca×2n×(n+1)+i=0∑nca′×i+b=ca×2n×(n+1)+f(a′,b,c,n)
综上,如果a≥c,b≥ca \ge c,\ b \ge ca≥c, b≥c:
f(a,b,c,n)=f(a′,b′,c,n)+bc×n+ac×n×(n+1)2f(a, b, c, n) = f(a', b', c, n) + \frac{b}{c} \times n + \frac{a}{c} \times \frac{n \times (n + 1)}{2} f(a,b,c,n)=f(a′,b′,c,n)+cb×n+ca×2n×(n+1)
考虑a<c,b<ca < c,\ b < ca<c, b<c:
∑i=0na×i+bc∑i=0n∑j=1a×i+bc1\sum_{i = 0} ^{n} \frac{a \times i + b}{c}\\ \sum_{i = 0} ^{n} \sum_{j = 1} ^{\frac{a \times i + b}{c}}1\\ i=0∑nca×i+bi=0∑nj=1∑ca×i+b1
令m=a×n+bcm = \frac{a \times n + b}{c}m=ca×n+b,
∑i=0n∑j=1m[a×i+bc≥j]∑i=0n∑j=0m−1[a×i+bc≥j+1]∑i=0n∑j=0m−1[a×i+b≥jc+c]∑i=0n∑j=0m−1[a×i≥jc+c−b]∑i=0n∑j=0m−1[a×i>jc+c−b−1]∑i=0n∑j=0m−1[i>jc+c−b−1]a∑j=0m−1∑i=0n[i>jc+c−b−1]a∑j=0m−1n−jc+c−b−1an×m−∑i=0m−1ic+c−b−1a\sum_{i = 0} ^{n} \sum_{j = 1} ^{m} [\frac{a \times i + b}{c} \ge j]\\ \sum_{i = 0} ^{n} \sum_{j = 0} ^{m - 1}[\frac{a \times i + b}{c} \ge j + 1]\\ \sum_{i = 0} ^{n} \sum_{j = 0} ^{m - 1}[a \times i + b \ge jc + c]\\ \sum_{i = 0} ^{n} \sum_{j = 0} ^{m - 1}[a \times i \ge jc + c - b]\\ \sum_{i = 0} ^{n} \sum_{j = 0} ^{m - 1}[a \times i > jc + c - b - 1]\\ \sum_{i = 0} ^{n} \sum_{j = 0} ^{m - 1}[i > \frac{jc + c - b - 1]}{a}\\ \sum_{j = 0} ^{m - 1} \sum_{i = 0} ^{n}[i > \frac{jc + c - b - 1]}{a}\\ \sum_{j = 0} ^{m - 1} n - \frac{jc + c - b - 1}{a}\\ n \times m - \sum_{i = 0} ^{m - 1} \frac{ic + c - b - 1}{a}\\ i=0∑nj=1∑m[ca×i+b≥j]i=0∑nj=0∑m−1[ca×i+b≥j+1]i=0∑nj=0∑m−1[a×i+b≥jc+c]i=0∑nj=0∑m−1[a×i≥jc+c−b]i=0∑nj=0∑m−1[a×i>jc+c−b−1]i=0∑nj=0∑m−1[i>ajc+c−b−1]j=0∑m−1i=0∑n[i>ajc+c−b−1]j=0∑m−1n−ajc+c−b−1n×m−i=0∑m−1aic+c−b−1
a′=c,b′=c−b−1,c′=aa' = c, b' = c - b - 1, c' = aa′=c,b′=c−b−1,c′=a,f(a,b,c,n)=n×m−f(a′,b′,c′,m−1)f(a, b, c, n) = n \times m - f(a', b', c', m - 1)f(a,b,c,n)=n×m−f(a′,b′,c′,m−1)。
过程中a=a%c,c=aa = a \% c, c = aa=a%c,c=a,发现跟gcd\gcdgcd的求解过程是一样的,最后aaa会变成000,也就是递归出口。
long long f(long long a, long long b, long long c, long long n) {if (!a) {return (b / c) * (n + 1);}if (a >= c || b >= c) {return f(a % c, b % c, c, n) + (b / c) * (n + 1) + (a / c) * n * (n + 1) / 2;}long long m = (a * n + b) / c;return n * m - f(c, c - b - 1, a, m - 1);
}
g(a,b,c,n)=∑i=0ni×a×i+bc,(0≤a,b,c,n)g(a, b, c, n) = \sum\limits_{i = 0} ^{n} i \times \frac{a \times i + b}{c}, (0 \le a, b, c, n)g(a,b,c,n)=i=0∑ni×ca×i+b,(0≤a,b,c,n)
b≥c,b′=b%c,d=bcb \ge c, b' = b\ \%\ c, d = \frac{b}{c}b≥c,b′=b % c,d=cb:
∑i=0ni×a×i+b′c+d×id×n×(n+1)2+∑i=0ni×a×i+b′cg(a,b,c,n)=d×n×(n+1)2+g(a,b′,c,n)\sum_{i = 0} ^{n} i \times \frac{a \times i + b'}{c} + d \times i\\ d \times \frac{n \times (n + 1)}{2} + \sum_{i = 0} ^{n} i \times \frac{a \times i + b'}{c}\\ g(a, b, c, n) = d \times \frac{n \times (n + 1)}{2} + g(a, b', c, n)\\ i=0∑ni×ca×i+b′+d×id×2n×(n+1)+i=0∑ni×ca×i+b′g(a,b,c,n)=d×2n×(n+1)+g(a,b′,c,n)
a≥c,a′=a%c,d=aca \ge c, a' = a\ \%\ c, d = \frac{a}{c}a≥c,a′=a % c,d=ca:
∑i=0ni×a′×i+bc+d×i2d×n×(n+1)×(2n+1)6+∑i=0ni×a′×i+bcd×n×(n+1)×(2n+1)6+g(a′,b,c,n)\sum_{i = 0} ^{n} i \times \frac{a' \times i + b}{c} + d \times i ^ 2\\ d \times \frac{n \times (n + 1) \times (2n + 1)}{6} + \sum_{i = 0} ^{n} i \times \frac{a' \times i + b}{c}\\ d \times \frac{n \times (n + 1) \times (2 n + 1)}{6} + g(a', b, c, n)\\ i=0∑ni×ca′×i+b+d×i2d×6n×(n+1)×(2n+1)+i=0∑ni×ca′×i+bd×6n×(n+1)×(2n+1)+g(a′,b,c,n)
综上,a≥c,b≥ca \ge c, \ b \ge ca≥c, b≥c:
g(a,b,c,n)=bc×n×(n+1)2+ac×n×(n+1)×(2n+1)6+g(a′,b′,c,n)g(a, b, c, n) = \frac{b}{c} \times \frac{n \times (n + 1)}{2} + \frac{a}{c} \times \frac{n \times (n + 1) \times (2n + 1)}{6} + g(a', b', c, n)\\ g(a,b,c,n)=cb×2n×(n+1)+ca×6n×(n+1)×(2n+1)+g(a′,b′,c,n)
a<c,b<c,m=a×n+bc,a′=c,b′=c−b−1,c′=aa < c, b < c, m = \frac{a \times n + b}{c}, a' = c, b' = c - b - 1, c'= aa<c,b<c,m=ca×n+b,a′=c,b′=c−b−1,c′=a:
∑i=0ni×∑j=1a×i+bc1∑i=0ni×∑j=1m[a×i+bc≥j]∑i=0ni×∑j=0m−1[a×i+bc≥j+1]∑i=0ni×∑j=0m−1[i>jc+c−b−1a]∑j=0m−1∑i=0ni[i>jc+c−b−1a]calc(n)=n×(n+1)2∑j=0m−1calc(n)−calc(jc+c−b−1a)m×n×(n+1)−f(a′,b′,c′,m−1)−h(a′,b′,c′,m−1)2\sum_{i = 0} ^{n} i \times \sum_{j = 1} ^{\frac{a \times i + b}{c}}1\\ \sum_{i = 0} ^{n} i \times \sum_{j = 1} ^{m} [\frac{a \times i + b}{c} \ge j]\\ \sum_{i = 0} ^{n} i \times \sum_{j = 0} ^{m - 1} [\frac{a \times i + b}{c} \ge j + 1]\\ \sum_{i = 0} ^{n} i \times \sum_{j = 0} ^{m - 1} [i > \frac{jc + c - b - 1}{a}]\\ \sum_{j = 0} ^{m - 1} \sum_{i = 0} ^{n} i [i > \frac{jc + c - b - 1}{a}]\\ calc(n) = \frac{n \times (n + 1)}{2}\\ \sum_{j = 0} ^{m - 1} calc(n) - calc(\frac{jc + c - b - 1}{a})\\ \frac{m \times n \times (n + 1) - f(a', b', c', m- 1) - h(a', b', c', m - 1)}{2}\\ i=0∑ni×j=1∑ca×i+b1i=0∑ni×j=1∑m[ca×i+b≥j]i=0∑ni×j=0∑m−1[ca×i+b≥j+1]i=0∑ni×j=0∑m−1[i>ajc+c−b−1]j=0∑m−1i=0∑ni[i>ajc+c−b−1]calc(n)=2n×(n+1)j=0∑m−1calc(n)−calc(ajc+c−b−1)2m×n×(n+1)−f(a′,b′,c′,m−1)−h(a′,b′,c′,m−1)
h(a,b,c,n)=∑i=0n(a×i+bc)2,(0≤a,b,c,n)h(a, b, c, n) = \sum\limits_{i = 0} ^{n} \left(\frac{a \times i + b}{c} \right) ^ 2, (0 \le a, b, c, n)h(a,b,c,n)=i=0∑n(ca×i+b)2,(0≤a,b,c,n)
b≥c,b′=b%c,d=bcb \ge c,b' = b \ \% \ c, d = \frac{b}{c}b≥c,b′=b % c,d=cb
∑i=0n(a×i+b′c+d)2∑i=0n(a×i+b′c)2+d2+2×d×a×i+b′cd2×(n+1)+2×d×f(a,b′,c,n)+h(a,b′,c,n)\sum_{i = 0} ^{n} \left(\frac{a \times i + b'}{c} + d \right) ^2\\ \sum_{i = 0} ^{n} \left(\frac{a \times i + b'}{c} \right) ^ 2 + d ^ 2 + 2 \times d \times \frac{a \times i + b'}{c}\\ d ^ 2 \times (n + 1) + 2 \times d \times f(a, b', c, n) + h(a, b', c, n)\\ i=0∑n(ca×i+b′+d)2i=0∑n(ca×i+b′)2+d2+2×d×ca×i+b′d2×(n+1)+2×d×f(a,b′,c,n)+h(a,b′,c,n)
a≥c,a′=a%c,d=aca \ge c, a' = a\ \%\ c, d = \frac{a}{c}a≥c,a′=a % c,d=ca
∑i=0n(d×i+a′×i+bc)2∑i=0nd2×i2+(a′×i+bc)2+2×d×i×a′×i+bcd2×n×(n+1)×(2n+1)6+h(a′,b,c,n)+2×d×g(a′,b,c,n)\sum_{i = 0} ^{n} \left(d \times i + \frac{a' \times i + b}{c} \right) ^ 2\\ \sum_{i = 0} ^{n} d ^ 2 \times i ^ 2 + \left(\frac{a' \times i + b}{c} \right) ^ 2 + 2 \times d \times i \times \frac{a' \times i + b}{c}\\ d ^ 2 \times \frac{n \times (n + 1) \times (2n + 1)}{6} + h(a', b, c, n) + 2 \times d \times g(a', b, c, n)\\ i=0∑n(d×i+ca′×i+b)2i=0∑nd2×i2+(ca′×i+b)2+2×d×i×ca′×i+bd2×6n×(n+1)×(2n+1)+h(a′,b,c,n)+2×d×g(a′,b,c,n)
综上,a≥c,b≥ca \ge c,\ b \ge ca≥c, b≥c:
h(a,b,c,n)=ac×bc×n×(n+1)+(bc)2×(n+1)+(ac)2×n×(n+1)×(2n+1)6+2×bc×f(a′,b′,c,n)+h(a′,b′,c,n)+2×ac×g(a′,b′,c,n)h(a, b, c, n) = \frac{a}{c} \times \frac{b}{c} \times n \times (n + 1) + (\frac{b}{c}) ^ 2 \times (n + 1) + (\frac{a}{c}) ^ 2 \times \frac{n \times (n + 1) \times (2n + 1)}{6}+\\ 2 \times \frac{b}{c} \times f(a', b', c, n) + h(a', b', c, n) + 2 \times \frac{a}{c} \times g(a', b', c, n)\\ h(a,b,c,n)=ca×cb×n×(n+1)+(cb)2×(n+1)+(ca)2×6n×(n+1)×(2n+1)+2×cb×f(a′,b′,c,n)+h(a′,b′,c,n)+2×ca×g(a′,b′,c,n)
a<c,b<c,m=a×n+bca < c,\ b < c, m = \frac{a \times n + b}{c}a<c, b<c,m=ca×n+b:
n2=2×n×(n+1)2−n=2×∑i=0ni−n∑i=0n(a×i+bc)2∑i=0n(2×∑j=0a×i+bcj−a×i+bc)h(a,b,c,n)=2×∑i=0n∑j=0a×i+bcj−f(a,b,c,n)∑i=0n∑j=0a×i+bcj∑j=1mj∑i=0n[a×i+bc≥j]∑j=0m−1(j+1)∑i=0n[a×i+bc≥j+1]∑j=0m−1(j+1)∑i=0n[a>jc+c−b−1a]∑j=0m−1(j+1)(n−jc+c−b−1a)m×(m+1)2×n−g(a′,b′,c′,n)−f(a′,b′,c′)h(a,b,c,n)=m×(m+1)×n−f(a,b,c,n)−2×g(a′,b′,c′,n)−2×f(a′,b′,c′,n)n ^ 2 = 2 \times \frac{n \times (n + 1)}{2} - n = 2 \times \sum_{i = 0} ^{n} i - n\\ \sum_{i = 0} ^{n} (\frac{a \times i + b}{c}) ^ 2\\ \sum_{i = 0} ^{n} \left(2 \times \sum_{j = 0} ^{\frac{a \times i + b}{c}}j - \frac{a \times i + b}{c} \right)\\ h(a, b, c, n) = 2 \times \sum_{i = 0} ^{n} \sum_{j = 0} ^{\frac{a \times i + b}{c}} j - f(a, b, c, n)\\ \sum_{i = 0} ^{n} \sum_{j = 0} ^ \frac{a \times i + b}{c}j\\ \sum_{j = 1} ^{m} j\sum_{i = 0} ^{n} [\frac{a \times i + b}{c} \ge j]\\ \sum_{j = 0} ^{m - 1} (j + 1) \sum_{i = 0} ^{n} [\frac{a \times i + b}{c} \ge j + 1]\\ \sum_{j = 0} ^{m - 1} (j + 1) \sum_{i = 0} ^{n}[a > \frac{jc + c - b - 1}{a}]\\ \sum_{j = 0} ^{m - 1} (j + 1) (n - \frac{jc + c - b - 1}{a})\\ \frac{m \times (m + 1)}{2} \times n - g(a', b', c', n) - f(a', b', c')\\ h(a, b, c, n) = m \times (m + 1) \times n - f(a, b, c, n) - 2 \times g(a', b', c', n) - 2 \times f(a', b', c', n)\\ n2=2×2n×(n+1)−n=2×i=0∑ni−ni=0∑n(ca×i+b)2i=0∑n⎝⎛2×j=0∑ca×i+bj−ca×i+b⎠⎞h(a,b,c,n)=2×i=0∑nj=0∑ca×i+bj−f(a,b,c,n)i=0∑nj=0∑ca×i+bjj=1∑mji=0∑n[ca×i+b≥j]j=0∑m−1(j+1)i=0∑n[ca×i+b≥j+1]j=0∑m−1(j+1)i=0∑n[a>ajc+c−b−1]j=0∑m−1(j+1)(n−ajc+c−b−1)2m×(m+1)×n−g(a′,b′,c′,n)−f(a′,b′,c′)h(a,b,c,n)=m×(m+1)×n−f(a,b,c,n)−2×g(a′,b′,c′,n)−2×f(a′,b′,c′,n)
P5170 【模板】类欧几里得算法
#include <bits/stdc++.h>using namespace std;const int mod = 998244353, inv2 = 499122177, inv6 = 166374059;struct Res {int f, g, h;
};inline int add(int x, int y) {return x + y < mod ? x + y : x + y - mod;
}inline int sub(int x, int y) {return x >= y ? x - y : x - y + mod;
}Res calc(int a, int b, int c, int n) {if (!a) {return {1ll * (b / c) * (n + 1) % mod, 1ll * (b / c) * n % mod * (n + 1) % mod * inv2 % mod, 1ll * (b / c) * (b / c) % mod * (n + 1) % mod};}if (a >= c || b >= c) {Res cur= calc(a % c, b % c, c, n);int f = add(add(cur.f, 1ll * (b / c) * (n + 1) % mod), 1ll * (a / c) * n % mod * (n + 1) % mod * inv2 % mod);int g = add(add(1ll * (b / c) * n % mod * (n + 1) % mod * inv2 % mod, 1ll * (a / c) * n % mod * (n + 1) % mod * (2 * n + 1) % mod * inv6 % mod), cur.g);int h = add(add(add(1ll * (a / c) * (b / c) % mod * n % mod * (n + 1) % mod, 1ll * (b / c) * (b / c) % mod * (n + 1) % mod), add(1ll * (a / c) * (a / c) % mod * n % mod * (n + 1) % mod * (2 * n + 1) % mod * inv6 % mod, 1ll * 2 * (b / c) % mod * cur.f % mod)), add(cur.h, 1ll * 2 * (a / c) * cur.g % mod));return {f, g, h};}int m = (1ll * a * n + b) / c;Res cur = calc(c, c - b - 1, a, m - 1);int f = sub(1ll * n * m % mod, cur.f);int g = 1ll * sub(sub(1ll * m * n % mod * (n + 1) % mod, cur.f), cur.h) * inv2 % mod;int h = sub(sub(sub(1ll * m * (m + 1) % mod * n % mod, f), 2ll * cur.g % mod), 2ll * cur.f % mod);return {f, g, h};
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);int T, a, b, c, n;scanf("%d", &T);while (T--) {scanf("%d %d %d %d", &n, &a, &b, &c);Res ans = calc(a, b, c, n);printf("%d %d %d\n", ans.f, ans.h, ans.g);}return 0;
}