• 计蒜客 18492.Upside down primes-米勒拉宾判大素数 (German Collegiate Programming Contest 2015 ACM-ICPC Asia Training League 暑假第一阶段第三场 K)


     K. Upside down primes

    传送门

    这个题就是把大数按字符串输进去,判断一下是不是素数,然后反转180度,先判断反转之后的东西是不是一个数,如果是的话,再把这个数判一下是不是素数,如果都满足条件就yes。

    直接调用两次米勒拉宾判大素数就可以了。

    代码:

      1 //K-米勒拉宾判大素数
      2 #include<iostream>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<iomanip>
      6 #include<stdio.h>
      7 #include<stdlib.h>
      8 #include<math.h>
      9 #include<cstdlib>
     10 #include<set>
     11 #include<map>
     12 #include<ctime>
     13 #include<stack>
     14 #include<queue>
     15 #include<vector>
     16 #include<set>
     17 using namespace std;
     18 typedef long long ll;
     19 #define INF 0x7fffffff
     20 #define LIT 0x3f3f3f3f
     21 #define me(a,b) memset(a,b,sizeof(a))
     22 #define PI acos(-1.0)
     23 #define ios ios::sync_with_stdio(0),cin.tie(0);
     24 const int N=1e5+5;
     25 const int maxn=1e9;
     26 const int S=20;
     27 ll sw(ll a)
     28 {
     29     if(a==0||a==2||a==5||a==8||a==1)
     30         return a;
     31     if(a==6)
     32         return 9;
     33     if(a==9)
     34         return 6;
     35     else
     36         return -1;
     37 }
     38 ll mult_mod(ll a,ll b,ll mod)
     39 {
     40     a%=mod;b%=mod;
     41     ll ans=0;
     42     while(b)
     43     {
     44         if(b&1)
     45         {
     46             ans=ans+a;
     47             if(ans>=mod)
     48             ans=ans-mod;
     49         }
     50         a=a<<1;
     51         if(a>=mod) a=a-mod;
     52         b=b>>1;
     53     }
     54     return ans;
     55 }
     56 ll pow_mod(ll a,ll b,ll mod)
     57 {
     58     ll ans=1;
     59     a=a%mod;
     60     while(b)
     61     {
     62         if(b&1)
     63         {
     64             ans=mult_mod(ans,a,mod);
     65         }
     66         a=mult_mod(a,a,mod);
     67         b=b>>1;
     68     }
     69     return ans;
     70 }
     71 bool check(ll a,ll n,ll x,ll t)
     72 {
     73     ll ret=pow_mod(a,x,n);
     74     ll last=ret;
     75     for(int i=1;i<=t;i++)
     76     {
     77         ret=mult_mod(ret,ret,n);
     78         if(ret==1&&last!=1&&last!=n-1)
     79             return true;
     80         last=ret;
     81     }
     82     if(ret!=1) return true;
     83     else return false;
     84 }
     85 bool Miller_Rabin(ll n)
     86 {
     87     if(n<2)return false;
     88     if(n==2) return true;
     89     if((n&1)==0) return false;
     90     ll x=n-1,t=0;
     91     while((x&1)==0){x>>=1;t++;}
     92     for(int i=0;i<S;i++)
     93     {
     94         ll a=rand()%(n-1)+1;
     95         if(check(a,n,x,t))
     96             return false;
     97     }
     98     return true;
     99 }
    100 char ch[20];
    101 int main()
    102 {
    103     cin>>ch;
    104     ll n=0,bit=1;
    105     int len=strlen(ch);
    106     bool flag=false;
    107     for(int i=len-1;i>=0;i--)
    108     {
    109         n+=(ch[i]-'0')*bit;
    110         bit*=10;
    111     }
    112     if(!Miller_Rabin(n))
    113     {
    114         cout<<"no"<<endl;
    115         return 0;
    116     }
    117     n=0;bit=1;
    118     for(int i=0;i<len;i++){
    119         ll cnt=sw(ch[i]-'0');
    120         if(cnt==-1){
    121             cout<<"no"<<endl;
    122             return 0;
    123         }
    124         n+=cnt*bit;
    125         bit*=10;
    126     }
    127     if(Miller_Rabin(n))
    128         cout<<"yes"<<endl;
    129     else
    130         cout<<"no"<<endl;
    131 }

    就这些,没了。

    因为这场没找到数据结构的题,就和一个队友一起刚了数论题,这场主要是图论和数论。难过,图论选手不理我,是真的回家休息去了,上一场比赛的A题和这场的图论题都比较好,如果,你能看到的话,可以去看看这些题,正好和你最近学的算法有关系。

    唉,算了,都讨厌死我了。

    就这些,备忘一下,米勒拉宾有点厉害。

    最近在慢慢改代码风格,因为我发现队友们写代码都是把大括号对齐了写,现在再改回来。

    继续去爱我的线段树去了,我滚了。

  • 相关阅读:
    apache 访问出现403 Forbidden
    linux下用Apache一个IP多个域名建虚拟主机
    利用xargs 可以一次性卸载到位
    linux安装包地址备忘
    ii7安装php
    基于jQuery的对象切换插件:soChange 1.5 (点击下载)
    phpMyAdmin 缺少 mysqli 扩展。请检查 PHP 配置
    Android中的自动朗读(TTS)
    Android中的手势
    Android中的SQLiteOpenHelper类
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9729144.html
Copyright © 2020-2023  润新知