推柿子大赛了属于是。

题目要求三个柿子,不妨分别记为:

\[\begin {align}
f (a, b, c, n) &= \sum \limits _{i = 0} ^{n} \lfloor \frac {ai + b} {c} \rfloor
\nonumber \\
g (a, b, c, n) &= \sum \limits _{i = 0} ^{n} \lfloor \frac {ai + b} {c} \rfloor ^2
\nonumber \\
h (a, b, c, n) &= \sum \limits _{i = 0} ^{n} i\lfloor \frac {ai + b} {c} \rfloor
\nonumber \end {align}
\]

分开化简即可。


Case #1 F.

分类讨论:

  • 若 \(a = 0\),则 \(f (a, b, c, n) = (n + 1) \lfloor \frac {b} {c} \rfloor\)

  • 若 \(a \geq c\) 或 \(b \geq c\),则:

    \[\begin {align}
    f (a, b, c, n) &= \sum \limits _{i = 0} ^{n} (\lfloor \frac {i(a \bmod c) + (b \bmod c)} {c} \rfloor + i \lfloor \frac {a} {c} \rfloor + \lfloor \frac {b} {c} \rfloor)
    \nonumber \\
    &= f (a \bmod c, b \bmod c, c, n) + \frac {n (n + 1)} {2} \lfloor \frac {a} {c} \rfloor + (n + 1) \lfloor \frac {b} {c} \rfloor
    \nonumber \end {align}
    \]
  • 若 \(a < c\) 且 \(b < c\),记 \(m = \lfloor \frac {an + b} {c} \rfloor\),则有:

    \[\begin {align}
    f (a, b, c, n) &= \sum \limits _{i = 0} ^{n} \sum \limits _{j = 1} ^{m} [j \leq \lfloor \frac {ai + b} {c} \rfloor]
    \nonumber \\
    &= \sum \limits _{i = 0} ^{n} \sum \limits _{j = 0} ^{m - 1} [j c + c < ai + b + 1]
    \nonumber \\
    &= \sum \limits _{j = 0} ^{m - 1} \sum \limits _{i = 0} ^{n} [i > \lfloor \frac {jc + c - b - 1} {a} \rfloor]
    \nonumber \\
    &= \sum \limits _{j = 0} ^{m - 1} (n - \lfloor \frac {jc + c - b - 1} {a} \rfloor)
    \nonumber \\
    &= nm - f(c, c - b - 1, a, m - 1)
    \nonumber \end {align}
    \]

接下来递归求解。不难发现,递归出口为 \(a = 0\)。

在其他递归过程中,若只关注 \(a\),\(c\) 两项,可以发现我们是在交错执行以下两步:交换 $a $ 和 \(c\)、将 \(a\) 对 \(c\) 取模。这与欧几里得算法类似,故具有和欧几里得算法一样的 \(O(\log )\) 时间复杂度。


Case #2 G & H.

把它们放在一起是因为求法互相依赖,巨大多公式警告。

继续分类讨论。

  • 若 \(a = 0\),则:

    \[\begin {align}
    g (a, b, c, n) &= (n + 1) \lfloor \frac {b} {c} \rfloor ^ 2
    \nonumber \\
    h(a, b, c, n) &= \frac {n(n + 1)} {2} \lfloor \frac {b} {c} \rfloor
    \nonumber \end {align}
    \]
  • 若 \(a \geq c\) 或 \(b \geq c\),则:

    \[\begin {align}
    g (a, b, c, n) &= \sum \limits _{i = 0} ^{n} (\lfloor \frac {i (a \bmod c) + (b \bmod c)} {c} \rfloor + i \lfloor \frac {a} {c} \rfloor + \lfloor \frac {b} {c} \rfloor) ^2
    \nonumber \\
    &= \sum \limits _{i = 0} ^{n} \lfloor \frac {i (a \bmod c) + (b \bmod c)} {c} \rfloor) ^2 + 2 (i\lfloor \frac {a} {c} \rfloor + \lfloor \frac {b} {c} \rfloor)\lfloor \frac {i (a \bmod c) + (b \bmod c)} {c} \rfloor + (i\lfloor \frac {a} {c} \rfloor + \lfloor \frac {b} {c} \rfloor)^2
    \nonumber \\
    &= g(a \bmod c, b \bmod c, c, n) + 2 \lfloor \frac {a} {c} \rfloor h (a\bmod c, b \bmod c, c, n) + 2 \lfloor \frac {b} {c} \rfloor f (a\bmod c, b \bmod c, c, n)
    \nonumber \\
    &+ \sum \limits _{i = 0} ^{n} (\lfloor \frac {a} {c} \rfloor ^2 i^2 + 2 \lfloor \frac {a} {c} \rfloor \lfloor \frac {b} {c} \rfloor i + \lfloor \frac {b} {c} \rfloor ^2)
    \nonumber \\
    &= g(a \bmod c, b \bmod c, c, n) + 2 \lfloor \frac {a} {c} \rfloor h (a\bmod c, b \bmod c, c, n) + 2 \lfloor \frac {b} {c} \rfloor f (a\bmod c, b \bmod c, c, n)
    \nonumber \\
    &+ \frac {n (n + 1) (2n + 1)} {6} \lfloor \frac {a} {c} \rfloor ^2 + n(n + 1) \lfloor \frac {a} {c} \rfloor \lfloor \frac {b} {c} \rfloor + (n + 1)\lfloor \frac {b} {c} \rfloor ^2
    \nonumber \end {align}
    \]

    接下来关注到 \(h (a, b, c, n)\)。

    \[\begin {align}
    h (a, b, c, n) &= \sum \limits _{i = 0} ^{n} [i \lfloor \frac {i (a \bmod c) + (b \bmod c)} {c} \rfloor + i (i \lfloor \frac {a} {c} \rfloor + \lfloor \frac {b} {c} \rfloor)]
    \nonumber \\
    &= h (a \bmod c, b \bmod c, c, n) + \sum \limits _{i = 0} ^{n} (i^2 \lfloor \frac {a} {c} \rfloor + i \lfloor {b} {c} \rfloor)
    \nonumber \\
    &= h (a \bmod c, b \bmod c, c, n) + \frac {n(n + 1)(2n + 1)} {6} \lfloor \frac {a} {c} \rfloor + \frac {n (n + 1)} {2} \lfloor \frac {b} {c} \rfloor
    \nonumber \end {align}
    \]
  • 若 \(a < c\) 且 \(b < c\),仍记 \(m = \lfloor \frac {an + b} {c} \rfloor\),则有:

    \[\begin {align}
    g (a, b, c, n) &= 2 \sum \limits _{i = 0} ^{n} \frac {\lfloor \frac {ai + b} {c} \rfloor (\lfloor \frac {ai + b} {c} \rfloor + 1)} {2} - \sum \limits _{i = 0} ^{n} \lfloor \frac {ai + b} {c} \rfloor
    \nonumber \\
    &= -f (a, b, c, n) + 2 \sum \limits _{i = 0} ^{n} \sum \limits _{j = 1} ^{m} j [j \leq \lfloor \frac {ai + b} {c} \rfloor]
    \nonumber \\
    &= -f(a, b, c, n) + 2 \sum \limits _{i = 0} ^{n} \sum \limits _{j = 0} ^{m - 1} (j + 1) [jc + c \leq ai + b + 1]
    \nonumber \\
    &= -f(a, b, c, n) + 2\sum \limits _{j = 0} ^{m - 1} (j + 1) \sum \limits _{i = 0} ^{n} [i > \lfloor \frac {jc + c - b - 1} {a} \rfloor]
    \nonumber \\
    &= -f(a, b, c, n) + 2\sum \limits _{j = 0} ^{m - 1} (j + 1) (n - \lfloor \frac {jc + c - b - 1} {a} \rfloor)
    \nonumber \\
    &= nm(m + 1) - f(a, b, c, n) - 2 (\sum \limits _{j = 0} ^{m - 1} j \lfloor \frac {jc + c - b - 1} {a} \rfloor + \sum \limits _{j = 0} ^{m - 1} \lfloor \frac {jc + c - b - 1} {a} \rfloor)
    \nonumber \\
    &= nm^2 + nm - f (a, b, c, n) - 2h(c, c - b - 1, a, m - 1) - 2f(c, c - b - 1, a, m - 1)
    \nonumber \end {align}
    \]

    不要慌还有 \(h(a, b, c, n)\)。

    \[\begin {align}
    h (a, b, c, n) &= \sum \limits _{i = 0} ^{n} i \sum \limits _{j = 1} ^{m} [j \leq \lfloor \frac {ai + b} {c} \rfloor]
    \nonumber \\
    &= \sum \limits _{i = 0} ^{n} i \sum \limits _{j = 0} ^{m - 1} [jc + c < ai + b + 1]
    \nonumber \\
    &= \sum \limits _{j = 0} ^{m - 1} \sum \limits _{i = 0} ^{n} i [i > \lfloor \frac {jc + c - b - 1} {a} \rfloor]
    \nonumber \\
    &= \frac {mn(n + 1)} {2} - \sum \limits _{j = 0} ^{m - 1} \sum \limits _{i = 0} ^{n} i[i \leq \lfloor \frac {jc + c - b - 1} {a} \rfloor]
    \nonumber \\
    &= \frac {mn(n + 1)} {2} - \sum \limits _{j = 0} ^{m - 1} \sum \limits _{i = 0} ^{\lfloor \frac {jc + c - b - 1} {a} \rfloor} i
    \nonumber \\
    &= \frac {mn(n + 1)} {2} - \sum \limits _{j = 0} ^{m - 1} \frac {\lfloor \frac {jc + c - b - 1} {a} \rfloor (\lfloor \frac {jc + c - b - 1} {a} \rfloor + 1)} {2}
    \nonumber \\
    &= \frac {1} {2} [mn(n + 1) - \sum \limits _{j = 0} ^{m - 1} \lfloor \frac {jc + c - b - 1} {a} \rfloor^2 - \sum \limits _{j = 0} ^{m - 1} \lfloor \frac {jc + c - b - 1} {a} \rfloor]
    \nonumber \\
    &= \frac {1} {2} [mn(n + 1) - g(c, c - b - 1, a, m - 1) - f (c, c - b - 1, a, m - 1)]
    \nonumber \end {align}
    \]

Case #3 All.

呜呜呜到这里柿子终于推完了。

显然这三个函数的柿子结构是类似的,我们考虑递归同时求解三个函数即可。

#include <cstdio>

typedef long long LL;
int Abs (int x) { return x < 0 ? -x : x; }
int Max (int x, int y) { return x > y ? x : y; }
int Min (int x, int y) { return x < y ? x : y; } int Read () {
int x = 0, k = 1;
char s = getchar ();
while (s < '0' || s > '9') {
if (s == '-')
k = -1;
s = getchar ();
}
while ('0' <= s && s <= '9')
x = (x << 3) + (x << 1) + (s ^ 48), s = getchar ();
return x * k;
} void Write (LL x) {
if (x < 0)
putchar ('-'), x = -x;
if (x > 9)
Write (x / 10);
putchar (x % 10 + '0');
} void Print (LL x, char s) { Write (x), putchar (s); } const int Mod = 998244353;
const LL Inv6 = 166374059;
const LL Inv2 = 499122177; struct Node {
LL f, g, h;
Node () {}
Node (LL F, LL G, LL H) { f = F, g = G, h = H; }
}; LL Sum (LL n) { return n * (n + 1) % Mod * Inv2 % Mod; }
LL Squa (LL n) { return n * (n + 1) % Mod * (2 * n % Mod + 1) % Mod * Inv6 % Mod; } Node Calc (LL a, LL b, LL c, LL n) {
Node Res;
if (!a) {
Res.f = (n + 1) * (b / c) % Mod;
Res.g = (b / c) * (b / c) % Mod * (n + 1) % Mod;
Res.h = Sum (n) * (b / c) % Mod;
}
else if (a >= c || b >= c) {
Node Tmp = Calc (a % c, b % c, c, n);
Res.f = (Tmp.f + Sum (n) * (a / c) % Mod + (n + 1) * (b / c) % Mod) % Mod;
Res.g = (Tmp.g + 2 * (a / c) % Mod * Tmp.h % Mod + 2 * (b / c) % Mod * Tmp.f) % Mod;
Res.g = (Res.g + Squa (n) * (a / c) % Mod * (a / c) % Mod) % Mod;
Res.g = (Res.g + n * (n + 1) % Mod * (a / c) % Mod * (b / c) % Mod) % Mod;
Res.g = (Res.g + (n + 1) * (b / c) % Mod * (b / c) % Mod) % Mod;
Res.h = (Tmp.h + Squa (n) * (a / c) % Mod + Sum (n) * (b / c) % Mod) % Mod;
}
else {
LL m = (a * n + b) / c;
Node Tmp = Calc (c, c - b - 1, a, m - 1);
Res.f = (n * m % Mod - Tmp.f) % Mod;
Res.f = (Res.f + Mod) % Mod;
Res.g = (n * m % Mod * m % Mod + n * m % Mod - Res.f - 2 * Tmp.h % Mod - 2 * Tmp.f % Mod) % Mod;
Res.g = (Res.g + Mod) % Mod;
Res.h = (m * n % Mod * (n + 1) % Mod - Tmp.g - Tmp.f) % Mod;
Res.h = (Res.h + Mod) % Mod * Inv2 % Mod;
}
return Res;
} int main () {
int t = Read ();
while (t--) {
LL n = Read (), a = Read (), b = Read (), c = Read ();
Node Res = Calc (a, b, c, n);
Print (Res.f, ' '), Print (Res.g, ' '), Print (Res.h, '\n');
}
return 0;
}

Solution -「Luogu 5170」类欧几里得算法的更多相关文章

  1. Solution -「LOJ #138」「模板」类欧几里得算法

    \(\mathcal{Description}\)   Link.   \(T\) 组询问,每次给出 \(n,a,b,c,k_1,k_2\),求 \[\sum_{x=0}^nx^{k_1}\left\ ...

  2. Solution -「Luogu 4135」作诗

    写在前面 & 前置芝士   好像是好久没有打理 blog 了.感觉上学期是有点颓.嘶,初三了好好冲一次吧.   那么回到这道题目.你会分块就能看懂. 题目大意   先挂个来自洛谷的 link. ...

  3. Solution -「Luogu 3959」 宝藏

    果真是宝藏题目. 0x01 前置芝士 这道题我是真没往状压dp上去想.题目来源. 大概看了一下结构.盲猜直接模拟退火!\xyx 所需知识点:模拟退火,贪心. 0x02 分析 题目大意:给你一个图,可能 ...

  4. [P5170] 类欧几里得算法

    "类欧几里得算法"第二题 P5170 [题意]已知\(n,a,b,c\),求 \[ \begin{aligned} f_{1}(a,b,c,n)&=\sum_{i=0}^n ...

  5. LOJ138 类欧几里得算法

    类欧几里得算法 给出 \(T\) 组询问,每组用 \(n, a, b, c, k_1, k_2\) 来描述.对于每组询问,请你求出 \[ \sum_{x = 0} ^ {n} x ^ {k_1} {\ ...

  6. Luogu 5170 【模板】类欧几里得算法

    原理不难但是写起来非常复杂的东西. 我觉得讲得非常好懂的博客.   传送门 我们设 $$f(a, b, c, n) = \sum_{i = 0}^{n}\left \lfloor \frac{ai + ...

  7. Solution -「ARC 104E」Random LIS

    \(\mathcal{Description}\)   Link.   给定整数序列 \(\{a_n\}\),对于整数序列 \(\{b_n\}\),\(b_i\) 在 \([1,a_i]\) 中等概率 ...

  8. BZOJ3817 Sum(类欧几里得算法)

    设$t=\sqrt r$,原题转化为$\sum_{x=1}^n(4*\lfloor\frac{tx}2\rfloor-2*\lfloor tx\rfloor+1)$考虑如何求$\sum_{x=1}^n ...

  9. 「Luogu 1525」关押罪犯

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description \(S\)城现有两座监狱,一共关押着\(N\)名罪犯,编号分别为\(1 - N\) ...

随机推荐

  1. 关于IPC和PTH用户权限问题,psexec拒绝访问(Access Denied)的原因

    前瞻 关于net use和psexec无法使用本地管理员组用户建立连接的问题 测试环境: win7系统,存在域环境 域名:de1ay 普通域用户: de1ay\de1ay 域管理员用户:de1ay\A ...

  2. Docker容器的安装和使用

    Docker容器的安装和使用 安装: curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun 或国内:curl -sSL ...

  3. Invocation failed Unexpected end of file from server java.lang.RuntimeException: Invocation failed Unexpected end of file from server

    Android studio 提交 push的时候报错. Invocation failed Unexpected end of file from serverjava.lang.RuntimeEx ...

  4. 在SpringBoot中使用logback优化异常堆栈的输出

    一.背景 在我们在编写程序的过程中,无法保证自己的代码不抛出异常.当我们抛出异常的时候,通常会将整个异常堆栈的信息使用日志记录下来.通常一整个异常堆栈的信息是比较多的,而且存在一些没用的信息.那么我们 ...

  5. netty系列之:netty中常用的xml编码解码器

    目录 简介 XmlFrameDecoder XmlDecoder 总结 简介 在json之前,xml是最常用的数据传输格式,虽然xml的冗余数据有点多,但是xml的结构简单清晰,至今仍然运用在程序中的 ...

  6. linux中MySQL主从配置(Django实现主从读写分离)

    一 linux中MySQL主从配置原理(主从分离,主从同步) mysql主从配置的流程大体如图: 1)master会将变动记录到二进制日志里面: 2)master有一个I/O线程将二进制日志发送到sl ...

  7. vue - Vue路由(扩展)

    忙里偷闲,还在学校,趁机把后面的路由多出来的知识点学完 十.缓存路由组件 让不展示的路由组件保持挂载,不被销毁 在我们的前面案例有一个问题,都知道vue的路由当我们切换一个路由后,另一个路由就会被销毁 ...

  8. 精彩分享 | 欢乐游戏 Istio 云原生服务网格三年实践思考

    作者 吴连火,腾讯游戏专家开发工程师,负责欢乐游戏大规模分布式服务器架构.有十余年微服务架构经验,擅长分布式系统领域,有丰富的高性能高可用实践经验,目前正带领团队完成云原生技术栈的全面转型. 导语 欢 ...

  9. Linux下将一个文件压缩分包成多个小文件

    压缩分包 将文件test分包压缩成10M 的文件: tar czf - test | split -b 10m - test.tar.gz 解压 将第一步分拆的多个包解压: cat test.tar. ...

  10. 好客租房31-事件绑定this指向(bind)

    事件this指向bind 利用ES5中bind方法 将事件处理程序中的this和组件实例绑定在一起 //导入react     import React from 'react'           ...