1156 Sexy Primes (20)

打印 上一主题 下一主题

主题 1048|帖子 1048|积分 3144

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Sexy primes are pairs of primes of the form (p, p+6), so-named since "sex" is the Latin word for "six". (Quoted from http://mathworld.wolfram.com/SexyPrimes.html)
Now given an integer, you are supposed to tell if it is a sexy prime.
Input Specification:

Each input file contains one test case. Each case gives a positive integer N (≤10^8).
Output Specification:

For each case, print in a line Yes if N is a sexy prime, then print in the next line the other sexy prime paired with N (if the answer is not unique, output the smaller number). Or if N is not a sexy prime, print No instead, then print in the next line the smallest sexy prime which is larger than N.
Sample Input 1:

  1. 47
复制代码
Sample Output 1:

  1. Yes
  2. 41
复制代码
Sample Input 2:

  1. 21
复制代码
Sample Output 2:

  1. No
  2. 23
复制代码
 标题大意:性感素数是指形如 (p, p+6) 这样的一对素数。给定一个整数,判定其是否为一个性感素数。若N是一个性感素数,则在一行中输出Yes,并在第二行输出与N配对的另一个性感素数(若配对的数不唯一,输出较小的谁人)。若N不是性感素数,则在一行中输出No,然后在第二行输出大于N的最小性感素数。
分析:分别判定p和p-6,p和p+6是否同时为素数,假如是,则为性感素数输出Yes。不是的话就从p+1往后找到第一个满足要求的数。
  1. #include<algorithm>
  2. #include <iostream>
  3. #include  <cstdlib>
  4. #include  <cstring>
  5. #include   <string>
  6. #include   <vector>
  7. #include   <cstdio>
  8. #include    <queue>
  9. #include    <stack>
  10. #include    <ctime>
  11. #include    <cmath>
  12. #include      <map>
  13. #include      <set>
  14. #define INF 0xffffffff
  15. #define db1(x) cout<<#x<<"="<<(x)<<endl
  16. #define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
  17. #define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
  18. #define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
  19. #define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
  20. using namespace std;
  21. int num[1000005]={1,1},prime[100000];
  22. int getprime()
  23. {
  24.     int t=0;
  25.     for(int i=2;i<=1000;++i)
  26.         if(num[i]==0)
  27.             for(int j=i*i;j<=1000000;j+=i)
  28.                 num[j]=1;
  29.     for(int i=2;i<=1000000;++i)
  30.         if(num[i]==0)prime[t++]=i;
  31.     return t;
  32. }
  33. bool judge(int a,int t)
  34. {
  35.     if(a==1)return 0;
  36.     for(int i=0;i<t;++i)
  37.         if(a%prime[i]==0&&a!=prime[i])return 0;
  38.     return 1;
  39. }
  40. void getans(int n,int t)
  41. {
  42.     while(n)
  43.     {
  44.         if(judge(n,t)==1)
  45.         {
  46.             if(judge(n-6,t)==1)
  47.             {
  48.                 printf("No\n%d\n",n);
  49.                 return;
  50.             }
  51.             else if(judge(n+6,t)==1)
  52.             {
  53.                 printf("No\n%d\n",n);
  54.                 return;
  55.             }
  56.         }
  57.         n++;
  58.     }
  59.     return;
  60. }
  61. int main(void)
  62. {
  63.     #ifdef test
  64.     freopen("in.txt","r",stdin);
  65.     //freopen("in.txt","w",stdout);
  66.     clock_t start=clock();
  67.     #endif //test
  68.     int cnt=getprime();
  69.     int n,ans=0;scanf("%d",&n);
  70.     bool f=judge(n,cnt),f1;
  71.     if(f)
  72.     {
  73.         f1=judge(n-6,cnt);
  74.         if(!f1)
  75.         {
  76.             f1=judge(n+6,cnt);
  77.             if(f1)printf("Yes\n%d\n",n+6);
  78.             else getans(n+1,cnt);
  79.         }
  80.         else printf("Yes\n%d\n",n-6);
  81.     }
  82.     else getans(n+1,cnt);
  83.     #ifdef test
  84.     clockid_t end=clock();
  85.     double endtime=(double)(end-start)/CLOCKS_PER_SEC;
  86.     printf("\n\n\n\n\n");
  87.     cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
  88.     cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
  89.     #endif //test
  90.     return 0;
  91. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

圆咕噜咕噜

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表