0
点赞
收藏
分享

微信扫一扫

多校训练第1轮.G——Goldbach’s conjecture【素数】(哥德巴赫猜想)


​​题目传送门​​

多校训练第1轮.G——Goldbach’s conjecture【素数】(哥德巴赫猜想)_#include

题意

  • 求哥德巴赫猜想

题解

  • 数据范围多校训练第1轮.G——Goldbach’s conjecture【素数】(哥德巴赫猜想)_#include_02
  • 不能打表,直接暴力搜就可以
  • 分析复杂度:考虑素数分布:多校训练第1轮.G——Goldbach’s conjecture【素数】(哥德巴赫猜想)_打表_03。不太严谨地近似为随机分布,则可以认为枚举成功的概率是
    多校训练第1轮.G——Goldbach’s conjecture【素数】(哥德巴赫猜想)_数据_04的。因此就算暴力判断跑得很满,也是多校训练第1轮.G——Goldbach’s conjecture【素数】(哥德巴赫猜想)_数据_05的。
    实测多校训练第1轮.G——Goldbach’s conjecture【素数】(哥德巴赫猜想)_数据_06

AC-Code

#include <iostream>
#include <vector>
#include <cstring>

using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6;

bool IsPrime(int x) { //
if (x <= 1) return false;
if (~x & 1 || x % 3 == 0) return x <= 3; // 取反末尾是1说明是偶数
for (int i = 5, j = 2; i * i <= x; i += j, j = j == 2 ? 4 : 2)
if (x % i == 0) return false;
return true;
}
int main() {
int n; while (cin >> n) {
if (n == 4) {
cout << "2 2" << endl; continue;
}
if (IsPrime(n - 3)) {
cout << 3 << " " << n - 3 << endl; continue;
}
for (int i = 5, j = 2; ; i += j, j = j == 2 ? 4 : 2) {
if (IsPrime(i) && IsPrime(n - i)) {
cout << i << " " << n - i << endl; break;
}
}
}

}


举报

相关推荐

0 条评论