1 solutions
-
2
67要拿🏀杯国一,你们知道吗?
结论
- 若 或 是质数,答案为
- 若 是合数,答案为
分析
1.
无法进行任何拆分,直接返回 。
2. 是质数
假设存在拆分 且 。设 ,则 ,,且 。因为 是质数且 ,所以 ,于是 ,与 矛盾。因此质数无法拆分。
3. 是合数()
偶数合数:设 。每次拆出 :,。最终得到 个 ,答案为 。
奇数合数:设 且 。目标是构造 个 和 个 。
-
如果 是 的倍数,拆 , 是偶数全拆成 。
-
否则设 是 的最小奇质因子(,),拆 :
- 是 的倍数,拆成 + 一堆
- 中 是偶数,所以这部分全是偶数,全拆成
举例():
,不是 的倍数,最小奇质因子 ,。
第一步:
- 是 的倍数:, 是偶数 → 个 ,加上 个 ,共 个元素
- 是偶数:全拆成 个
最终: 个 + 个 + 个 = 个元素 = ✓
最终答案为 。
代码
#include <bits/stdc++.h> using namespace std; vector<int> sieve_primes(int limit) { vector<bool> is_prime(limit + 1, true); vector<int> primes; is_prime[0] = is_prime[1] = false; for (int i = 2; i <= limit; ++i) { if (is_prime[i]) primes.push_back(i); for (int p : primes) { if (1LL * i * p > limit) break; is_prime[i * p] = false; if (i % p == 0) break; } } return primes; } bool is_prime(int n, const vector<int>& primes) { if (n < 2) return false; for (int p : primes) { if (1LL * p * p > n) break; if (n % p == 0) return false; } return true; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); vector<int> primes = sieve_primes(31623); int T; cin >> T; while (T--) { int n; cin >> n; if (n == 1) cout << 1 << '\n'; else if (is_prime(n, primes)) cout << 1 << '\n'; else cout << n / 2 << '\n'; } return 0; }
Information
- ID
- 3
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 4
- Tags
- # Submissions
- 41
- Accepted
- 8
- Uploaded By