跳到正文

个人记录

2025-8-15测试总结

Prominence约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年8月17日
收录
2026年8月20日
更新
2026年11月14日
篇幅
约 4 分钟

版本

  1. v1当前

T1 - gcd

题目

其中对于 100%100\% 的数据,每个数字都 ≤106\le10^6。

题解

先从最容易的开始,判断是否 not coprime,只需要线性跑一边 gcd ,判断是否 >1\gt1 就行。

然后就是 pairwise coprime 了。我们注意到,两个数互质,和两个数的质因子都不相同,这两个条件互为充要条件。可以预处理每个数字的质因子有哪些。

怎么记录每个数的质因子呢?线性筛是个好东西,就它了!众所周知,正整数 xx 的质因子数一定不超过 log⁡2x\log_2x,所以空间开到 106×20×410^6\times20\times4 BytesBytes 是完全没有问题的。核心代码如下:

C++
int cnt[1000006], p[1000006][22];
int primes[100005], pc = 0;
bool s[1000006];
void init() {
	for (int i = 2; i <= 1000000; i++) {
		if (!s[i]) {
			primes[++pc] = i;
			cnt[i] = 1;
			p[i][1] = i;
		}
		for (int j = 1; j <= pc && primes[j] * i <= 1000000; j++) {
			int t = primes[j] * i;
			s[t] = true;
			memcpy(p[t], p[i], sizeof p[i]);
			if (i % primes[j]) {
				cnt[t] = cnt[i] + 1;
				p[t][cnt[t]] = primes[j];
			} else {
				cnt[t] = cnt[i];
				break;
			}
		}
	}
}

把这些质因子都存在一个 set 或者一个哈希表里判重就可以区分出 setwise coprime 和 pairwise coprime 了。

Code

C++
#include <bits/stdc++.h>
using namespace std;
int n, arr[1000006];
int cnt[1000006], p[1000006][22];
int primes[100005], pc = 0;
bool s[1000006];
void init() {
	for (int i = 2; i <= 1000000; i++) {
		if (!s[i]) {
			primes[++pc] = i;
			cnt[i] = 1;
			p[i][1] = i;
		}
		for (int j = 1; j <= pc && primes[j] * i <= 1000000; j++) {
			int t = primes[j] * i;
			s[t] = true;
			memcpy(p[t], p[i], sizeof p[i]);
			if (i % primes[j]) {
				cnt[t] = cnt[i] + 1;
				p[t][cnt[t]] = primes[j];
			} else {
				cnt[t] = cnt[i];
				break;
			}
		}
	}
}
inline int gcd(int x, int y) {
	while (y) {
		int tmp = x;
		x = y;
		y = tmp % y;
	}
	return x;
}
set<int> ps;
int main() {
	//freopen("gcd.in", "r", stdin);
	//freopen("gcd.out", "w", stdout);
	init();
	scanf("%d", &n);
	int sum = 0;
	int g;
	for (int i = 1; i <= n; i++) {
		scanf("%d", arr + i);
		if (i == 1) g = arr[i];
		else g = gcd(g, arr[i]);
		sum += cnt[arr[i]];
		for (int j = 1; j <= cnt[arr[i]]; j++) {
			ps.insert(p[arr[i]][j]);
		}
	}
	if (g > 1) printf("not coprime");
	else if (sum ^ ps.size()) printf("setwise coprime");
	else printf("pairwise coprime");
	return 0;
}

感慨

这题花了我 1h,难度 J3 左右吧,果真是绝世好题,还是对数学不太熟啊,明明比 NhOI T6 简单。

留下一句话:What *** problem it is!

T2 - play

原文