Primes (Gym-102267B)

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值