Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

素数筛法+二分可以过

Posted by FangCun at 2016-12-14 11:41:40 on Problem 2262
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

#define MAXN 1000010
#define ll long long

int n;

int Prime[MAXN];
bool NotPrime[MAXN];
int pcnt;

void InitPrime(int n)
{
    for(ll i=2;i<=n;i++)
    {
        if(!NotPrime[i])
        {
            Prime[pcnt++]=i;
            for(ll j=i*i;j<=n;j+=i) NotPrime[j]=true;
        }
    }
}

int main()
{
    InitPrime(999999);

    while(scanf("%d",&n),n)
    {
        for(int i=0;i<pcnt;i++)
        {
            int a=Prime[i];
            int b=*lower_bound(Prime,Prime+pcnt,n-a);

            if(a+b==n)
            {
                printf("%d = %d + %d\n",n,a,b);
                break;
            }
        }
    }

    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator