LightOJ 1141 Program E
Description
In this problem, you are given an integer number s. You can transform any integer number A to another integer number B by adding x to A. This x is an integer number which is a prime factor of A (please note that 1 and A are not being considered as a factor of A). Now, your task is to find the minimum number of transformations required to transform s to another integer number t.
Input
Input starts with an integer T (≤ 500), denoting the number of test cases.
Each case contains two integers: s (1 ≤ s ≤ 100) and t (1 ≤ t ≤ 1000).
Output
For each case, print the case number and the minimum number of transformations needed. If it's impossible, then print -1.
Sample Input
2
6 12
6 13
Sample Output
Case 1: 2
Case 2: -1
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <queue>
- #include <cmath>
- using namespace std;
- #define MAX 0x3f3f3f3f
- typedef struct{
- int step;
- int now;
- }Node;
- int s, t;
- bool prime[1010];
- int marks[1001];
- int BFS(){
- queue<Node> q;
- Node start;
- start.now = s;
- start.step = 0;
- q.push( start );
- memset( marks, 0x3f, sizeof( marks ) );
- int ans = MAX;
- while( !q.empty() ){
- Node n = q.front();
- q.pop();
- if( n.now == t ){
- ans = min( ans, n.step );
- continue;
- }
- for( int i = 2; i < n.now; i++ ){
- if( n.now % i != 0 || !prime[i] ){
- continue;
- }
- if( n.now + i > t ){
- continue;
- }
- if( marks[n.now+i] > n.step + 1 ){
- Node temp;
- temp.now = n.now + i;
- temp.step = n.step + 1;
- marks[n.now+i] = temp.step;
- q.push( temp );
- }
- }
- }
- if( ans < MAX ){
- return ans;
- }
- return -1;
- }
- int main(){
- int T, Case = 1;
- memset( prime, true, sizeof( prime ) );
- for( int i = 2; i <= 1000; i++ ){
- for( int j = 2; i * j <= 1000; j++ ){
- prime[i*j] = false;
- }
- }
- cin >> T;
- while( T-- ){
- cin >> s >> t;
- cout << "Case " << Case++ << ": " << BFS() << endl;
- }
- return 0;
- }
LightOJ 1141 Program E的更多相关文章
- LightOJ 1141 Number Transformation
Number Transformation In this problem, you are given an integer number s. You can transform any inte ...
- ****ural 1141. RSA Attack(RSA加密,扩展欧几里得算法)
1141. RSA Attack Time limit: 1.0 secondMemory limit: 64 MB The RSA problem is the following: given a ...
- LightOj:1030-Discovering Gold(期望dp模板)
传送门:http://www.lightoj.com/volume_showproblem.php?problem=1030 Discovering Gold Time Limit: 2 second ...
- 九度OJ 1141:Financial Management (财务管理) (平均数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:939 解决:489 题目描述: Larry graduated this year and finally has a job. He's ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Solved: “Cannot execute a program. The command being executed was \roslyn\csc.exe”
When you publish your ASP.NET project to a hosting account such as GoDaddy, you may run into the iss ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- 关于The C compiler "arm-none-eabi-gcc" is not able to compile a simple test program. 的错误自省...
在 GCC ARM Embedded https://launchpad.net/gcc-arm-embedded/ 上面下载了个arm-none-eabi-gcc 用cmake 编译时 #指定C交叉 ...
- c中使用gets() 提示warning: this program uses gets(), which is unsafe.
今天在C代码中使用gets()时提示“warning: this program uses gets(), which is unsafe.”,然后这个程序还能运行,无聊的我开始查阅资料,为啥gets ...
随机推荐
- Ubuntu系统下面软件安装更新命令
在ubuntu服务器下安装包的时候,经常会用到sudo apt-get install 包名 或 sudo pip install 包名,那么两者有什么区别呢? 1.区别 pip用来安装来自PyPI( ...
- Oracle 删除重复的记录,只保留一条
查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 where Id in (select Id from 表 g ...
- 配置Java EE Eclipse+Tomcat开发环境
以下将详细介绍在Eclipse下搭建Java EE开发环境的每一步, 环境:Win 7 + JDK 1.7 + Eclipse IDE for Java EE Developers 3.7 +Tomc ...
- 各种U启网启什么的都是浮云
对于支持BIOS的电脑,优盘启动,网络启动的各种方案感觉都是浮云,从硬盘启动PE进行维护才是最可靠的.不点在开发wee的过程中给了我们很多维护的灵感,不用费劲地折腾fbinst/U+/量产/PXE/I ...
- robotframework笔记22
创建测试库 支持的编程语言 机器人框架本身是用写的 Python 和自然的测试 库扩展它可以使用相同的实现 语言. 运行时框架上 Jython ,图书馆也可以 实现使用 Java . 纯Python代 ...
- HTML5自学笔记[ 14 ]canvas绘图基础2
canvas绘制路径不仅可以绘制直线和多边形,还提供了绘制曲线的方法,利用这些方法可以画出多种曲线效果. 方法1:arc(x,y,r,起始弧度,结束弧度,绘制方向);其中(x,y)为圆心坐标,r为半径 ...
- C# System.Diagnostics.Stopwatch 类
测量一个时间间隔的运行时间 a.调用 Start 方法 b.调用 Stop 方法 c.使用 Elapsed 属性检查运行时间. 如: System.Diagnostics.Stopwatch stop ...
- python中的binascii
import binascii as B s = 'abcde' h = B.b2a_hex(s) # 字符串转16进制 '6162636465' h = B.hexlify(s) # 作用同上 s ...
- poj-------(2240)Arbitrage(最短路)
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15640 Accepted: 6563 Descri ...
- 初学java之菜单条,菜单,菜单项的设置
package project; import javax.swing.*; import java.awt.event.KeyEvent; import java.awt.event.InputEv ...