[AcWing 1292] 哥德巴赫猜想

image
image


点击查看代码
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 1e6 + 10;

int n;
vector<int> primes;
bool st[N];

void get_primes(int x)
{
    for (int i = 2; i <= x; i ++) {
        if (!st[i])
            primes.push_back(i);
        for (auto p : primes) {
            if (p * i > x)
                break;
            st[p * i] = true;
            if (i % p == 0)
                break;
        }
    }
}

void solve()
{
    get_primes(1e6);
    while (cin >> n) {
        if (n == 0)
            break;
        for (auto p : primes) {
            if (p == 2)
                continue;
            int b = n - p;
            if (!st[b]) {
                printf("%d = %d + %d\n", n, p, b);
                break;
            }
        }
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

  1. 线性筛法做预处理,降低复杂度
posted @ 2022-08-03 22:44  wKingYu  阅读(12)  评论(0编辑  收藏  举报