跳到正文

题解

题解:P12254 [蓝桥杯 2024 国 Java B] 美丽区间

Perfect_Youth约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月17日
篇幅
约 2 分钟
题目
P12254

版本

仅有当前版本

简单题喵qwq。

思路:

可以先预处理一个答案数组,在准备一个存上一次 ll 的位置的变量,然后按题意模拟即可。

代码:

C++
#include <bits/stdc++.h>
#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1 , 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;

using namespace std;

inline
int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ '0');
		ch = getchar();
	}
	return x * f;
}

const int N = 1e6 + 7;

int k, t, l, cnt[N], ans;

int main() {
	k = read();
	l = 1;
	for (int i = 1; i <= 1000000; ) {
		i = l + k;
		while (__gcd(i, l) != 1 && i <= 1000000) i++;
		ans++;
		for (int j = l; j <= min(i, 1000000); j++) cnt[j] = ans;
		l = i + 1;
	}
	t = read();
	while (t--) {
		k = read();
		printf ("%d\n", cnt[k]);
	}
	return 0;
}
原文