【题解】类欧几里得算法

题面

,要求 级别的时间解决。

通常

洛谷模版链接:https://www.luogu.com.cn/problem/P5170

解析

(1)当

可以理解为 先单独处理,分别从每个 中拿出所有 也单独处理。

再将 剩下的和 加起来

(2)当

,代表循环内下取整可以取到的最大整数。

上面这句话的意思:当 时,所有 都会被计入 1。

也就是说一个 ,会被计入 次,求出的值和原式相同。

分析 Iverson 括号的内部条件,

这样我们就得到了和 有关的约束,原式变为:

将求和下标平移 ,将原式变成符合 函数的形式:

(1)当

(2)当

这两种变换使参数 变为 ,与欧几里得算法的辗转相除结构一致,从而保证 的时间复杂度。

派生形式

函数的推导

(1)当

(2)当

,代表循环内下取整可以取到的最大整数。

,则原式为:

又因为:

所以,原式为:

函数的推导

(1)当

,则有:

所以原式为:

(2)当

因为 ,则原式为:

,设

将求和下标平移 ,将原式变成符合 函数的形式:

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const LL P = 998244353;
LL i2 = 499122177, i6 = 166374059;   // 逆元

struct node {
    LL f, g, h;
};

node dfs(LL a, LL b, LL c, LL n) {
    if (n < 0) return {0, 0, 0};
    node res;
    LL qa = a / c, qb = b / c, ra = a % c, rb = b % c;
    if (a == 0) {
        res.f = (n + 1) * qb % P;
        res.g = n * (n + 1) % P * i2 % P * qb % P;
        res.h = (n + 1) * qb % P * qb % P;
    }
    else if (a >= c || b >= c){
        node no = dfs(ra, rb, c, n);
        res.f = (no.f + n * (n + 1) % P * i2 % P * qa % P + (n + 1) * qb % P) % P;
        res.g = (no.g + n * (n + 1) % P * (2 * n + 1) % P * i6 % P * qa % P
            + n * (n + 1) % P * i2 % P * qb % P) % P;
        res.h = (no.h + 2 * qa % P * no.g % P + 2 * qb % P * no.f % P +
            n * (n + 1) % P * (2 * n + 1) % P * i6 % P * qa % P * qa % P +
            n * (n + 1) % P * qa % P * qb % P + (n + 1) * qb % P * qb % P) % P;
    }
    else if (n == 0) {
		res.f = qb;
		res.g = 0;
		res.h = qb * qb % P;
	}
    else {
        LL m = (a * n + b) / c;
        node no = dfs(c, c - b - 1, a, m - 1);
        res.f = ((n * m % P - no.f) % P + P) % P;  
        LL term1 = m % P * n % P * (n + 1) % P * i2 % P;
        LL term2 = (no.f + no.h) % P * i2 % P;
        res.g = (term1 - term2 + P) % P;
        res.h = (n % P * m % P * (m + 1) % P
            - (2 * no.g) % P 
            - (2 * no.f) % P 
            - res.f + 4 * P) % P;
    }
    return res;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int T;
    cin >> T;
    while (T --) {
        LL a, b, c, n;
        cin >> n >> a >> b >> c;
        node ans = dfs(a, b, c, n);
        cout << ans.f << " " << ans.h << " " << ans.g << "\n";
    }

    return 0;
}
相关推荐
可编程芯片开发18 分钟前
基于CPS-SPWM链式STATCOM系统在电压不平衡环境下控制策略的simulink建模与仿真
算法
Jerry1 小时前
LeetCode 202. 快乐数
算法
hans汉斯1 小时前
基于改进交叉熵损失函数与Transformer的心电信号高风险分类研究
功能测试·深度学习·算法·yolo·目标检测·分类·transformer
Jerry1 小时前
LeetCode 349. 两个数组的交集
算法
熊猫_豆豆2 小时前
QT6 Android C++ 自制美观闹钟
android·c++·qt·闹钟
YuK.W2 小时前
Leetcode100: 70.爬楼梯、118.杨辉三角、198.打家劫舍
java·算法·leetcode
随意起个昵称2 小时前
状压dp-基础题目2([USACO12MAR] Cows in a Skyscraper G)
c++·算法·动态规划
Jerry3 小时前
LeetCode 1002. 查找共用字符
算法
无限的鲜花4 小时前
协程本质是函数加状态机——零基础深入浅出 C++20 协程
c++·算法·c++20
精明的身影4 小时前
C++自学之路1:Hello world
开发语言·c++