牛客網__Wonderprime Brands

鏈接:https://ac.nowcoder.com/acm/contest/984/G
來源:牛客網
 

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld

題目描述

The cows are forever competing to see who has the best brand. The latest rage is brands that are 'Wonderprimes'. You probably already know that a brand consists of a sequence of digits that does not begin with 0; brands actually look a lot like positive integers.
A wonderprime is a number that can be partitioned into two prime numbers, each of which has at least D digits and, of course, doesn't start with 0. When D=2, the number 11329 is a wonderprime (since it connects 113 and 29, both of which are prime).
Only a few of the cows have wonderprime brands, but they all want one. Your job is to find the first wonderprime greater than or equal to a supplied integer N (1 <= N <= 2,000,000,000). No integer greater than 2,000,000,000 will be required.

輸入描述:

Line 1: Two space-separated integers: D and N(1 <= D <= 5, 1<= N <= 2000000000)

輸出描述:

Line 1: A line that contains the first wonderprime no smaller than N.

示例1

輸入

複製

2 11328

輸出

複製

11329

一開始預處理出素數表,寫了一波後發現,要處理的數超過能處理的素數表,然後一想到其實大於某個數的素數很少複雜度就可以找到,所以就暴力找了,然後就根據前面n位剛好等於和前面n位大於兩種情況去算就可以了

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 1e6+9;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f3f3f3f;

int a[50];

int Find(int x){
    if(x==1)return 2;
    for(int i=x;;i++){
        int flag=1;
        for(int j=2;j*j<=i;j++){
            if(i%j==0){flag=0;break;}
        }
        if(flag)return i;
    }
}

ll solve(int x,int y,int p){
    ll tmp1=Find(x);
    if(tmp1==x){
        ll tmp2=Find(y);
        ll tmp=tmp2;
        ll res=x;
        while(tmp){
            res*=10;
            tmp/=10;
        }
        return res+tmp2;
    }
    else{
        ll tmp2=Find(p);
        ll res=tmp1;
        ll tmp=tmp2;
        while(tmp){
            res*=10;
            tmp/=10;
        }
        return res+tmp2;
    }
}

ll solve(int x,int y){
        ll tmp1=Find(x);
        ll tmp2=Find(y);
        ll res=tmp1;
        ll tmp=tmp2;
        while(tmp){
            res*=10;
            tmp/=10;
        }
        return res+tmp2;
}

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    int tmp=m;
    int num=0;
    while(tmp){
        a[num++]=tmp%10;
        tmp/=10;
    }
    int flag=0;
    ll ans=INF;
    for(int i=n;i<=num-n;i++){
        flag=1;
        int tmp1=0;
        int j;
        int t;
        for(j=num-1,t=1;j>=0;j--,t++){
            tmp1=tmp1*10+a[j];
            if(t==i)break;
        }
        int tmp2=0;
        int mm=1;
        for(j--;j>=0;j--){
            tmp2=tmp2*10+a[j];
            mm*=10;
        }
        if(tmp2==0)tmp2=mm/10;
        ans=min(ans,solve(tmp1,tmp2,mm/10));
        ans=min(ans,solve(tmp1+1,mm/10));
    }
    if(flag==0){
        int nn=1;
        for(int i=1;i<n;i++)nn*=10;
        int tmpp=Find(nn);
        printf("%d%d",tmpp,tmpp);
    }
    else{
        printf("%lld\n",ans);
    }
    return 0;
}








 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章