zoukankan      html  css  js  c++  java
  • poj1811

    Given a big integer number, you are required to find out whether it's a prime number.

    Input

    The first line contains the number of test cases T (1 <= T <= 20 ), then the following T lines each contains an integer number N (2 <= N < 2 54).

    Output

    For each test case, if N is a prime number, output a line containing the word "Prime", otherwise, output a line containing the smallest prime factor of N.

    Sample Input

    2
    5
    10
    

    Sample Output

    Prime
    2


    //============================================================================
    // Name        : HDU3864.cpp
    // Author      : 
    // Version     :
    // Copyright   : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================
    
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <string.h>
    #include <time.h>
    using namespace std;
    const int S=15;
    long long mult_mod(long long a,long long b,long long c)
    {
        a%=c;
        b%=c;
        long long ret=0;
        while(b)
        {
            if(b&1){ret+=a;ret%=c;}
            a<<=1;
            if(a>=c)a%=c;
            b>>=1;
        }
        return ret;
    }
    long long pow_mod(long long x,long long n,long long mod)
    {
        if(n==1)return x%mod;
        x%=mod;
        long long tmp=x;
        long long ret=1;
        while(n)
        {
            if(n&1)ret=mult_mod(ret,tmp,mod);
            tmp=mult_mod(tmp,tmp,mod);
            n>>=1;
        }
        return ret;
    }
    long long check(long long a,long long n,long long x,long long t)
    {
        long long ret=pow_mod(a,x,n);
        long long 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(long long n)
    {
        if(n<2)return false;
        if(n==2)return true;
        if((n&1)==0)return false;
        long long x=n-1;
        long long t=0;
        while((x&1)==0){x>>=1;t++;}
        for(int i=0;i<S;i++)
        {
            long long a=rand()%(n-1)+1;
            if(check(a,n,x,t))
                return false;
        }
        return true;
    }
    
    long long factor[100];
    int tol;
    long long gcd(long long a,long long b)
    {
        if(a==0)return 1;
        if(a<0)return gcd(-a,b);
        while(b)
        {
            long long t=a%b;
            a=b;
            b=t;
        }
        return a;
    }
    
    long long Pollard_rho(long long x,long long c)
    {
        long long i=1,k=2;
        long long x0=rand()%x;
        long long y=x0;
        while(1)
        {
            i++;
            x0=(mult_mod(x0,x0,x)+c)%x;
            long long d=gcd(y-x0,x);
            if(d!=1&&d!=x)return d;
            if(y==x0)return x;
            if(i==k)
            {
                y=x0;
                k+=k;
            }
        }
    }
    
    void findfac(long long n)
    {
        if(Miller_Rabin(n))
        {
            factor[tol++]=n;
            return;
        }
        long long p=n;
        while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);
        findfac(p);
        findfac(n/p);
    }
    
    int main()
    {
        srand(time(NULL));
        long long n;
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%lld",&n);
            tol=0;
            findfac(n);
            if(tol==1)
            {
                printf("Prime
    ");
                continue;
            }
            sort(factor,factor+tol);
            printf("%lld
    ",factor[0]);
        }
        return 0;
    }
     
  • 相关阅读:
    CLR Via CSharp读书笔记(26)
    RH253读书笔记(10)-Appendix A Installing Software
    RH253读书笔记(9)-Lab 9 Account Management Methods
    一位程序员工作10年总结的13个忠告
    不跟你谈“五险一金”的老板,都是在耍流氓
    RH253读书笔记(8)-Lab 8 Securing Data
    RH253读书笔记(7)-Lab 7 Electronic Mail
    RH253读书笔记(6)-Lab 6 Implementing Web(HTTP) Services
    robotium测试创建java文件和junit文件区别
    LigerUi中为Grid表加上序号,并调整适当宽度!(实例)
  • 原文地址:https://www.cnblogs.com/ellery/p/11661899.html
Copyright © 2011-2022 走看看