zoukankan      html  css  js  c++  java
  • 杭电1795--The least one(判断素数)

    The least one

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 534    Accepted Submission(s): 203


    Problem Description
      In the RPG game “go back ice age”(I decide to develop the game after my undergraduate education), all heros have their own respected value, and the skill of killing monsters is defined as the following rule: one hero can kill the monstrers whose respected values are smaller then himself and the two respected values has none common factor but 1, so the skill is the same as the number of the monsters he can kill. Now each kind of value of the monsters come. And your hero have to kill at least M ones. To minimize the damage of the battle, you should dispatch a hero with minimal respected value. Which hero will you dispatch ? There are Q battles, in each battle, for i from 1 to Q, and your hero should kill Mi ones at least. You have all kind of heros with different respected values, and the values(heros’ and monsters’) are positive.
     
    Input
      The first line has one integer Q, then Q lines follow. In the Q lines there is an integer Mi, 0<Q<=1000000, 0<Mi<=10000.
     
    Output
      For each case, there are Q results, in each result, you should output the value of the hero you will dispatch to complete the task.
     
    Sample Input
    2
    3
    7
     
    Sample Output
    5
    11
     
    Author
    wangye
     
    Source
     
    Recommend
    wangye   |   We have carefully selected several similar problems for you:  1793 1796 1797 1794 1798 
    题意:杀怪兽游戏,英雄的能力要大于怪兽;并且两者能力都为质数,综合可得所求答案为 大于给出数的最小素数;
     1 #include <cstdio>
     2 #include <iostream>
     3 using namespace std;
     4 int i, j, sieve[10010];
     5 void is_prime()
     6 {
     7     for(i = 2; i < 10010; i++)
     8     {
     9         if(sieve[i] == 0)
    10             for(j = i*i; j < 10010; j+=i)
    11                 sieve[j] = 1;
    12     }
    13 }
    14 int main()
    15 {
    16     int m, t;
    17     is_prime(); 
    18     scanf("%d", &t);
    19     while(t--)
    20     {
    21         scanf("%d", &m);
    22         for(i = m + 1; i < 10010; i++)
    23             if(sieve[i] == 0)
    24                 break;
    25         printf("%d
    ", i); 
    26     }
    27     return 0;
    28 } 
     1 /*孪生素数: 所谓孪生素数指的是间隔为 2 的相邻素数,它们之间的距离已经近得不能再近了。
     2 
     3 
     4 
     5 
     6 若n≥6且n-1和n+1为孪生素数,那么n一定是6的倍数。
     7 证明:
     8 ∵ n-1和n+1是素数 ┈┈┈┈┈ ①
     9 ∴ n-1和n+1是奇数
    10 ∴ n是偶数,即n是2的倍数 ┈┈┈┈┈ ②
    11 假设n不是3的倍数,得:
    12 n=3x+1 或 n=3x+2,
    13 如果n=3x+1,则n-1=3x,与①违背,故n≠3x+1;
    14 如果n=3x+2,则n+1=3(x+1),与①违背,故n≠3x+2;
    15 ∴假设不成立,即n是3的倍数,又有②得结论:
    16 n是6的倍数。
    17 
    18 
    19 由上面的规律可以推出下面结论:
    20 若x≧1且n=6x-1或n=6x+1不是素数,那么n一定不是2和3的倍数。
    21 证明:
    22 ∵n=6x-1或n=6x+1,即n=2(3x)-1或n=2(3x)+1或n=3(2x)-1或n=3(2x)+1。
    23 ∴n一定不是2和3的倍数。
    24 
    25 
    26 素数出现规律:
    27 当n≧5时,如果n为素数,那么n mod 6 = 1 或 n mod 6 = 5,即n一定出现在6x(x≥1)两侧。
    28 证明:
    29 当x≥1时,有如下表示方法:
    30 ┈┈ 6x,6x+1,6x+2,6x+3,6x+4,6x+5,6(x+1),6(x+1)+1┈┈
    31 不在6x两侧的数为6x+2,6x+3,6x+4,即2(3x+1),3(2x+1),2(3x+2),它们一定不是素数,所以素数一定出现在6x的两侧。*/
    32 bool isPrime(int num)
    33 {
    34     if (num == 2 || num == 3)
    35     {
    36         return true;
    37     }
    38     if (num % 6 != 1 && num % 6 != 5)
    39     {
    40         return false;
    41     }
    42     for (int i = 5; i*i <= num; i += 6)
    43     {
    44         if (num % i == 0 || num % (i+2) == 0)
    45         {
    46             return false;
    47         }
    48     }
    49     return true;
    50 }
    高效判断素数方法
     
  • 相关阅读:
    python(内置高阶函数)
    cms 环境搭建
    cookie、session 和 token 区别
    接口用例设计
    python(字符编码与转码)
    从北斗卫星时钟(北斗校时器)发展纵论世界卫星导航新格局
    北斗授时系统(GPS授时设备)错一秒会怎样?京准电子科技
    北斗校时服务器(GPS时钟服务器)在电力调度系统应用
    GPS卫星时钟(北斗授时设备)在监狱管理系统方案
    NTP校时(网络对时服务器)IPC网络摄像机时钟同步
  • 原文地址:https://www.cnblogs.com/soTired/p/4695006.html
Copyright © 2011-2022 走看看