2428: Super Prime 求超级素数

本文介绍了一个算法问题——寻找指定序号的超级素数。超级素数是指从最低位开始的所有子串均为素数的整数。文章提供了一段C++代码实现,包括素数判断、超级素数生成等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2428: Super Prime


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K31388Standard

In these days, skywind had thought a series of new problem. But he must keep these problem for the coming important contests such as "JiLin Provincial Collegiate Programming Contest" and "The 2007 ACM Asia Programming Contest Changchun Site Network Preliminary Contest". This time, he brings out a easy problem.

Superprime is such integer that all part of it from lowest digit is prime. For example, 9547 is a superprime because 7, 47, 547 and 9547 is all prime. Given a index i, your task is to find the i-th superprime. Noting that in this problem, 1 is considered as a prime, but 2 is NOT.

Input and Output

Each line of input is a integer i, that you should print the i-th superprime in each line. The maximal superprime in output is less than 10^9;

Sample Input

1
2
212

Sample Output

1
3
9547

 

Problem Source: skywind

#include<iostream>
#include<cmath>
using namespace std;
int prime[40000],is[40000],flag[40000];
int pl;
int isprime(int n)
{
    double k=sqrt(n);
    for(int i=0;is[i]<=k;i++)
    {
        if(n%is[i]==0) return 0;
    }
    return 1;
}
int main()
{
    for(int i=2;i<40000;i++)
    {
        if(flag[i]==0) is[pl++]=i;
        for(int j=0;j<pl&&i*is[j]<40000;j++)
        {
            flag[i*is[j]]=1;
            if(i%is[j]==0) break;
        }
    }
    int base[10];
    base[1]=1;
    for(int i=2;i<10;i++) base[i]=base[i-1]*10;
    prime[0]=1;
    for(int i=1;i<4;i++) prime[i]=is[i];
    int temp_begin=0,temp_end=4,cnt=4;
    for(int t=2;t<10;t++)
    {
        for(int s=1;s<10;s++)
        {
            int p=s*base[t];
            for(int i=temp_begin;i<temp_end;i++)
            {
                int m=p+prime[i];
                if(isprime(m)) prime[cnt++]=m;
            }
        }
        temp_begin=temp_end;temp_end=cnt;
    }
    int n;
    while(cin>>n)
    {
        cout<<prime[n-1]<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值