icpc-Repeating Goldbachs

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.

Sample Input and Output

20   3

30   4

40   5

#include<bits/stdc++.h>
using namespace std;
int f(int a)
{
    int i;
    if(a==0||a==1) return 0;
    int flag=0;
    for(i=2;i<=sqrt(a);i++)
    {
        if(a%i==0) {flag=1;break;}
    }
    if(!flag) return 1;
    return 0;
}
int main()
{
    int n;
    cin>>n;
    int cnt=0;
    int i=2;
    while(n>=4)
    {
        for(;i<=n/2;i++)
        {
            if(f(i)) break;
        }
        int a;
        a=n-i;
        if(f(a))
        {
            n=a-i;i=2;cnt++;
        }
        else
        {
            i++;
        }
    }
    printf("%d",cnt);
}

主要是对素数的判断,这里要注意,此题卡sqrt求素数,不用sqrt就会超时!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值