题意:
给定一个奇数n,求k(k <= 3)个质数相加等于n
思路:
哥德巴赫猜想:任一>2的偶数都可以写成两个质数的和
因此可得,如果n是质数,则n就由他本身得到
如果n不是质数,就利用哥德巴赫猜想,n-=3,此时n为偶数,再由两个质数相加得到
Code:
#pragma GCC optimize(3) #pragma GCC optimize(2) #include <map> #include <set> #include <array> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <cstring> #include <sstream> #include <iostream> #include <stdlib.h> #include <algorithm> #include <unordered_map> using namespace std; typedef long long ll; typedef pair<int, int> PII; #define Time (double)clock() / CLOCKS_PER_SEC #define sd(a) scanf("%d", &a) #define sdd(a, b) scanf("%d%d", &a, &b) #define slld(a) scanf("%lld", &a) #define slldd(a, b) scanf("%lld%lld", &a, &b) const int N = 1e6 + 10; const ll M = 4e12; const int mod = 998244353; bool is_prime(int n) { if (n < 2) return false; for (int i = 2; i <= n / i; i++) { if (n % i == 0) { return false; } } return true; } int main() { #ifdef ONLINE_JUDGE #else // freopen("/home/jungu/code/in.txt", "r", stdin); // freopen("/home/jungu/code/out.txt", "w", stdout); // freopen("/home/jungu/code/out.txt","w",stdout); #endif // ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n; sd(n); if (is_prime(n)) { printf("%d %d ", 1, n); } else { n -= 3; printf("3 "); for (int i = 2; i <= n; i++) { if (is_prime(i) && is_prime(n - i)) { printf("3 %d %d", i, n - i); break; } } } return 0; }