推柿子大赛了属于是。

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

\[\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. Java-GUI编程之Swing组件

    目录 为组件设置边框 使用JToolBar创建工具条 JColorChooser和JFileChooser JColorChooser JFileChooser JOptionPane 基本概述 四种 ...

  2. navicat软件、 python操作MySQL

    查询关键字之having过滤 having与where的功能是一模一样的 都是对数据进行筛选 where用在分组之前的筛选 havng用在分组之后的筛选 为了更好的区分 所以将where说成筛选 ha ...

  3. CF1682E Unordered Swaps

    鸽着,我不知道为什么对? 题意: 思路: code: #include<bits/stdc++.h> using namespace std; const int N=5e5+5; int ...

  4. Java_Scanner的使用

    目录 Scanner对象 scanner.next()和scanner.nextln()的区别 scanner.hasNext()和scanner.hasNextln() Scanner拓展 视频课程 ...

  5. Mac Book安装Windows发烫的问题

    Mac Book安装Windows后,电脑发烫,风扇一直高速旋转.针对此问题百度搜索了一下, 大多数人说更改电源选项,由"平衡"模式改为"节能"模式,亲身体验了 ...

  6. client offset scroll 之间的区别

    一.client 属性 值 clientWidth 元素被设置的宽度 + padding左右内间距 clientHeight 元素被设置的高度 + padding上下内间距 clientLeft 左 ...

  7. Java常用类-包装类

    包装类 ​ Java中的基本类型功能简单,不具备对象的特性,为了使基本类型具备对象的特性,所以出现了包装类,就可以像操作对象一样操作基本类型数据;包装类不是为了取代基本数据类型,而是在数据类型需要使用 ...

  8. mysql数据恢复 根据旧备份的sql文件和当前data下的ibd文件恢复innodb引擎数据

    1.使用navicat fro mysql数据库工具进行恢复 2.将原有备份的sql文件导入数据库 3.新建一个空数据库 4将备份数据库的数据表复制到新建数据库(只复制表格式) 5.在命令行模式中 u ...

  9. 【小程序自动化Minium】一、框架介绍和环境搭建

    微信小程序自动化测试系列分享 一.Minium 简介 minium 是微信团队为小程序专门开发的自动化框架,我们可以用它来做小程序的UI自动化测试,但是它的能力却不仅仅在于UI自动化. 正是得益于官方 ...

  10. Vue回炉重造之封装一个实用的人脸识别组件

    前言 人脸识别技术现在越来越火,那么我们今天教大家实现一个人脸识别组件. 资源 element UI Vue.js tracking-min.js face-min.js 源码 由于我们的电脑有的有摄 ...