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 ...
随机推荐
- [js] 函数节流
原文链接:http://www.alloyteam.com/2012/11/javascript-throttle/
- hdu5787(数位dp)
基础的数位dp,才发现今天才终于彻底搞懂了数位dp... // // main.cpp // hdu5787.1 // // Created by New_Life on 16/8/10. // Co ...
- Sqlserver_In、exists使用
in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,每次loop 循环再对内表进行查询. 一直以来认为exists 比in 效率高的说法是不准确的.如果查询的两个表大小相当, ...
- hiho_100_八数码
题目大意 8数码问题,在3x3的矩阵中填入0-8九个数字,0可以和它相邻的数字进行交换.从初始状态到达状态F(3x3的方格从上到下,从左到右,形成的数字串为123456780)所需要最少移动的次数. ...
- Libsvm:脚本(subset.py、grid.py、checkdata.py) | MATLAB/OCTAVE interface | Python interface
1.脚本 This directory includes some useful codes: 1. subset selection tools. (子集抽取工具) subset.py 2. par ...
- iOS蓝牙4.0开发(BLE)
智能设备 和 app 通过 BLE通讯的两种模型 模型一:设备提供数据,app 展示数据: 比如小米手环 模型二:app提供数据,设备接收: 模型与corebluetooth的对应关系: 模型一:智能 ...
- 安卓App热补丁动态修复技术介绍
版权声明:本文由johncz原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/169 来源:腾云阁 https://www.q ...
- Git 分支管理是一门艺术
转载: Git 分支管理是一门艺术 1 要确保:团队成员从主分支(master)获得的都是处于可发布状态的代码,而从开发分支(develop)应该总能够获得最新开发进展的代码. 2 "辅助分 ...
- 用java实现冒泡排序法
一.基本思路: 冒泡排序是一种简单的交换类排序.其基本思路是,从头开始扫描待排序的元素,在扫描过程中依次对相邻元素进行比较,将关键字值大的元素后移.每经过一趟排序后,关键字值最大的元素将移到末尾,此时 ...
- DSP28377S - ADC学习编程笔记
DSP28377S - ADC学习编程笔记 彭会锋 2016-08-04 20:19:52 1 ADC类型导致的配置区别 F28377S的ADC类型是Type 4类型,我的理解是不同类型的ADC采 ...