K Upside down primes German Collegiate Programming Contest 2015 [ Miller_Rabin ]

本文深入探讨了Upsidedown Primes的概念,即数字旋转180度后仍为素数的情况,同时介绍了如何使用Miller-Rabin算法进行高效的大数素性测试。通过具体的代码实现,展示了数据翻转和素性判断的过程。

题目链接:

K. Upside down primes

题意概括:

判断输入是否为素数,并且翻转180度后也为素数,翻转规则如下:

  • 翻转后6与9要互换
  • 3、4、7 翻转后无意义,当错误处理(无法组成数,自然就不是素数)
  • 其他数翻转后不改变

题解分析:

数据的读入和翻转不是问题,我的处理是直接用数组模拟,其实用 string 做会更好。有一点需要注意的是数据范围是1e16,所以都要用long long。

至于判断素数,其实本题用遍历到平方根的 O\left ( \sqrt{n} \right ) 算法也可以,但是看到大数第一反应还是用了Miller_Rabin算法(米勒拉宾素数判定法,概率素数测试算法,涉及费马小定理等许多数论),这些在之后的博客会详解,不再多提。

AC代码:

#include <stdio.h>
#include <iostream>
using namespace std;

typedef long long  ll;
const int T = 9;

ll mod_mult(ll a, ll b, ll mod) {           //大数乘法取模
    a %= mod;
    b %= mod;
    ll ans = 0;
    while (b) {
        if (b & 1) {
            ans += a;
            if (ans >= mod)
                ans -= mod;
        }
        a <<= 1;
        if (a >= mod) a = a - mod;
        b >>= 1;
    }
    return ans;
}

ll mod_pow(ll x, ll n, ll mod) {            //快速幂
    if (n == 0) return 1;
    ll res = mod_pow(mod_mult(x , x , mod), n / 2, mod);
    if (n & 1) res = mod_mult(res , x , mod);
    return res;
}


bool check(ll a, ll n, ll x, ll t) {       //来判断是不是素数
    ll ret = mod_pow(a, x, n), last = ret;
    for (int i = 1; i <= t; i ++) {
        ret = mod_mult(ret, ret, n);
        if (ret == 1 && last != 1 && last != n - 1) return true;//合数
        last = ret;
    }
    if (ret != 1) return true;
    else return false;
}

bool Miller_Rabin(ll n)         //Miller_Rabin算法
{
    if( n < 2) return false;
    if( n == 2) return true;
    if(!(n & 1)) return false;//偶数
    ll x = n - 1, t = 0;
    while (!(x & 1)) { x >>= 1; t++;}
    for (int i = 0; i < T; i ++) {
        ll a = rand() % (n - 1) + 1;
        if (check(a, n, x, t))
            return false;
    }
    return true;
}

int main(){

    int temp[17], cnt=0;
    ll n;
    bool flag = true;

    scanf("%lld",&n);
    
    if(flag = Miller_Rabin(n))
        while(n > 0) {
            temp[cnt] = n % 10;
            n = n / 10;
            if (temp[cnt] == 3 || temp[cnt] == 4 || temp[cnt] == 7)
            {flag = false; break;}
            else if(temp[cnt] == 6)
                temp[cnt] = 9;
            else if(temp[cnt] == 9)
                temp[cnt] = 6;

            cnt ++;
        }

    if(flag){
        for(int i = 0; i < cnt ; i ++)
            n = n * 10 + temp[i];
        if (!Miller_Rabin(n))
            flag = false;
    }
    if (flag)
        printf("yes\n");
    else printf("no\n");
}

                                       

                                                Upside down primes

Last night, I must have dropped my alarm clock. When the alarm went off in the morning, it showed 51:80 instead of 08:15. This made me realize that if you rotate a seven segment display like it is used in digital clocks by 180 degrees, some numbers still are numbers after turning them upside down.

My favourite numbers are primes, of course. Your job is to check whether a number is a prime and still a prime when   turned upside down.

Input Format

One line with the integer N in question \left ( 1\leq N\leq 10^{16} \right ).  N will not have leading zeros.

Output Format

Print one line of output containing "yes" if the number is a prime and still a prime if turned upside down, "no"  otherwise.

样例输入1

151

样例输出1

yes

样例输入2

23

样例输出2

no

样例输入3

18115211

样例输出3

no
### 安装MID360设备时的方向问题 在安装MID360设备时,确保其正确方向是非常重要的。虽然没有明确的文档指出MID360设备不能倒置安装,但通常情况下,这类设备设计为特定方向使用以保证最佳性能和安全性[^1]。 #### 正确安装方向 - **设备标识**:首先检查设备本身是否有任何标识或箭头指示安装方向。这些标识通常位于设备的外壳上,用于指导用户如何正确安装。 - **传感器位置**:MID360设备可能包含多个传感器,如加速度计、陀螺仪等。这些传感器的设计方向会影响数据采集的准确性。因此,按照制造商推荐的方向安装可以确保传感器正常工作[^1]。 - **安装指南**:查阅官方提供的安装手册或在线资源,了解具体的安装步骤和推荐的安装位置。制造商通常会提供详细的安装指南,包括推荐的安装角度和位置[^1]。 #### 倒置安装的可能性 如果确实需要将MID360设备倒置安装,建议先联系制造商的技术支持部门获取专业意见。某些设备可能允许通过软件配置来适应不同的安装方向,但这需要特定的设置和校准过程[^1]。 #### 校准与测试 无论采用何种安装方式,安装完成后都应进行一次全面的校准和测试,以确保所有功能均能正常运行。这包括但不限于: - **校准传感器**:使用配套软件对设备内的各种传感器进行校准。 - **功能测试**:检查所有功能是否按预期工作,特别是那些依赖于特定方向的功能。 ### 示例代码 ```python def check_device_orientation(orientation): """ 检查设备的安装方向是否正确。 参数: orientation (str): 设备的安装方向,例如 'upright' 或 'inverted'。 返回: str: 安装方向的状态信息。 """ if orientation == 'upright': return "设备已正确安装。" elif orientation == 'inverted': return "设备倒置安装,请确认是否符合制造商的要求。" else: return "未知的安装方向。" # 示例调用 print(check_device_orientation('upright')) # 输出: 设备已正确安装。 print(check_device_orientation('inverted')) # 输出: 设备倒置安装,请确认是否符合制造商的要求。 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值