万能欧几里得板子

对于 \((a,b,c,n)\),考虑一条直线 \(y = \frac{ax+b}{c}\),那么 \(\lfloor \frac{ai+b}{c} \rfloor\) 即为横坐标为 \(i\) 的点下方的整点,对于一个整点其贡献看题目。

两种操作中,向上(即 U 操作)会把当前横坐标答案加上这个整点的贡献,向右(即 R 操作)会加上当前横坐标答案,并更新横坐标及其答案,根据题意修改 UR 操作即可直接套用模板。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int p=998244353;
struct node {
	...
	node operator*(const node &o) const {
		...
	}
};
node qpow(node a,int b) {
	node ans=...;
	while(b) {
		if(b&1) ans=ans*a;
		a=a*a;
		b>>=1;
	}
	return ans;
}
node euclid(int a,int b,int c,int n,node U,node R) {
	if(b>=c) return qpow(U,b/c)*euclid(a,b%c,c,n,U,R);
	if(a>=c) return euclid(a%c,b,c,n,U,qpow(U,a/c)*R);
	int m=(a*n+b)/c;
	if(!m) return qpow(R,n);
	return qpow(R,(c-b-1)/a)*U*euclid(c,(c-b-1)%a,a,m-1,R,U)*qpow(R,n-(c*m-b-1)/a);
}
posted @ 2025-12-23 21:18  System_Error  阅读(1)  评论(0)    收藏  举报