zoukankan      html  css  js  c++  java
  • 51Nod 1181 质数中的质数(质数筛法)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181

    如果一个质数,在质数列表中的编号也是质数,那么就称之为质数中的质数。例如:3 5分别是排第2和第3的质数,所以他们是质数中的质数。现在给出一个数N,求>=N的最小的质数中的质数是多少(可以考虑用质数筛法来做)。
     
    Input
    输入一个数N(N <= 10^6)
    Output
    输出>=N的最小的质数中的质数。
    Input示例
    20
    Output示例
    31

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <string>
     6 #include <cmath>
     7 using namespace std;
     8 #define ll long long
     9 const int N=10005;
    10 const int mod=1e9+7;
    11 const int maxn=1e7;
    12 int prime[maxn];
    13 void getPrime()
    14 {
    15     memset(prime,0,sizeof(prime));
    16     for(int i=2;i<=maxn;i++){
    17         if(!prime[i]) prime[++prime[0]]=i;
    18         for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){
    19             prime[prime[j]*i]=1;
    20             if(i%prime[j]==0) break;
    21         }
    22     }
    23 }
    24 int main()
    25 {
    26     int n;
    27     getPrime();
    28     while(cin>>n){
    29         int flag=0;
    30         for(int i=1;i<=n;i++){
    31             if(prime[i]>=n){
    32                 flag=i;
    33                 break;
    34             }
    35         }
    36         for(int i=1;i<=flag;i++){
    37             if(prime[i]>=flag){
    38                 flag=i;
    39                 break;
    40             }
    41         }
    42         cout<<prime[prime[flag]]<<endl;
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    Codeforces 385C
    Codeforces 496C
    HDU 6114 Chess
    Codeforces 839B
    Codeforces 483B
    Codeforces 352B
    Codeforces 768B
    Codeforces 38B
    Codeforces 735B
    Codeforces 534B
  • 原文地址:https://www.cnblogs.com/shixinzei/p/7351216.html
Copyright © 2011-2022 走看看