zoukankan      html  css  js  c++  java
  • 【bzoj3667】Rabin-Miller算法

    3667: Rabin-Miller算法

    Time Limit: 60 Sec  Memory Limit: 512 MB
    Submit: 1200  Solved: 363
    [Submit][Status][Discuss]

    Input

    第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一个数字,保证在64位长整形范围内,并且没有负数。你需要对于每个数字:第一,检验是否是质数,是质数就输出Prime 
    第二,如果不是质数,输出它最大的质因子是哪个。 

    Output

    第一行CAS(CAS<=350,代表测试数据的组数) 
    以下CAS行:每行一个数字,保证是在64位长整形范围内的正数。 
    对于每组测试数据:输出Prime,代表它是质数,或者输出它最大的质因子,代表它是和数 

    Sample Input

    6
    2
    13
    134
    8897
    1234567654321
    1000000000000

    Sample Output

    Prime
    Prime
    67
    41
    4649
    5
     
     
     
    【吐槽】
    这是一道模板题,但是出了很玄学的错误,交到bzoj上一直wa。
    然后要到了数据,用cena评测,然后发现并没有错误。。。
    哪位大神知道这种玄学错误的话,欢迎指正,感激不尽。
     1 /*************
     2   bzoj 3667
     3   by chty
     4   2016.11.7
     5 *************/
     6 #include<iostream>
     7 #include<cstdio>
     8 #include<cstring>
     9 #include<cstdlib>
    10 #include<ctime>
    11 #include<cmath>
    12 #include<algorithm>
    13 using namespace std;
    14 typedef long long ll;
    15 const ll prime[20]={2,3,5,7,11,13,17,19,23};
    16 ll T,maxx;
    17 inline ll read()
    18 {
    19     ll x=0,f=1;  char ch=getchar();
    20     while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
    21     while(isdigit(ch))  {x=x*10+ch-'0';  ch=getchar();}
    22     return x*f;
    23 }
    24 ll gcd(ll a,ll b) {return !b?a:gcd(b,a%b);}
    25 ll mul(ll x,ll y,ll mod) {return ((x*y-(ll)(((long double)x*y+0.5)/mod)*mod)%mod+mod)%mod;}//一行快速乘
    26 ll fast(ll a,ll b,ll mod) {ll sum=1;for(;b;b>>=1,a=mul(a,a,mod))if(b&1)sum=mul(sum,a,mod);return sum;}
    27 bool Rabin_Miller(ll p,ll a)
    28 {
    29     if(p==2)  return 1;
    30     if((p&1==0)||p==1)  return 0;
    31     ll d=p-1;
    32     while(!(d&1))  d>>=1; 
    33     ll m=fast(a,d,p);
    34     if(m==1)  return 1;
    35     for(;d<p&&d>=0;m=mul(m,m,p),d<<=1)  {if(m==p-1)  return 1;}
    36     return 0;
    37 }
    38 bool isprime(ll x)
    39 {
    40     for(ll i=0;i<9;i++)
    41     {
    42         if(prime[i]==x)  return 1;
    43         if(!Rabin_Miller(x,prime[i]))  return 0;
    44     }
    45     return 1;
    46 }
    47 void Pollord_Rho(ll x)
    48 {
    49     if(isprime(x))  {maxx=max(maxx,x); return;}
    50     ll c=3;
    51     while(1)
    52     {
    53         ll x1(1),x2(1),i(1),k(2);
    54         while(1)
    55         {
    56             x1=(mul(x1,x1,x)+c)%x;
    57             ll d=gcd(abs(x1-x2),x);
    58             if(d>1&&d<x)
    59             {
    60                 Pollord_Rho(d);
    61                 Pollord_Rho(x/d);
    62                 return;
    63             }
    64             if(x1==x2)  break;
    65             if(++i==k)  k<<=1,x2=x1;
    66         }
    67         c++;
    68     }
    69 }
    70 void solve(ll n)
    71 {
    72     if(isprime(n))  {puts("Prime"); return;}
    73     maxx=0;
    74     Pollord_Rho(n);
    75     printf("%lld
    ",maxx);
    76 }
    77 int main()
    78 {
    79     freopen("cin.in","r",stdin);
    80     freopen("cout.out","w",stdout);
    81     T=read();
    82     for(ll i=1;i<=T;i++) {ll n=read();solve(n);}
    83     return 0;
    84 }
     
     
     
     
  • 相关阅读:
    CSS浮动(float、clear)通俗讲解
    JAVA 类的加载
    数据库操作 delete和truncate的区别
    正则表达式 匹配相同数字
    Oracle EBS OM 取消订单
    Oracle EBS OM 取消订单行
    Oracle EBS OM 已存在的OM订单增加物料
    Oracle EBS OM 创建订单
    Oracle EBS INV 创建物料搬运单头
    Oracle EBS INV 创建物料搬运单
  • 原文地址:https://www.cnblogs.com/chty/p/6040298.html
Copyright © 2011-2022 走看看