旅行前的准备 (25分)

LX同学想要游遍整个中国甚至全世界!所以这个国庆假期她计划去长沙玩。但是在她做旅行前的准备的时候,她收到了老师的作业,并且要求在国庆假期结束之前上交!LX同学非常的生气,告诉了你这个消息。你也觉得实在是太过分了,但是没有办法,只好帮助LX同学完成她的作业。

老师给了LX同学两个整数,分别是 x 和 y 。每次LX同学可以从中选择一个数 num ,把这个数变成 (num+2) mod p 或 (num∗2) mod p 或 (num∗num) mod p ,请问,最少需要多少次操作,能使这两个整数相等。
输入格式:

一行三个正整数 x,y,p(3≤p≤10​6​​,1≤x,y<p) ,保证p 是一个奇数。
输出格式:

一行一个整数表示最少操作次数。
样例1
输入样例

3 4 5

输出样例

1

样例2
输入样例

2 3 5

输出样例

2

提示

样例一解释:3∗3≡4(mod5) 。

样例二解释:3∗2≡1(mod5) ,1∗2≡2(mod5) 。

这道题我用暴力bfs的做法只能过一部分的测试点,可能是哪里能有化吧,毕竟光是检测是否在队列中都需要进行set里的count方法,数据量又略大,可能还有啥更方便的标记已入队的方法吧.

25分的题只得了13分

//
// Created by TIGA_HUANG on 2020/10/6.
//

#include <iostream>
#include <climits>
#include <set>
#include <queue>

using namespace std;

int p, depth;

struct node {
    int x;
    int y;
    int layer;
};

bool operator<(const node &x, const node &y) {
    if (x.x < y.x) return true;
    return x.x == y.x && x.y < y.y;
}

int ans = INT_MAX;
set<node> inq;

void bfs(int x, int y) {
    queue<node> q;
    q.push({x, y});
    inq.insert({x, y});
    while (!q.empty()) {
        node t = q.front();
        int layer = t.layer;
        if (t.x == t.y) {
            cout << t.layer;
            return;
        }
        x = t.x % p;
        y = t.y % p;
        q.pop();
        t.x = (x % p + 2 % p) % p;
        t.y = y;
        t.layer = layer + 1;
        if (!inq.count(t)) {
            q.push(t);
            inq.insert(t);
        }
        t.x = (x % p + x % p) % p, t.y = y;
        if (!inq.count(t)) {
            q.push(t);
            inq.insert(t);
        }
        t.x = (x % p * x % p) % p, t.y = y;
        if (!inq.count(t)) {
            q.push(t);
            inq.insert(t);
        }
        t.x = x, t.y = (y % p + 2 % p) % p;
        if (!inq.count(t)) {
            q.push(t);
            inq.insert(t);
        }
        t.x = x, t.y = (y % p + y % p) % p;
        if (!inq.count(t)) {
            q.push(t);
            inq.insert(t);
        }
        t.x = x, t.y = (y % p * y % p) % p;
        if (!inq.count(t)) {
            q.push(t);
            inq.insert(t);
        }
    }
}

int main() {
    int x, y;
    cin >> x >> y >> p;
    bfs(x % p, y % p);
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值