【题解】类欧几里得算法

题面

求 ,要求 级别的时间解决。

通常 。

洛谷模版链接: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;
}
相关推荐
Sakura_j_h_6 小时前
EasyEDA PCB 快速设计Skills
c++·python·github
ZhengEnCi7 小时前
Agent 架构怎么选:从单 Agent 到多 Agent 编排的选型决策指南
算法·架构
程序喵大人7 小时前
【C++入门】内存模型与资源管理 - 09 shared_ptr 和 weak_ptr 解决共享,也带来代价
开发语言·c++·资源管理·智能指针·内存模型
秋天的怀念_David7 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Pniubi8 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
hhb_6188 小时前
大模型落地三重优化实战
人工智能·算法·机器学习
云和数据.ChenGuang8 小时前
光哥说Python RAG 系统需要哪些评估指标:从检索到生成的全链路评测指南
网络·人工智能·python·算法·langchain
jimy19 小时前
C++ 中的 `T&&`:右值引用与转发引用
开发语言·c++
2601_9707038310 小时前
工业除湿机租赁:把一份租金合同拆开看
大数据·数据结构·算法·数据库架构·推荐算法
心中有你021410 小时前
Java实现Dijkstra最短路径算法(邻接表 + 优先队列)
java·数据库·算法