zoukankan      html  css  js  c++  java
  • HDU 2138 How many prime numbers

    How many prime numbers

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2138
    64-bit integer IO format: %I64d      Java class name: Main
     Give you a lot of positive integers, just to find out how many prime numbers there are.
     

    Input

      There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.
     

    Output

      For each case, print the number of prime numbers you have found out.
     

    Sample Input

    3
    2 3 4

    Sample Output

    2

    Source

     
    解题:miller - rabbin 素性测试
    其实我们判的是伪素数,伪素数就是这个数很有可能是素数,但是不排除不是素数的可能,一旦miller rabin判断某个数不是素数,那么这个数一定不是素数,如果判断这个数是素数,则这个数是伪素数。由费马定理
    $a^{p-1} equiv 1(mod quad p)$
     那么我们可以把p-1表示成
    $p - 1 = d*2^{r}$
    那么很明显
     $a^{p-1} = a^{d*2^{r}}$
    如果
    $a^{d} equiv 1 (mod quad p)$ 
    那么
    $a^{d}$ $a^{2d} dots$ $a^{d*2^{r}}$
    都能模p余1
    所以p是伪素数,如果
    $a^{d}   otequiv 1(modquad p)$
    那么我们只需要看看
    $a^{d*2^{i}} modquad p == p-1qquad i < r$
    么,存在,那么
    $a^{p-1} equiv 1(modquad p)$
    成立,否则,这个数一定是合数
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 LL quickPow(LL a,LL d,LL n){
     5     LL ret = 1;
     6     while(d){
     7         if(d&1) ret = (ret*a)%n;
     8         d >>= 1;
     9         a = (a*a)%n;
    10     }
    11     return ret;
    12 }
    13 bool check(int a,int d,int n){
    14     if(n == a) return true;
    15     while(~d&1) d >>= 1;
    16     LL t = quickPow(a,d,n);
    17     while(d < n-1 && t != 1 && t != n-1){
    18         t = t*t%n;
    19         d <<= 1;
    20     }
    21     return (d&1) || t == n-1;
    22 }
    23 bool isP(int n){
    24     if(n == 2) return true;
    25     if(n < 2 || 0 == (n&1)) return false;
    26     static int p[3] = {2,3,61};
    27     for(int i = 0; i < 3; ++i)
    28         if(!check(p[i],n-1,n)) return false;
    29     return true;
    30 }
    31 int main(){
    32     int n,cnt,tmp;
    33     while(~scanf("%d",&n)){
    34         cnt = 0;
    35         while(n--){
    36             scanf("%d",&tmp);
    37             cnt += isP(tmp);
    38         }
    39         printf("%d
    ",cnt);
    40     }
    41     return 0;
    42 }
    View Code
  • 相关阅读:
    Spring中的Bean的配置形式
    使用外部属性文件配置Bean以及Bean的生命周期方法
    运行时找到main方法所在的类
    获取SpringMVC中所有RequestMapping映射URL信息
    RequestBody只能读取一次的问题
    接口标记为@ResponseBody却不进入ResponseBodyAdvice
    springboot打成jar包后无法解压
    Springboot打包执行源码解析
    关于base64的一个小细节
    Liquibase使用入门
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4497007.html
Copyright © 2011-2022 走看看