哥德巴赫猜想推广。题意:给一个数,如果它能被分解成四个素数的和,那么输出这四个素数,如果不能,输出指定信息。
我的解题思路:由于一个大于2的偶数能够被分解成两个素数的和,因此可以推出一个大于8的数必定能分解成4个素数的和。首先如果这个大于8的数是偶数,那么它能够被分解成两个偶数的和,再把这两个偶数根据哥德巴赫猜想分解,如果是奇数的话,那么它必定能分解成2,3和一个偶数的和,在把这个偶数分解就行了。
我的解题代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
using namespace std;
const int N = 10000001;
bool isprime[N];
int primes[N], pn;
int num;
void InitRead();
void DataProcess();
void FastSieve(int maxn);
int main()
{
InitRead();
while (~scanf("%d", &num))
{
DataProcess();
}
return 0;
}
void InitRead()
{
pn = 0;
memset(isprime, true, sizeof(isprime));
isprime[0] = isprime[1] = false;
FastSieve(N - 1);
return;
}
void DataProcess()
{
if (num < 8)
{
puts("Impossible.");
return;
}
int x;
if (num % 2 == 0)
{
x = num - 4;
printf("2 2 ");
}
else
{
x = num - 5;
printf("2 3 ");
}
for (int i=0; i<pn; ++i)
{
if (isprime[x - primes[i]])
{
printf("%d %d\n", primes[i], x - primes[i]);
break;
}
}
return;
}
void FastSieve(int maxn)
{
for (int i=2; i<=maxn; ++i)
{
if (isprime[i]) primes[pn++] = i;
for (int j=0; j<pn; ++j)
{
if (i * primes[j] > maxn) break;
isprime[i * primes[j]] = false;
if (i % primes[j] == 0) break;
}
}
return;
}