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

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <cmath>
  7. using namespace std;
  8. #define MAX 0x3f3f3f3f
  9. typedef struct{
  10. int step;
  11. int now;
  12. }Node;
  13. int s, t;
  14. bool prime[1010];
  15. int marks[1001];
  16. int BFS(){
  17. queue<Node> q;
  18. Node start;
  19. start.now = s;
  20. start.step = 0;
  21. q.push( start );
  22. memset( marks, 0x3f, sizeof( marks ) );
  23. int ans = MAX;
  24. while( !q.empty() ){
  25. Node n = q.front();
  26. q.pop();
  27. if( n.now == t ){
  28. ans = min( ans, n.step );
  29. continue;
  30. }
  31. for( int i = 2; i < n.now; i++ ){
  32. if( n.now % i != 0 || !prime[i] ){
  33. continue;
  34. }
  35. if( n.now + i > t ){
  36. continue;
  37. }
  38. if( marks[n.now+i] > n.step + 1 ){
  39. Node temp;
  40. temp.now = n.now + i;
  41. temp.step = n.step + 1;
  42. marks[n.now+i] = temp.step;
  43. q.push( temp );
  44. }
  45. }
  46. }
  47. if( ans < MAX ){
  48. return ans;
  49. }
  50. return -1;
  51. }
  52. int main(){
  53. int T, Case = 1;
  54. memset( prime, true, sizeof( prime ) );
  55. for( int i = 2; i <= 1000; i++ ){
  56. for( int j = 2; i * j <= 1000; j++ ){
  57. prime[i*j] = false;
  58. }
  59. }
  60. cin >> T;
  61. while( T-- ){
  62. cin >> s >> t;
  63. cout << "Case " << Case++ << ": " << BFS() << endl;
  64. }
  65. return 0;
  66. }

LightOJ 1141 Program E的更多相关文章

  1. LightOJ 1141 Number Transformation

    Number Transformation In this problem, you are given an integer number s. You can transform any inte ...

  2. ****ural 1141. RSA Attack(RSA加密,扩展欧几里得算法)

    1141. RSA Attack Time limit: 1.0 secondMemory limit: 64 MB The RSA problem is the following: given a ...

  3. LightOj:1030-Discovering Gold(期望dp模板)

    传送门:http://www.lightoj.com/volume_showproblem.php?problem=1030 Discovering Gold Time Limit: 2 second ...

  4. 九度OJ 1141:Financial Management (财务管理) (平均数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:939 解决:489 题目描述: Larry graduated this year and finally has a job. He's ...

  5. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  6. 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 ...

  7. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  8. 关于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交叉 ...

  9. c中使用gets() 提示warning: this program uses gets(), which is unsafe.

    今天在C代码中使用gets()时提示“warning: this program uses gets(), which is unsafe.”,然后这个程序还能运行,无聊的我开始查阅资料,为啥gets ...

随机推荐

  1. Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处http://blog.csdn.net/xiaanming/article/details ...

  2. android studio导入 so ,jar 文件。

    环境为: Android Studio 1.0.2 如果是jar文件的话,请直接拷贝jar文件到项目的libs文件夹下,然后运行:Sync Project with Gradle Files.如下图2 ...

  3. bottomNavigationBar 底部导航tab MD

    1.先上图: 此底部Tab完全可以满足日常的开发 2.使用: 很简单,使用Gradle构建:compile ‘com.ashokvarma.android:bottom-navigation-bar: ...

  4. Unity5中叹为观止的实时GI效果

    http://www.manew.com/thread-43970-1-1.html 今天为大家分享unity与Alex Lovett共同使用unity5制作的Shrine Arch-viz Demo ...

  5. 【CodeForces 651A】Joysticks 模拟

    题意:给定a,b,每个单位时间可以将a,b中一台加1,一台减2,求最久可以支持多久. #include <cstdio> #include <algorithm> using ...

  6. pvresize - Unix, Linux Command

    NAME pvresize - resize a disk or partition in use by LVM2 SYNOPSIS pvresize [-d|--debug] [-h|--help] ...

  7. [maven] 常用仓库地址

    共有的仓库 http://mvnrepository.com/ http://repo1.maven.org/maven2/ http://repository.jboss.com/maven2/ h ...

  8. Shell基础:常用技巧&重定向&管道操作

    Shell脚本介绍和常用工具 Shell脚本 Shell脚本:实际就是windows里的批处理脚本,多条可一次执行的Shell命令集合.Linux上的脚本可以用很多种语言实现,bash shell是比 ...

  9. debug && release

    http://www.cnblogs.com/awpatp/archive/2009/11/05/1597038.html Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调 ...

  10. 多分类问题中,实现不同分类区域颜色填充的MATLAB代码(demo:Random Forest)

    之前建立了一个SVM-based Ordinal regression模型,一种特殊的多分类模型,就想通过可视化的方式展示模型分类的效果,对各个分类区域用不同颜色表示.可是,也看了很多代码,但基本都是 ...