zoukankan      html  css  js  c++  java
  • 大素数判断(miller-Rabin测试)

    题目:PolandBall and Hypothesis

    A. PolandBall and Hypothesis
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    PolandBall is a young, clever Ball. He is interested in prime numbers. He has stated a following hypothesis: "There exists such a positive integer n that for each positive integer m number n·m + 1 is a prime number".

    Unfortunately, PolandBall is not experienced yet and doesn't know that his hypothesis is incorrect. Could you prove it wrong? Write a program that finds a counterexample for any n.

    Input

    The only number in the input is n (1 ≤ n ≤ 1000) — number from the PolandBall's hypothesis.

    Output

    Output such m that n·m + 1 is not a prime number. Your answer will be considered correct if you output any suitable m such that 1 ≤ m ≤ 103. It is guaranteed the the answer exists.

    Examples
    input
    3
    output
    1
    input
    4
    output
    2

    代码:
    #include <iostream>
    #include <time.h>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <set>
    #include <map>
    
    #define LL long long
    #define MAX 1<<30
    #define MIN -1<<30
    #define INIT0(a) memset((a), 0, sizeof(a))
    using namespace std; 
    
    const double PI = 3.14159265358979323;
    const double les = 0.00000005;
    const long N = 200000;   
    const int S=8;///判断几次 (8~12)  
    
    /// (a*b)%c  
    LL mult_mod(LL a,LL b,LL c)  
    {  
        a%=c; b%=c;  
        LL ret=0; LL temp=a;  
        while(b)  
        {  
            if(b&1)  
            {  
                ret+=temp;  
                if(ret>c) ret-=c;  
            }  
            temp<<=1;  
            if(temp>c) temp-=c;  
            b>>=1LL;  
        }  
        return ret;  
    }  
      
    /// (a^n)%mod  
    LL pow_mod(LL a,LL n,LL mod)  
    {  
        LL ret=1;  
        LL temp=a%mod;  
        while(n)  
        {  
            if(n&1) ret=mult_mod(ret,temp,mod);  
            temp=mult_mod(temp,temp,mod);  
            n>>=1LL;  
        }  
        return ret;  
    }  
      
    /// check a^(n-1)==1(mod n)  
    bool check(LL a,LL n,LL x,LL t)  
    {  
        LL ret=pow_mod(a,x,n);  
        LL last=ret;  
        for(int i=1;i<=t;i++)  
        {  
            ret=mult_mod(ret,ret,n);  
            if(ret==1&&last!=1&&last!=n-1) return true;  
            last=ret;  
        }  
        if(ret!=1) return true;  
        return false;  
    }  
      
    bool Miller_Rabin(LL n)  
    {  
        if(n<2) return false;  
        if(n==2) return true;  
        if((n&1)==0) return false;  
        LL x=n-1;  
        LL t=0;  
        while((x&1)==0) { x>>=1; t++;}  
        srand(time(NULL));  
      
        for(int i=0;i<S;i++)  
        {  
            LL a=rand()%(n-1)+1;  
            if(check(a,n,x,t)) return false;  
        }  
        return true;  
    }  
    
    int main(){
        // freopen("input1.txt", "r", stdin);
        // freopen("output1.txt", "w", stdout);
        int n;
        cin>>n;
        
        for (int i = 1; i <= 1000; ++i)
        {
            if(!Miller_Rabin(n*i+1)){
                cout<<i<<endl;
                return 0;
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    浅谈Huffman树
    CF884D:Boxes And Balls
    MySQL单表查询(重要)
    MySQL字段完整性约束(重要)
    MySQL数据类型(重要)
    数据库基本操作
    MySQL权限管理
    MySQL存储引擎概述
    数据库基础
    并发编程小结
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/6296913.html
Copyright © 2011-2022 走看看