Files
python/GESP/Level5/GESP202409/20240902.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

72 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
// 欧拉筛
const int N = 1e5 + 10;
int primes[N], cnt; // primes[]存储所有素数
bool st[N]; // st[x]存储x是否被筛掉
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i])
primes[cnt++] = i;
for (int j = 0; primes[j] * i <= n; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0)
break;
}
}
}
/*
3
6 188 9999
参考答案
2
4
-1
*/
int main() {
get_primes(N);
int T;
cin >> T;
while (T--) {
int h;
cin >> h;
// 如果h质数,则一次魔法攻击就可以解决问题,答案是1,continue表示本组测试用例处理完成
if (!st[h]) {
cout << 1 << endl;
continue;
}
// 如果没有干掉,需要找到最大的质数扣除掉
int mx = 0;
for (int i = 0;; i++) {
if (primes[i] > h)
break;
else
mx = max(mx, primes[i]);
}
// 扣除掉最大质数
h -= mx;
int ans = 1;
// 开始物理攻击
int base = 1;
for (int i = 1;; i++) {
ans++;
h -= base;
if (h < 0) {
cout << -1 << endl;
break;
}
if (h == 0) {
cout << ans << endl;
break;
}
base *= 2;
}
}
return 0;
}