The Goldbach Conjecture states that any even number x ≥ 4 can be expressed as the sum of two primes. It can be verified that the conjecture is true for all x ≤ 106 .
Define a Goldbach step as taking x (4 ≤ x ≤ 106 ), finding primes p and q (with p ≤ q) that sum to x, and replacing x with q − p. If there are multiple pairs of primes which sum to x, we take the pair with the largest difference. That difference must be even and less than x. Therefore, we can repeat more Goldbach steps, until we can reach a number less than 4.
Given x, find how many Goldbach steps it takes until reaching a number less than 4.
Input
The input will consist of a single integer x (4 ≤ x ≤ 106 ).
Output Print,
on a single line, the number of Goldbach steps it takes to reach a number less than 4.
就是一个关于哥德巴赫的小扩展吧,之前在学习做过相识的,关键技巧就是只需要枚举到x/2,
for循环时用i,和x-i,这样就不用开双重循环了
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e6+10;
int ans;
bool fun(int x)
{
if(x<2)
return 0;
for(int i=2;i<=x/i;i++)
if(x%i==0)
return 0;
return 1;
}
int main()
{
int x;
cin>>x;
while(1)
{
if(x<4)
break;
for(int i=2;i<=x/2;i++)
{
if(fun(i)&&fun(x-i))
{
x=abs(x-i-i);
if(abs(x-i-i)%2==0)
break;
}
}
ans++;
}
cout<<ans;
return 0;
}