• USACO 1.5 Superprime Rib


    Superprime Rib

    Butchering Farmer John's cows always yields the best prime rib. You can tell prime ribs by looking at the digits lovingly stamped across them, one by one, by FJ and the USDA. Farmer John ensures that a purchaser of his prime ribs gets really prime ribs because when sliced from the right, the numbers on the ribs continue to stay prime right down to the last rib, e.g.:

         7  3  3  1
    

    The set of ribs denoted by 7331 is prime; the three ribs 733 are prime; the two ribs 73 are prime, and, of course, the last rib, 7, is prime. The number 7331 is called a superprime of length 4.

    Write a program that accepts a number N 1 <=N<=8 of ribs and prints all the superprimes of that length.

    The number 1 (by itself) is not a prime number.

    PROGRAM NAME: sprime

    INPUT FORMAT

    A single line with the number N.

    SAMPLE INPUT (file sprime.in)

    4
    

    OUTPUT FORMAT

    The superprime ribs of length N, printed in ascending order one per line.

    SAMPLE OUTPUT (file sprime.out)

    2333
    2339
    2393
    2399
    2939
    3119
    3137
    3733
    3739
    3793
    3797
    5939
    7193
    7331
    7333
    7393


    题目大意:我们想要这样的数字,所有的前缀都是素数(质数),比如7193,从左边开始,7是质数,71是质数719是质数7193是质数,给定一个n(1到8),顺序输出长度为n的符合要求的所有的数。
    思路:很暴力的题目呀,数据很水,直接暴力枚举,然后check就好。但是暴力也有剪枝策略,首位只能是2,3,5,7,中的一个(因为第一个前缀就是单独的第一个字符,要求是素数),其他位只能是1,3,7,9中的一个(因为一定会成为某个前缀的结尾,然而如果结尾是偶数或者5的话就是2或者5的倍数就不是质数了),这样就可以很快得到答案。
      1 /*
      2 ID:fffgrdc1
      3 PROB:sprime
      4 LANG:C++
      5 */
      6 #include<cstdio>
      7 #include<iostream>
      8 #include<cmath>
      9 #include<cstring>
     10 using namespace std;
     11 int n;
     12 int temp=0;
     13 bool bo[10002];
     14 int cnt=0,prime[2000];
     15 const int maxn=10000;
     16 void init()
     17 {
     18     memset(bo,0,sizeof(bo));
     19     bo[0]=bo[1]=1;
     20     for(int i=2;i<maxn;i++)
     21     {
     22         if(!bo[i])
     23         {
     24             prime[++cnt]=i;
     25             for(int j=2;j*i<maxn;j++)
     26             {
     27                 bo[i*j]=1;
     28             }
     29         }
     30     }
     31 }
     32 bool check(int x)
     33 {
     34     if(x<=maxn)return !bo[x];
     35     int temp=sqrt(double(x));
     36     for(int i=1;i<=cnt&&prime[i]<=temp;i++)
     37     {
     38         if(!(x%prime[i]))return 0;
     39     }
     40     return 1;
     41 }
     42 void dfs(int x,int nown)
     43 {
     44     x++;
     45     int temp;
     46     if(x==1)
     47     {
     48         dfs(x,2);
     49         dfs(x,3);
     50         dfs(x,5);
     51         dfs(x,7);
     52     }
     53     else if(x==n)
     54     {
     55         temp=nown*10+1;
     56         if(check(temp))
     57             printf("%d
    ",temp);
     58         temp+=2;//==3
     59         if(check(temp))
     60             printf("%d
    ",temp);
     61         temp+=4;//==7
     62         if(check(temp))
     63             printf("%d
    ",temp);
     64         temp+=2;//==9
     65         if(check(temp))
     66             printf("%d
    ",temp);
     67         return ;
     68     }
     69     else
     70     {
     71         temp=nown*10+1;
     72         if(check(temp))
     73             dfs(x,temp);
     74         temp+=2;//==3
     75         if(check(temp))
     76             dfs(x,temp);
     77         temp+=4;//==7
     78         if(check(temp))
     79             dfs(x,temp);
     80         temp+=2;//==9
     81         if(check(temp))
     82             dfs(x,temp);
     83     }
     84 }
     85 int main()
     86 {
     87     freopen("sprime.in","r",stdin);
     88     freopen("sprime.out","w",stdout);
     89     init();
     90     scanf("%d",&n);
     91     if(n==1)
     92     {
     93         printf("2
    3
    5
    7
    ");
     94     }
     95     else
     96     {
     97         dfs(0,0);
     98     }
     99     return 0;
    100 }
    对了,借此机会学习了一下线性筛法,附上代码
     1 void init()
     2 {
     3     memset(bo,0,sizeof(bo));
     4     bo[0]=bo[1]=1;
     5     for(int i=2;i<maxn;i++)
     6     {
     7         if(!bo[i])prime[++cnt]=i;
     8         for(int j=1;j<=cnt;j++)
     9         {
    10             if(prime[j]*i>=maxn)break;
    11             bo[prime[j]*i]=1;
    12             if(!i%prime[j])break;
    13         }
    14     }
    15 }


    USACO section 1 结束了,大概就是各种模拟加普通的搜索,感觉没什么难度,继续努力。
  • 相关阅读:
    SSH的基本操作
    Aptana Studion出现 duplicate location重复定位报错
    centOS6.6虚拟机启动后登陆界面无法显示
    putty的基本使用
    Linux之如何进行固定IP、DNS等设置
    \无法访问。您可能没有权限使用网络资源。请与这台服务器的管理员联系以查明您是否有访问权限。找不到网络路径。解决方法和思路。
    访问共享时提示“你没有权限访问\192.168.0.100共享盘”解决方法?
    ESXI问题汇总
    【亲测】使用U盘安装ESXI6.7全过程以及遇到的问题记录
    U盘安装EXSi6.7出错menu.c32文件
  • 原文地址:https://www.cnblogs.com/xuwangzihao/p/5002627.html
Copyright © 2020-2023  润新知