首页 > 其他分享 >闲话 22.12.26

闲话 22.12.26

时间:2022-12-26 16:58:43浏览次数:66  
标签:lfloor 26 right frac 闲话 sum rfloor 22.12 left

闲话

下一个目标就是 LP!
我觉得在闲话里写这种东西有奇效呢(
上次也是 上上次也是
抄书会被干吗?

我不知道啊 我反正只看从网上找到的资料

模拟赛 T2 \(O(n)\)?这种东西不算不闲的闲话吧
答案肯定是 [1, x - 1] * [x, y] 或 [x, y] * [y + 1, n] 的更大值
这启发我们固定 len,在后缀链接树上合并 endpos,记录 max/min
假设 min = max 这个点只有一个 endpos
反之 min < max,我们取 [min, min + len - 1] [max, max + len - 1] 都可以作为 [x, y]
判 max - min >= len 算答案
就没了
你可能会问
就是可能目前的 len 不是最优的
可能这部分长 那部分短
但是发现 len 是连续的
所以答案最优

类欧几里得算法

【模板】类欧几里得算法

Euclidean?

我们一个一个说。

\(1.\ f(a, b, c, n)\)

\[f(a, b, c, n) = \sum_{i=0}^{n} \left\lfloor\frac{ai + b}{c}\right\rfloor \]

我们分情况讨论。

首先预处理一下。当 \(a \ge c\) 或 \(b\ge c\) 时我们能将其对 \(c\) 取模:

\[\begin{aligned} & \ f(a, b, c, n) \\ = \ & \sum_{i=0}^{n} \left\lfloor\frac{ai + b}{c}\right\rfloor \\ = \ & \sum_{i=0}^{n} \left\lfloor\frac{\left(\left\lfloor \frac{a}{c}\right\rfloor c + a\bmod c \right) i +\left(\left\lfloor \frac{b}{c}\right\rfloor c + b\bmod c \right) }{c}\right\rfloor \\ = \ & \sum_{i=0}^{n} \left\lfloor\frac{\left\lfloor \frac{a}{c}\right\rfloor c i+\left\lfloor \frac{b}{c}\right\rfloor c + (a\bmod c) i + b\bmod c }{c}\right\rfloor \\ = \ & \sum_{i=0}^{n} \left\lfloor\frac{\left\lfloor \frac{a}{c}\right\rfloor c i + \left\lfloor \frac{b}{c}\right\rfloor c}{c}\right\rfloor + \sum_{i=0}^{n} \left\lfloor\frac{(a\bmod c) i + b\bmod c }{c}\right\rfloor \\ = \ & \left\lfloor \frac{a}{c}\right\rfloor \frac{n(n + 1)} 2 + \left\lfloor \frac{b}{c}\right\rfloor (n + 1) + \sum_{i=0}^{n} \left\lfloor\frac{(a\bmod c) i + b\bmod c }{c}\right\rfloor \\ = \ & \left\lfloor \frac{a}{c}\right\rfloor \frac{n(n + 1)} 2 + \left\lfloor \frac{b}{c}\right\rfloor (n + 1) + f(a\bmod c, b\bmod c, c, n) \end{aligned}\]

然后我们可以假定 \(a, b < c\) 了。考虑转化贡献:

\[\begin{aligned} & \ f(a, b, c, n) \\ = \ & \sum_{i=0}^{n} \left\lfloor\frac{ai + b}{c}\right\rfloor \\ = \ & \sum_{i=0}^{n} \sum_{j=1}^{\left\lfloor\frac{ai + b}{c}\right\rfloor} 1 \\ = \ & \sum_{j=1}^{\left\lfloor\frac{an + b}{c}\right\rfloor} \sum_{i=0}^{n} \left[j \le \left\lfloor\frac{ai + b}{c}\right\rfloor\right ] \\ = \ & \sum_{j=1}^{\left\lfloor\frac{an + b}{c}\right\rfloor} \sum_{i=0}^{n} \left[j \le \frac{ai + b}{c} \right] \\ = \ & \sum_{j=1}^{\left\lfloor\frac{an + b}{c}\right\rfloor} \sum_{i=0}^{n} \left[j c - b - 1 < ai \right] \\ = \ & \sum_{j=1}^{\left\lfloor\frac{an + b}{c}\right\rfloor} \sum_{i=0}^{n} \left[ \left\lfloor \frac{j c - b - 1} a\right\rfloor < i \right] \\ = \ & \sum_{j=1}^{\left\lfloor\frac{an + b}{c}\right\rfloor} \left( n - \left\lfloor \frac{j c - b - 1} a\right\rfloor \right) \\ = \ & n \left\lfloor\frac{an + b}{c}\right\rfloor - \sum_{j=0}^{\left\lfloor\frac{an + b}{c}\right\rfloor- 1} \left\lfloor \frac{j c + c - b - 1} a\right\rfloor \end{aligned}\]

记 \(m = \left\lfloor\frac{an + b}{c}\right\rfloor\),我们有

\[f(a, b, c, n) = nm - f(c, c - b - 1, a, m - 1) \]

这两个式子共同的地给出了类似于辗转相除法的做法,因此复杂度为 \(O(\log n)\)。这也是该算法名称的来源。

\(2.\ g(a, b, c, n)\)

同理,有

\[\begin{aligned} & \ g(a, b, c, n) \\ = \ & \sum_{i=0}^{n}i \left\lfloor\frac{ai + b}{c} \right\rfloor \\ = \ & \sum_{i=0}^{n}i \left\lfloor\frac{\left(\left\lfloor \frac{a}{c}\right\rfloor c + a\bmod c \right) i +\left(\left\lfloor \frac{b}{c}\right\rfloor c + b\bmod c \right) }{c}\right\rfloor \\ = \ & \sum_{i=0}^{n}i \left\lfloor\frac{\left\lfloor \frac{a}{c}\right\rfloor c i+\left\lfloor \frac{b}{c}\right\rfloor c + (a\bmod c) i + b\bmod c }{c}\right\rfloor \\ = \ & \sum_{i=0}^{n}i \left\lfloor\frac{\left\lfloor \frac{a}{c}\right\rfloor c i + \left\lfloor \frac{b}{c}\right\rfloor c}{c}\right\rfloor + \sum_{i=0}^{n}i \left\lfloor\frac{(a\bmod c) i + b\bmod c }{c}\right\rfloor \\ = \ & \left\lfloor \frac{a}{c}\right\rfloor \frac{n(n + 1)(2n + 1)} 2 + \left\lfloor \frac{b}{c}\right\rfloor \frac{n(n + 1)}{2} + \sum_{i=0}^{n} i\left\lfloor\frac{(a\bmod c) i + b\bmod c }{c}\right\rfloor \\ = \ & \left\lfloor \frac{a}{c}\right\rfloor \frac{n(n + 1)(2n + 1)} 2 + \left\lfloor \frac{b}{c}\right\rfloor \frac{n(n + 1)}{2} + g(a\bmod c, b\bmod c, c, n) \end{aligned}\]

记 \(m = \left\lfloor\frac{an + b}{c}\right\rfloor\)。同理,有

\[\begin{aligned} & \ g(a, b, c, n) \\ = \ & \sum_{i=0}^{n}i \left\lfloor\frac{ai + b}{c}\right\rfloor \\ = \ & \sum_{i=0}^{n} \sum_{j=1}^{\left\lfloor\frac{ai + b}{c}\right\rfloor} i \\ = \ & \sum_{j=1}^{m} \sum_{i=0}^{n} i \left[j \le \left\lfloor\frac{ai + b}{c}\right\rfloor\right ] \\ = \ & \sum_{j=1}^{m} \sum_{i=0}^{n} i \left[j \le \frac{ai + b}{c} \right] \\ = \ & \sum_{j=1}^{m} \sum_{i=0}^{n} i \left[j c - b - 1 < ai \right] \\ = \ & \sum_{j=0}^{m - 1} \sum_{i=0}^{n} i \left[ \left\lfloor \frac{j c + c - b - 1} a\right\rfloor < i \right] \end{aligned}\]

记 \(t = \left\lfloor \frac{j c + c - b - 1} a\right\rfloor\),有

\[\begin{aligned} & \sum_{j=0}^{m - 1} \sum_{i=0}^{n} i \left[ t < i \right] \\ = \ & \sum_{j=0}^{m - 1} \frac{(n - t)(n + t + 1)} 2 \\ = \ & \frac 12 \left( m(n^2 + n) - \sum_{j=0}^{m - 1}(t^2 + t) \right) \\ = \ & \frac 12 \left( m(n^2 + n) - h(c, c - b - 1, a, m - 1) - f(c, c - b - 1, a, m - 1) \right) \end{aligned}\]

\(3.\ h(a, b, c, n)\)

同理,有

\[\begin{aligned} & \ h(a, b, c, n) \\ = \ & \sum_{i=0}^{n} \left\lfloor\frac{ai + b}{c}\right\rfloor ^2 \\ = \ & \sum_{i=0}^{n} \left\lfloor\frac{\left(\left\lfloor \frac{a}{c}\right\rfloor c + a\bmod c \right) i +\left(\left\lfloor \frac{b}{c}\right\rfloor c + b\bmod c \right) }{c}\right\rfloor ^2 \\ = \ & \sum_{i=0}^{n} \left\lfloor\frac{\left\lfloor \frac{a}{c}\right\rfloor c i+\left\lfloor \frac{b}{c}\right\rfloor c + (a\bmod c) i + b\bmod c }{c}\right\rfloor ^2 \\ = \ & \sum_{i=0}^{n} \left\lfloor\frac{\left\lfloor \frac{a}{c}\right\rfloor c i + \left\lfloor \frac{b}{c}\right\rfloor c}{c}\right\rfloor ^2 + \sum_{i=0}^{n} \left\lfloor\frac{(a\bmod c) i + b\bmod c }{c}\right\rfloor ^2 + 2\sum_{i=0}^{n} \left\lfloor\frac{\left\lfloor \frac{a}{c}\right\rfloor c i + \left\lfloor \frac{b}{c}\right\rfloor c}{c}\right\rfloor \times \left\lfloor\frac{(a\bmod c) i + b\bmod c }{c}\right\rfloor ^2 \\ = \ & \sum_{i=0}^{n} \left(\left\lfloor \frac{a}{c}\right\rfloor i + \left\lfloor \frac{b}{c}\right\rfloor\right) ^2 + \sum_{i=0}^{n} \left\lfloor\frac{(a\bmod c) i + b\bmod c }{c}\right\rfloor ^2 + 2\sum_{i=0}^{n} \left(\left\lfloor \frac{a}{c}\right\rfloor i + \left\lfloor \frac{b}{c}\right\rfloor\right) \times \left\lfloor\frac{(a\bmod c) i + b\bmod c }{c}\right\rfloor \\ = \ & \sum_{i=0}^{n} \left( \left\lfloor \frac{a}{c}\right\rfloor^2 i^2 + \left\lfloor \frac{b}{c}\right\rfloor^2 + 2\left\lfloor \frac{a}{c}\right\rfloor \left\lfloor \frac{b}{c}\right\rfloor i \right) + h(a \bmod c,b\bmod c, c, n) \\ & + 2\left\lfloor \frac{a}{c}\right\rfloor\sum_{i=0}^{n} i \left\lfloor\frac{(a\bmod c) i + b\bmod c }{c}\right\rfloor + 2\left\lfloor \frac{b}{c}\right\rfloor\sum_{i=0}^{n} \left\lfloor\frac{(a\bmod c) i + b\bmod c }{c}\right\rfloor \\ = \ & \left\lfloor \frac{a}{c}\right\rfloor^2 \frac{n(n + 1)(2n+1)} 2 + \left\lfloor \frac{b}{c}\right\rfloor^2(n + 1) + \left\lfloor \frac{a}{c}\right\rfloor \left\lfloor \frac{b}{c}\right\rfloor n(n + 1) + h(a \bmod c,b\bmod c, c, n)\\ & + 2\left\lfloor \frac{a}{c}\right\rfloor g(a \bmod n, b\bmod n, c, n) + 2\left\lfloor \frac{b}{c}\right\rfloor f(a \bmod n, b\bmod n, c, n) \end{aligned}\]

记 \(m = \left\lfloor\frac{an + b}{c}\right\rfloor, t = \left\lfloor \frac{j c + c - b - 1} a\right\rfloor\)。

我们不想出现两个求和号相乘,因此拆开 \(n^2\) 为 \(2n(n + 1) / 2 - n\)。

\[\begin{aligned} & \ f(a, b, c, n) \\ = \ & \sum_{i=0}^{n} \left\lfloor\frac{ai + b}{c}\right\rfloor ^2 \\ = \ & \sum_{i=0}^{n} \left( 2\sum_{j=1}^{\left\lfloor\frac{ai + b}{c}\right\rfloor} j - \left\lfloor\frac{ai + b}{c}\right\rfloor \right) \\ = \ & 2\sum_{i=0}^{n}\sum_{j=1}^{\left\lfloor\frac{ai + b}{c}\right\rfloor} j - f(a, b, c, n) \end{aligned}\]

\[\begin{aligned} & \sum_{i=0}^{n}\sum_{j=1}^{\left\lfloor\frac{ai + b}{c}\right\rfloor} j \\ = \ & \sum_{j=0}^{m - 1} (j + 1) \sum_{i=0}^{n} [j < \left\lfloor \frac{ai+b}{c}\right\rfloor] \\ = \ & \sum_{j=0}^{m - 1} (j + 1) \sum_{i=0}^{n} [i > t] \\ = \ & \sum_{j=0}^{m - 1} (j + 1) (n - t) \\ = \ & n\frac{m(m + 1)}2 - \sum_{j=0}^{m - 1} (j + 1)t \\ = \ & n\frac{m(m + 1)}2 - \sum_{j=0}^{m - 1} jt - \sum_{j=0}^{m - 1} t \\ = \ & n\frac{m(m + 1)}2 - g(c, c - b - 1, a, m - 1) - f(c, c - b - 1, a, m - 1) \end{aligned}\]

\[h(a, b, c, n) = nm(m + 1) - 2g(c, c - b - 1, a, m - 1) - 2f(c, c - b - 1, a, m - 1) - f(a, b, c, n) \]

我们可以将三个函数合并递归,这样减小了常数。

根据辗转相除法,总时间复杂度为 \(O(\log n)\)。

合并写法
int T, n, a, b, c;
struct dat {
	mint f, g, h;
};

dat f(int n, int a, int b, int c) {
	mint ac = a / c, bc = b / c, m = (1ll * a * n + b) / c, n1 = n + 1, n21 = 2 * n + 1;
	dat ret;
	if (a == 0) {
		ret.f = bc * n1, ret.g = bc * n * n1 * inv2, ret.h = bc * bc * n1;
		return ret;
	}
	if (a >= c or b >= c) {
		ret.f = n * n1 * inv2 * ac + bc * n1;
		ret.g = ac * n * n1 * n21 * inv6 + bc * n * n1 * inv2;
		ret.h = ac * ac * n * n1 * n21 * inv6 + 
				bc * bc * n1 + ac * bc * n * n1;

		auto v = f(n, a % c, b % c, c);

		ret.h += v.h + 2 * bc * v.f + 2 * ac * v.g;
		ret.g += v.g;
		ret.f += v.f;
		return ret;
	} 
	auto v = f((1ll * a * n + b) / c - 1, c, c - b - 1, a);
	ret.f = n * m - v.f;
	ret.g = inv2 * (n * m * n1 - v.h - v.f);
	ret.h = n * m * (m + 1) - 2 * v.g - 2 * v.f - ret.f;
	return ret;
}

signed main() {
	iot;
	cin >> T;
	while ( T -- ) {
		cin >> n >> a >> b >> c;
		auto v = f(n, a, b, c);
		cout << v.f << ' ' << v.h << ' ' << v.g << '\n';
	}
}

标签:lfloor,26,right,frac,闲话,sum,rfloor,22.12,left
From: https://www.cnblogs.com/joke3579/p/chitchat221226.html

相关文章