zoukankan      html  css  js  c++  java
  • hdu 4548 初始化+二分 *

    题意:小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识。问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数。给定一个区间,你能计算出这个区间内有多少个美素数吗?

    链接:点我

    先找出所有美素数,然后二分找位置,我这里直接用stl了,位置相减即可,注意区间端点的判断

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 #include<map>
     8 using namespace std;
     9 #define MOD 1000000007
    10 #define pb(a) push_back(a)
    11 const int INF=0x3f3f3f3f;
    12 const double eps=1e-5;
    13 typedef long long ll;
    14 #define cl(a) memset(a,0,sizeof(a))
    15 #define ts printf("*****
    ");
    16 const int MAXN=1000013;
    17 int a[50000];
    18 int n,m,tt,cnt;
    19 bool notprime[MAXN];//值为false表示素数,值为true表示非素数
    20 void init()
    21 {
    22     memset(notprime,false,sizeof(notprime));
    23     notprime[0]=notprime[1]=true;
    24     for(int i=2;i<MAXN;i++)
    25     if(!notprime[i])
    26     {
    27         if(i>MAXN/i)continue;//防止后面i*i溢出(或者i,j用long long)
    28         //直接从i*i开始就可以,小于i倍的已经筛选过了,注意是j+=i
    29         for(int j=i*i;j<MAXN;j+=i)
    30         notprime[j]=true;
    31     }
    32 }
    33 bool fun(int x)
    34 {
    35     int w=x;
    36     int sum=0;
    37     while(w)
    38     {
    39         sum+=(w%10);
    40         w/=10;
    41     }
    42     if(!notprime[x]&&!notprime[sum])    return 1;
    43     else return 0;
    44 }
    45 int main()
    46 {
    47     int i,j,k;
    48     #ifndef ONLINE_JUDGE
    49     freopen("1.in","r",stdin);
    50     #endif
    51     a[0]=2;
    52     int tot=1;
    53     init();
    54     for(i=3;i<=1000011;i++)
    55     {
    56         if(fun(i))
    57         {
    58             a[tot++]=i;
    59         }
    60     }
    61     int tt;
    62     scanf("%d",&tt);
    63     int ca=0;
    64     while(tt--)
    65     {
    66         ca++;
    67         int aa,bb;
    68         scanf("%d%d",&aa,&bb);
    69         if(aa==bb)
    70         {
    71             if(fun(aa))
    72             {
    73                 printf("Case #%d: 1
    ",ca);
    74             }
    75             else
    76             {
    77                 printf("Case #%d: 0
    ",ca);
    78             }
    79             continue;
    80         }
    81         int pos1,pos2;
    82         int f1=fun(aa);
    83         int f2=fun(bb);
    84         pos1=lower_bound(a,a+30123,bb)-a;
    85         pos2=lower_bound(a,a+30123,aa)-a;
    86         //printf("%d %d
    ",pos1,pos2);
    87         int q;
    88         if((f1==0&&f2==1)||(f1==1&&f2==1))
    89         {
    90             q=1;
    91         }
    92         else q=0;
    93         printf("Case #%d: %d
    ",ca,pos1-pos2+q);
    94     }
    95 }
  • 相关阅读:
    关于产品那些事
    关于“编程的本质”的探讨
    分享一款在线贝塞尔曲线调试器
    HTML、CSS、JS对unicode字符的不同处理
    HTTP Content-Disposition Explanation [ from MDN ]
    认证 (authentication) 和授权 (authorization) 的区别
    事件驱动引擎会取代多线程编程吗
    你所不知道的JSON
    都有哪些特殊而实用的的搜索引擎?
    巨头们的GitHub仓库整理
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4574690.html
Copyright © 2011-2022 走看看