跳到正文

个人记录

板子

General0826约 4 分钟
原文

这是 2026年8月4日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年8月3日
收录
2026年8月4日
更新
2026年11月8日
篇幅
约 4 分钟

版本

  1. v3当前
  2. v2
  3. v1

因为码风的修改是时候要有个收集板子的地方了。

欧拉函数

C++
int phi[N],prime[N],tot,pri[N]; 
void init (int n) {
	phi[1] = 1;
	for (int i = 2; i <= n; i++) {
		if (prime[i] == 0) {
			pri[++tot] = i;
			phi[i] = i - 1;
		}
		for (int j = 1; j <= tot && i * pri[j] <= n; j++) {
			prime[i*pri[j]] = 1;
			phi[i*pri[j]] = phi[i] * phi[pri[j]];
			if (i % pri[j] == 0) {
				phi[i*pri[j]] = phi[i] * pri[j];
				break; 
			}  
		}
	}
}

或 搭配线性筛

C++
int phi(int x){
	int ans = x;
	for (int i = 1; i <= tot; i++) 
		if (x % prime[i] == 0)
			ans = ans / v[i] * (v[i] - 1);
	return ans;
}

线性筛

C++
void init (int n) {
	for (int i = 2; i <= n; i++) {
		if (pri[i] == 0) 
			prime[++tot] = i;
		for (int j = 1; j <= tot && i * prime[j] <= n; j++) {
			pri[i*prime[j]] = 1;
			if (i % prime[j] == 0) 
				break; 
		}
	}
}

exgcd

C++
#include<bits/stdc++.h>
#define int long long
#define db double
using namespace std;
const int N=1e5+5;
int a,b,c,x,y;
int excit (int a, int b, int &x, int &y) {
	if (b == 0) {
		x=1;
		y=0;
		return a;
	}
	int gc = excit(b, a % b, x, y), t=x;
	x = y, y = t - (a / b) * y;
	return gc;  
}
void work () {
	cin >> a >> b >> c;
	int gc = excit(a, b, x, y);
	if (c % gc != 0) {
		cout << -1 << '\n';
		return ;
	}
	x = c / gc * x, y = c / gc * y;
	int dx = b / gc, dy = a / gc; 
	if (x < 0) {
		int k = ceil((1.0 - x) / dx);
		x += dx * k, y -= dy * k;
	}
	else {
		int k = (x - 1) / dx;
		x -= dx * k, y += dy * k;
	}
	if (y > 0)
		cout << (y - 1) / dy + 1 << ' ' << x << ' ' << (y - 1) % dy + 1 << ' ' << x + (y - 1) / dy * dx << ' ' << y << '\n';
	else
		cout << x << ' ' << y + dy * (int)ceil((1.0 - y) / dy) << '\n';
	return ;
}signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int T;
	cin >> T;
	while (T--)
		work();
	return 0;
}