POJ 3126 Prime Path

【回炉重造】POJ-3126.Prime Path 题解 【回炉重造】POJ-1426.Find The Multiple 题解 题目链接:POJ-1426.Find The Multiple 题目 题意 给你一个正整数 n ,让你找一个正整数 m m 必须能被 n 整除 m 是只由 0 和 1 这两种数字组成的 十进制 数 请输出 n 对应的 m 如果有多个结果输出任意一个即可 解题思路 深搜:【广搜要记录的东西太多可能超内存 题上说了不会超过100位,所以我们就定一个超过 100 位的 bitset 位容器 注意: 每一种情况都有判断条件,位置队列 阅读详情

Prime Path

https://vjudge.net/problem/POJ-3126
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.

Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on… Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033
1733
3733
3739
3779
8779
8179
The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

Input

One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

Output

One line for each case, either with a number stating the minimal cost or containing the word Impossible.

Sample Input

3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0

C++

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int MAXN = 10000 + 10;
bool isPrime[MAXN];
int prime[MAXN];
bool vis[MAXN];

void euler()
{
	int cnt = 0;
	for(int i = 2; i < MAXN; i ++){
		if(!isPrime[i])
			prime[cnt ++] = i;
		for(int j = 0; j < cnt && i * prime[j] < MAXN; j ++)
		{
			isPrime[i * prime[j]] = true;
			if(i % prime[j] == 0) break;
		}
	}
}

struct node{
	int num, step;
};

int bfs(int n, int m)
{
	queue<node> q;
	node now, next;
	now.num = n;
	now.step = 0;
	q.push(now);
	vis[n] = true;
	int a, b, c, d;
	while(!q.empty())
	{
		now = q.front();
		q.pop();
		if(now.num == m)
			return now.step;
		d = now.num % 10;
		c = now.num / 10 % 10;
		b = now.num / 100 % 10;
		a = now.num / 1000;
		next.step = now.step + 1;
		for(int i = 0; i < 4; i ++)
		{
			for(int j = 0; j <= 9; j ++)
			{
				if(i == 0 && j == 0) continue;
				if(i == 0)
					next.num = j * 1000 + b * 100 + c * 10 + d;
				else if(i == 1)
					next.num = a * 1000 + j * 100 + c * 10 + d;
				else if(i == 2)
					next.num = a * 1000 + b * 100 + j * 10 + d;
				else if(i == 3)
					next.num = a * 1000 + b * 100 + c * 10 + j;
				if(!isPrime[next.num] && !vis[next.num])
				{
					vis[next.num] = true;
					q.push(next);
				}
			}
		}		
	}
	return -1;
}

int main()
{
	int t, n, m;
	scanf("%d", &t);
	euler();
	while(t --)
	{
		scanf("%d%d", &n, &m);
		memset(vis, false, sizeof(vis));
		int ans = bfs(n, m);
		if(ans == -1) printf("Impossible\n");
		else printf("%d\n", ans);
	}
	return 0;
}
北大POJ3126-Prime Path编程题解析 素数在数学中扮演着重要角色,不仅因为它们是数论的基础,也是现代密码学的关键组成部分。素数的定义相对简单,但其性质却十分复杂,例如哥德巴赫猜想、孪生素数猜想等,都是数论中的未解问题。图由顶点(节点)和边组成,可以是有向的也可以是无向的。在有向图中,边的方向性很重要,而在无向图中边是双向的。图还可以分为有权图和无权图,有权图的边具有权重,表示连接两个节点的成本或距离。例如,社交网络可以看作是一个图,其中顶点代表用户,边代表用户之间的友谊关系。这个图是有向的,因为友谊关系可能是单向的。 阅读详情

相关推荐

hive sql每个用户的累积访问次数

我们有如下的用户访问数据 userId visitDate visitCount u01 2017/1/21 5 u02 2017/1/23 6 u03 2017/1/22 8 u04 2017/1/20 3 u01 2017/1/23 6 u01 2017/2/21 8 U02 2017/1/23 6 U01 2017/2/22 4 要求使用SQL...

weixin_39150719的博客 3480

E - Prime Path

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. — It is a matter of sec...

qq_44134712的博客 499

Win11系统下Abaqus2022与VS2022+oneAPI2023环境配置全攻略(附VUMAT子程序避坑指南)

本文详细指导在Windows 11系统下,为Abaqus 2022配置Visual Studio 2022与Intel oneAPI 2023的Fortran编译环境,以支持VUMAT等用户子程序开发。内容涵盖软件安装顺序、环境变量关联、快捷方式修改及关键验证步骤,并提供了首个VUMAT子程序的实战编写与模型验证方法,帮助用户高效搭建稳定可靠的仿真开发平台。

pytorch8learner的博客 313

【简单搜索】 POJ 3126 Prime Path详解

Prime Path: 题目链接:https://vjudge.net/problem/POJ-3126 The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. — It is a matter of sec

Qiuker_jl的博客 1603

termux 安装 rockylinux

往找最新的文件目录,选择ARM版本的image。或者直接查看目录或者国内阿里云镜像,按照cpu架构(安卓9手机一般是aarch64arm最新架构),获取镜像地址,以及checksum里的SHA256信息更新到以下rocky9.sh脚本,如果不需要直接使用即可。

欢迎来到别往的博客 983

基于立创EDA的IP5389BZ+IP6829 100W有线15W无线带屏充电宝DIY全流程解析

本文详细解析了基于立创EDA平台,采用IP5389BZ和IP6829芯片DIY一款100W有线快充、15W无线充电并带显示屏的移动电源的全过程。内容涵盖核心功能规划、硬件选型、电路设计、PCB布局、焊接组装、分步调试以及软件逻辑,为电子爱好者提供了一份从原理到实践的完整指南,并重点强调了电池安全与IP6829供电电压等关键注意事项。

weixin_34374684的博客 305

E - Prime Path(BFS+埃式筛)

E - Prime Path 题目 The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. — It is a matter of security to change such things every now

Mr.RainsdRop的胡思乱想 1336

【Qualcomm高通音频】音效调试_录音文件播放有杂音,如何定位原因?

一、录音播放数据处理过程介绍 (1)TX方向 麦克风(硅麦克风或者主机体麦克风)采集模拟音频信号,经过内部CODEC芯片中的adc转换为数字信号,在经过高通音频DSP处理音效后编码为wav/aac/amr/mp3等格式文件。 (2)RX方向 音频文件经过解码、重采样还原为pcm数据,经过DSP处理音效后送到内部CODEC,然后经过DAC转换为模拟信号,送到喇叭。 二、分析步骤 1...

crow_ch的博客 5971

搜索练习题I-09

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.  — It is a matter of s

snayf的博客 409

Jetson Nano摄像头选型与配置全攻略:CSI与USB接口实战指南

在嵌入式视觉与边缘计算项目中,摄像头作为核心传感器,其选型与配置直接影响系统性能。CSI(Camera Serial Interface)和USB(Universal Serial Bus)是两种主流接口,前者通过MIPI CSI-2协议直接与SoC通信,具备低延迟、高带宽特性,适合实时性要求高的应用;后者基于通用串行总线标准,即插即用,通用性强。在NVIDIA Jetson Nano平台上,合理选择摄像头接口并优化驱动配置,能充分发挥其GPU加速优势,提升图像处理流水线效率。本文聚焦Jetson Nano

weixin_29190651的博客 323

YOLO26改进策略【Neck】| arXiv 2025 分层多头注意力HMHA 子空间拆分提多样 + 通道重排降冗余,强化目标检测精度

HMHA(Hierarchical Multi-Head Attention,分层多头注意力)旨在解决传统多头注意力(MHA)在图像恢复任务中的核心缺陷——特征冗余。传统MHA将通道均匀拆分给各个头,导致不同头学习的子空间信息相似,进而出现多头关注同一区域、忽略退化区域修复的问题,限制了模型的特征表达能力。HMHA通过“分层子空间拆分+通道重排序”策略,让每个头学习差异化、互补的上下文特征,从根源上缓解冗余,提升图像恢复任务中对复杂退化场景的特征捕捉能力。

Limiiiing的博客 208

ayit第十四周训练f题

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. — It is a matter of security to change such things every now and then, to keep

m0_46628834的博客 565

webrtc+uniapp(h5和app)实现在线聊天功能 uniapp+网页+服务端源码

uniapp端代码 服务端代码 public/index.html

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 754

Prime Path(POJ 3126)解题报告

Prime Path(POJ 3126)解题报告 一、原题 Prime Path Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description The ministers of the cabinet were quite upset by the me

Dancinglikelink 973

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

【poj 3126】Prime Path 题意&题解&代码(C++)

poj

曾经追梦的happy时光 889

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

POJ 3126 Prime Path(素数路径)

POJ3126 Prime Path(素数路径) TimeLimit:1000MSMemoryLimit:65536K Description- 题目描述 The ministers of the cabinet were quite upset by the message from the Chief of Security stati...

470

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Poj 3126 Prime path

Poj 3126 Prime path 题意 两个四位数的素数A,B。改变A中的一位数,让它变成素数需要1镑,问由A变成B至少需要多少镑?这样的素数对一共有n组。 思路 这是一道bfs题,一定要记住标记已经访问的数,否则会超时。 代码 #include <cstdio> #include <queue> #include <cmath> struct N...

qq_33005379的博客 276

F - Prime Path POJ - 3126(36种情况的进阶BFS)

F - Prime Path POJ - 3126 The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offi...

石旭先森的博客 404

POJ3126 Prime Path

POJ3126 Prime Path 题意:只能通过素数到达想要的那个数,问你最短的路径; 解题思路:拥有所有素数,bfs 复盘过程:虽然是个基本的bfs的题,但想用dfs做,本来想借助dfs搞清楚他的迭代过程,结果搜索的分支十分混乱,不对,应该说迭代的太深了,想想dfs先序遍历的原理就知道不能如此,于是最后向bfs低头。明显基础题。。基础不牢,地动山摇! #include<stdio.h&...

weixin_43818893的博客 218

BFS POJ 3126 Prime Path

题目传送门 1 /* 2 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 3 BFS:先预处理1000到9999的素数,简单BFS一下。我没输出Impossible都AC,数据有点弱 4 */ 5 /************************************************ 6 Author ...

weixin_33717117的博客 137

poj3126 Prime Path

package bfs;import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;/**问题请参考http://poj.org/problem?id=3126 * @author rayli * @date:2014-7-17 上午9:18:47 * 给定两个四位素数a  b,要求把a变换到b 变换的过程

萝卜@兔子的专栏 337

POJ3126 Prime Path

http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 1033173337333739377987798179 分析:用字符串存m,n,这样改变各个位较方便 数组开大一点,开始数组开小了,结果就出错了 #include<stdi...

deyou1988的专栏 179
上一篇: POJ 1426 Find The Multiple
下一篇: POJ 3087 Shuffle'm Up
剑气扬名
博客等级 码龄7年 24粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值