You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
// 欧拉筛
const int N = 1e6 + 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;
}
}
}
int main() {
// 加快读入
ios::sync_with_stdio(false), cin.tie(0);
// 欧拉筛求出区间内所有质数
get_primes(N - 1); // 不能到N因为数组下标从0开始要不会越界~
int n;
while (cin >> n, n) {
for (int i = 1;; i++) { // 枚举每个奇数质数,不用上界,因为认为哥德巴赫猜想是正确的,肯定有解
int a = primes[i]; // primes[0]=2,因为2特殊是质数并且是奇数本题要求是奇数放过数字2
int b = n - a; // 差值
if (!st[b]) { // 差值也是质数,这个st数组用的漂亮啊我还以为要在primes中二分呢
// 第一个找的就是最小奇数质数因子,所以相对就是最大的奇数质数因子,两者的差就是最大
printf("%d = %d + %d\n", n, a, b);
break;
}
}
}
return 0;
}