Number Clicker(双向bfs)

思路

  • 题意:给你u,v,p,总共三种变化方法,求最少经过多少次能使得u变成v
  • 做法:用双bfs,注意一下v变成u的时候变化的过程是反的,并且最后一个方法,也就是u^(p-2)%p,这一步是一个循环的过程,也就是说,其实u不断做这个计算过程,其实最后一定会进入一个循环,我这个菜鸡不知道怎么证明,自己写代码发现对于大多数情况是对的,如有大佬懂,请评论给下讲解,谢谢。

转载代码

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
int u, v, mod, nxt, st, ed, se;
vector<int> vv;//vv记下路径
map<int, int> vis1, vis2, fa, bu;
ll fastpow(ll base, ll n, ll mod) {//快速幂
	ll ans = 1;
	while (n) {
		if (n & 1) ans *= base % mod, ans %= mod;
		base *= base, base %= mod;
		n >>= 1;
	}
	return ans % mod;
}
void bfs() {
	queue<pair<int, int> > p, q;
	p.push(make_pair(u, 0)); vis1[u] = 1;//p存下从u开始的bfs,second记的是次数
	q.push(make_pair(v, 0)); vis2[v] = 1;//q存下从v开始的bfs,second记的是次数
	while (!p.empty() || !q.empty()) {
		if (!p.empty()) {
			int x = p.front().first;
			int y = p.front().second;
			p.pop();
			if (y >= 200) continue;
			//1
			nxt = (x + 1) % mod;
			if (vis2[nxt]) { st = x; ed = nxt; se = 1; break; }//st记录从u开始的是那个点,ed记录从v开始最后重合时候在那个点
			if (!vis1[nxt]) { vis1[nxt] = 1; fa[nxt] = x; bu[nxt] = 1; p.push(make_pair(nxt, y + 1)); }
			//2
			nxt = (x + mod - 1) % mod;
			if (vis2[nxt]) { st = x; ed = nxt; se = 2; break; }
			if (!vis1[nxt]) { vis1[nxt] = 1; fa[nxt] = x; bu[nxt] = 2; p.push(make_pair(nxt, y + 1)); }
			//3
			nxt = fastpow(x, mod - 2, mod);
			if (vis2[nxt]) { st = x; ed = nxt; se = 3; break; }
			if (!vis1[nxt]) { vis1[nxt] = 1; fa[nxt] = x; bu[nxt] = 3; p.push(make_pair(nxt, y + 1)); }
		}
		if (!q.empty()) {
			int x = q.front().first;
			int y = q.front().second;
			q.pop();
			if (y >= 200) continue;
			//1
			nxt = (x - 1 + mod) % mod;
			if (vis1[nxt]) { st = nxt; ed = x; se = 1; break; }
			if (!vis2[nxt]) { vis2[nxt] = 1; fa[nxt] = x; bu[nxt] = 1; q.push(make_pair(nxt, y + 1)); }
			//2
			nxt = (x + 1) % mod;
			if (vis1[nxt]) { st = nxt; ed = x; se = 2; break; }
			if (!vis2[nxt]) { vis2[nxt] = 1; fa[nxt] = x; bu[nxt] = 2; q.push(make_pair(nxt, y + 1)); }
			//3
			nxt = fastpow(x, mod - 2, mod);
			if (vis1[nxt]) { st = nxt; ed = x; se = 3; break; }
			if (!vis2[nxt]) { vis2[nxt] = 1; fa[nxt] = x; bu[nxt] = 3; q.push(make_pair(nxt, y + 1)); }
		}
	}
}
void findp(int x) {
	if (x == u) return;
	findp(fa[x]);
	vv.push_back(bu[x]);
}
void findq(int x) {
	if (x == v) return;
	vv.push_back(bu[x]);
	findq(fa[x]);
}
int main() {
	scanf("%d%d%d", &u, &v, &mod);
	bfs();
	findp(st);
	vv.push_back(se);
	findq(ed);
	int si = vv.size();
	printf("%d\n", si);
	for (int i = 0; i < si; i++) printf("%d ", vv[i]);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
from PyQt5.QtCore import QTimer from PyQt5.QtGui import QCursor from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton class MouseClicker(QWidget): def init(self): super().init() # 设置窗口标题 self.setWindowTitle("鼠标连点器") # 创建UI界面 self.label_count = QLabel("点击次数:") self.edit_count = QLineEdit() self.label_interval = QLabel("间隔时间(ms):") self.edit_interval = QLineEdit() self.btn_start = QPushButton("开始") # 设置布局 layout_count = QHBoxLayout() layout_count.addWidget(self.label_count) layout_count.addWidget(self.edit_count) layout_interval = QHBoxLayout() layout_interval.addWidget(self.label_interval) layout_interval.addWidget(self.edit_interval) layout_main = QVBoxLayout() layout_main.addLayout(layout_count) layout_main.addLayout(layout_interval) layout_main.addWidget(self.btn_start) self.setLayout(layout_main) # 连接按钮的点击事件 self.btn_start.clicked.connect(self.start_clicking) # 创建计时器 self.timer = QTimer() self.timer.timeout.connect(self.click) def start_clicking(self): # 获取点击次数和间隔时间 count = int(self.edit_count.text()) interval = int(self.edit_interval.text()) # 设置计时器的时间间隔 self.timer.setInterval(interval) # 开始计时器 self.timer.start() # 禁用开始按钮 self.btn_start.setEnabled(False) # 设置光标样式为等待 QApplication.setOverrideCursor(QCursor(QtCore.Qt.WaitCursor)) # 记录已经点击的次数 self.clicked_count = 0 def click(self): # 点击鼠标左键 QCursor().pos() QCursor().setPos(100, 100) QCursor().pos() QApplication.processEvents() # 增加已经点击的次数 self.clicked_count += 1 # 如果达到设定的点击次数,停止计时器并启用开始按钮 if self.clicked_count >= int(self.edit_count.text()): self.timer.stop() self.btn_start.setEnabled(True) # 设置光标样式为默认 QApplication.restoreOverrideCursor() if name == "main": app = QApplication([]) mouse_clicker = MouseClicker() mouse_clicker.show() app.exec_() 打包为可执行文件
05-15

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值