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 ...
随机推荐
- JAVA学习提高之----安装多个JDK版本的问题
我的机器上最开始安装的是jdk1.6,后来因为工作需要又安装了jdk1.4.但是,环境变量我并未更改,还是指向jdk1.6的路径的.可是,在cmd窗口输入 Java -version 却得到是1.4. ...
- CentOS查看CPU、内存、网络流量和磁盘 I/O【转载,待整理】
http://blog.csdn.net/zbyufei/article/details/6413273
- Linux 系统时间查看 及 时区修改(自动同步时间)
1:使用date命令查看时区 [root@db-server ~]# date -R Sun, 11 Jan 2015 07:10:28 -0800 [root@db-server ~]# ...
- 删除github账号的方法
如果你不想使用自己的github账号了,github官网允许你删除账号,具体操作步骤为: 1.登录github网站,网站右上角的头像,在下拉菜单中选择"Settings"(设置): ...
- C语言中的转义字符
转义字符 意义 ASCII码值(十进制) \a 响铃(BEL) 007 \b 退格(BS) ,将当前位置移到前一列 008 \f 换页(FF),将当前位置移到下页开头 012 \n 换行(LF) ,将 ...
- vc++编译libtiff4.0.4
目录 第1章简介 1 第2章命令行编译 2 2.1 编译 2 2.1.1 使用VC++2010编译 2 2.1.2 使用VC++6编译 4 2.2 生成的文件 5 ...
- media query ie8- 兼容实现总结
虽然说响应式设计的理想状态是,需对pc/移动各种终端进行响应:但是现实是高分辨率的pc端与手机终端屏幕相差太大,像电商这样有大量图片和文字信息的同时排版要求精准的页面,设计一个同时适应高分辨率pc又适 ...
- NodeJS 各websocket框架性能分析
For a current project at WhoScored, I needed to learn JavaScript, Node.js and WebSocket channel, aft ...
- css3中clip属性
clip 属性用来设置元素的形状.用来剪裁绝对定位元素. 当一幅图像的尺寸大于包含它的元素时,"clip" 属性允许规定一个元素的可见尺寸,这样此元素就会被修剪并显示在这个元素中. ...
- Unity Invoke 方法
Invoke() 方法是 Unity3D 的一种委托机制 如: Invoke("a", 5); 它的意思是:5 秒之后调用 a() 方法: 使用 Invoke() 方法需要注意 ...