K. Easy Sigma
∑i=1n(−1)⌊i×k⌋,(n≤109,k≤104)\sum_{i = 1} ^{n} (-1) ^{\lfloor i \times \sqrt k \rfloor}, (n \le 10 ^ 9, k \le 10 ^ 4)\\ i=1∑n(−1)⌊i×k⌋,(n≤109,k≤104)
考虑(−1)x=1−2×(xmod2)=1−2(x−2×x2)=1−2x+4×⌊x2⌋(-1) ^{x} = 1 - 2 \times (x \mod 2) = 1 - 2(x - 2 \times \frac{x}{2}) = 1 - 2x + 4 \times \lfloor \frac{x}{2} \rfloor(−1)x=1−2×(xmod2)=1−2(x−2×2x)=1−2x+4×⌊2x⌋。
∑i=1n(−1)⌊i×k⌋n−2×∑i=1n⌊i×k⌋+4×∑i=1n⌊i×k2⌋\sum_{i = 1} ^{n} (-1) ^{\lfloor i \times \sqrt{k} \rfloor}\\ n - 2 \times \sum_{i = 1} ^{n} \lfloor i \times \sqrt k \rfloor + 4 \times \sum_{i = 1} ^{n} \lfloor \frac{ i \times \sqrt k}{2} \rfloor\\ i=1∑n(−1)⌊i×k⌋n−2×i=1∑n⌊i×k⌋+4×i=1∑n⌊2i×k⌋
我么假设f(a,b,c,n)=∑i=1n⌊(a×r+b)×ic⌋f(a, b, c, n) = \sum\limits_{i = 1} ^{n} \lfloor \frac{\left(a \times \sqrt r + b \right) \times i}{c} \rfloorf(a,b,c,n)=i=1∑n⌊c(a×r+b)×i⌋,
如果a×r+bc≥1,d=⌊a×r+bc⌋\frac{a \times \sqrt r + b} {c} \ge 1, d = \lfloor \frac{a \times \sqrt r + b} {c} \rfloorca×r+b≥1,d=⌊ca×r+b⌋:
d×n×(n+1)2+∑i=1n⌊(a×r+b−d×c)×ic⌋f(a,b,c,n)=d×n×(n+1)2+f(a,b−d×c,c,n)d \times \frac{n \times (n + 1)}{2} + \sum_{i = 1} ^{n} \lfloor \frac{\left(a \times \sqrt r + b - d \times c \right) \times i}{c} \rfloor \\ f(a, b, c, n) = d \times \frac{n \times (n + 1)}{2} + f(a, b - d \times c, c, n)\\ d×2n×(n+1)+i=1∑n⌊c(a×r+b−d×c)×i⌋f(a,b,c,n)=d×2n×(n+1)+f(a,b−d×c,c,n)
否则a×r+bc<1\frac{a \times \sqrt r + b}{c} < 1ca×r+b<1:
由类欧的一般套路,我们设m=⌊n×ar+bc⌋,t=a×r+bcm = \lfloor n \times \frac{a \sqrt r + b}{c} \rfloor, t = \frac{a \times \sqrt r + b}{c}m=⌊n×car+b⌋,t=ca×r+b,这里的ddd没有向下取整,是一个实数。
∑i=1n∑j=1m[j<t×i]∑i=1n∑j=1m[i>jt]∑j=1mn−⌊jt⌋∑i=1mn−⌊i×ca×r+b⌋n×m−∑i=1m⌊i×ca×r+b⌋\sum_{i = 1} ^{n} \sum_{j = 1} ^{m} [j < t \times i]\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} [i > \frac{j}{t}]\\ \sum_{j = 1} ^{m} n - \lfloor \frac{j}{t} \rfloor \\ \sum_{i = 1} ^{m} n - \lfloor \frac{i \times c}{a \times \sqrt r + b} \rfloor\\ n \times m - \sum_{i = 1} ^{m} \lfloor \frac{i \times c}{a \times \sqrt r + b} \rfloor \\ i=1∑nj=1∑m[j<t×i]i=1∑nj=1∑m[i>tj]j=1∑mn−⌊tj⌋i=1∑mn−⌊a×r+bi×c⌋n×m−i=1∑m⌊a×r+bi×c⌋
考虑将分母有理化,也就是乘上一个a×r−ba \times \sqrt r - ba×r−b。
n×m−∑i=1m⌊car−cba2r−b2i⌋n \times m - \sum_{i = 1} ^{m} \lfloor \frac{ca \sqrt r - cb}{a ^ 2 r - b ^ 2} i \rfloor\\ n×m−i=1∑m⌊a2r−b2car−cbi⌋
综上,可以通过类欧来解决,递归出口就是n=0n = 0n=0。
#include <bits/stdc++.h>using namespace std;double x;int r;long long f(long long a, long long b, long long c, long long n) {if (!n) {return 0;}long long t = (a * x + b) / c;if (t) {return n * (n + 1) / 2 * t + f(a, b - t * c, c, n);}long long A = c * a, B = -c * b, C = a * a * r - b * b;long long d = __gcd(A, __gcd(B, C)), m = n * (a * x + b) / c;A /= d, B /= d, C /= d;return n * m - f(A, B, C, m);
}int main() {// freopen("in.txt", "r", stdin);// freopen("ou.txt", "w", stdout);int T, n;scanf("%d", &T);while (T--) {scanf("%d %d", &n, &r);x = sqrt(r);int s = sqrt(r);if (s * s == r) {if (s % 2 == 0) {printf("%d\n", n);}else if (n % 2 == 0) {puts("0");}else {puts("-1");}}else {printf("%lld\n", n - 2 * f(1, 0, 1, n) + 4 * f(1, 0, 2, n));}}return 0;
}