Problem Description
A prime number is a natural number greater than 1 and has exactly 2 divisors which are 1 and the number itself.
You are given a prime number nn, find any 2 prime numbers aa and bb such that a+b=n or state that no such pair of primes exists.
Input
The input contains a single prime number nn(2≤n≤107).
Output
If there doesn't exist any 2 primes such that their summation is equal to nn then print -1, otherwise print the 2 primes on a single line separated by a space.
Examples
input
5
output
2 3
input
11
output
-1
题意:给出一个质数 n,寻找一个素数对 a、b 使得 a+b=n,若存在素数对 a、b 输出 a、b,否则输出 -1
思路:筛素数后从前向后暴力扫到 n/2 即可,对于每个素数 i,判断 n-i 是否为素数
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const int MOD = 1E9+7;
const int N = 10000000+5;
const int dx[] = {1,-1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int primes[N],cnt;
bool bPrime[N];
void getPrime(int n) {
bPrime[0]=true;
bPrime[1]=true;
for(int i=2; i<=n; i++) {
if(!bPrime[i])
primes[cnt++]=i;
for(int j=0; j<cnt&&i*primes[j]<n; j++) {
bPrime[i*primes[j]]=true;
if(i%primes[j]==0)
break;
}
}
}
int main() {
getPrime(1E7);
int n;
scanf("%d",&n);
bool flag=true;
for(int i=0; i<cnt && primes[i]<=n/2; ++i) {
if(!bPrime[n-primes[i]]) {
printf("%d %d\n",primes[i],n-primes[i]);
flag=false;
break;
}
}
if(flag)
printf("-1\n");
return 0;
}