洛谷P5535【XR-3】小道消息

#洛谷P5535【XR-3】小道消息
在这里插入图片描述

##题意

给你一个n和k,其中n代表有n-1个人,其中这些人的衣服上有编号2-n。第0天你把小道消息给第k个人,也就是编号为k+1,第一天收到小道消息的人可以把小道消息传给衣服上的数字与传递小道消息的人的衣服上的数字互质的人,求最少需要多少天能把小道消息传给所有人?

##思路

当时看到题目的时候就想到了最大的次数就是2次,因为如果你第0天是奇数的话,那第一天所有穿偶数号码衣服的人都可以知道这个小道消息,那么第二天所有穿奇数号码衣服的人都可以知道这个小道消息,即所有人都可以知道这个小道消息
然后我就想到了质数问题,刚开始我就想到了如果第0天选的是一个质数,那么第1天所有人都可以知道这个小道消息,但是这样子第一个样例都过不了,因此我就想到了质数的倍数问题
题目中数据最大是1e14,所以我用了Miller Rabin的算法,但是一直有5个测试点没过,后面我用非常暴力的方法判断是否为素数就过了
如果有看到博客的人帮我看看我的Miller_Rabin代码错在哪

###没过的代码


    #include<bits/stdc++.h>
    
    using namespace std;
    
    typedef long long ll;
    typedef unsigned long long ull;
    
    const int maxn = 30;
    
    int modular_exp(int a, int m, int n) {
        if (m == 0) {
            return 1;
        }
        if (m == 1) {
            return a % n;
        }
        int w = modular_exp(a, m / 2, n);
        w = w * w % n;
        if (m & 1)
            w = w * a % n;
        return w;
    }
    
    bool Miller_Rabin(int n) {
        if (n == 2)
            return true;
        for (int i = 0; i < maxn; i++) {
            int a = rand() % (n - 2) + 2;
            if (modular_exp(a, n, n) != a) {
                return false;
            }
        }
        return true;
    }
    
    int main(void) {
    #ifdef ACM_LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
    
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
    
        srand(time(NULL));
        ll n, k;
        cin >> n >> k;
        if (Miller_Rabin(k + 1)) {
            if (n + 1 < (k + 1) * 2)
                cout << '1' << endl;
            else
                cout << '2' << endl;
        } else {
            cout << '2' << endl;
        }
    }

###过了的代码

    #include<bits/stdc++.h>
    
    using namespace std;
    
    typedef long long ll;
    typedef unsigned long long ull;
    
    const int maxn = 30;
    
    ll modular_exp(ll a, ll m, ll n) {
        if (m == 0) {
            return 1;
        }
        if (m == 1) {
            return a % n;
        }
        ll w = modular_exp(a, m / 2, n);
        w = w * w % n;
        if (m & 1)
            w = w * a % n;
        return w;
    }
    
    bool Miller_Rabin(ll n) {
        if (n == 2)
            return true;
        for (int i = 0; i < maxn; i++) {
            ll a = rand() % (n - 2) + 2;
            if (modular_exp(a, n, n) != a) {
                return false;
            }
        }
        return true;
    }
    
    bool judge(ll x){
        float n_sqrt;
        for(auto i = 2; i <= (int)sqrt(x); i++){
            if(x % i == 0){
                return false;
            }
        }
        return true;
    }
    
    int main(void) {
    #ifdef ACM_LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
    
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
    
        //srand(time(NULL));
        ll n, k;
        cin >> n >> k;
        if (judge(k + 1)) {
            if ((n + 1) < (k + 1) * 2)
                cout << '1' << endl;
            else
                cout << '2' << endl;
        } else {
            cout << '2' << endl;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值