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

AC代码:

 1 #include<stdio.h>
2 #include<iostream>
3 #include<string.h>
4 #include<queue>
5 using namespace std;
6
7 int c[1010], vis[1010];
8 int a, b, f;
9
10 struct note
11 {
12 int x, s;
13 };
14
15 int prime()
16 {
17 memset(c, -1, sizeof(c));
18 c[0] = 0, c[1] = 0;
19 for(int i = 2; i*i < 1005; i++)
20 {
21 if(c[i])
22 for(int j = i+i; j < 1005; j += i)
23 c[j] = 0;
24 }
25 }
26
27 void bfs(int x)
28 {
29 queue<note>Q;
30 note p,q;
31 p.x = x;
32 p.s = 0;
33 vis[x] = 1;
34 Q.push(p);
35 while(!Q.empty())
36 {
37 p = Q.front();
38 Q.pop();
39 if(p.x == b)
40 {
41 f = p.s;
42 break;
43 }
44 for(int i = 2; i < p.x; i++)
45 {
46 if(c[i] == 0 || p.x + i > b || p.x % i != 0 || vis[p.x+i])
47 continue;
48 q.x = p.x + i;
49 vis[q.x] = 1;
50 q.s = p.s + 1;
51 Q.push(q);
52 }
53 }
54 return ;
55 }
56
57 int main()
58 {
59 int k = 0, flag = 0;
60 prime();
61
62 int t;
63 cin >> t;
64 while(t--)
65 {
66 f = -1;
67 memset(vis, 0, sizeof(vis));
68 cin >> a >> b;
69 bfs(a);
70 cout << "Case " << ++flag << ": " << f << endl;
71 }
72
73 return 0;
74 }

G - Number Transformation(BFS+素数)的更多相关文章

  1. G - Number Transformation BFS

    In this problem, you are given an integer number s. You can transform any integer number A to anothe ...

  2. LightOJ 1141 Number Transformation

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

  3. Codeforces 251C Number Transformation

    Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define L ...

  4. hdu4952 Number Transformation (找规律)

    2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...

  5. bzoj 3858: Number Transformation 暴力

    3858: Number Transformation Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 82  Solved: 41[Submit][Sta ...

  6. HDU-4952 Number Transformation

    http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/ ...

  7. CodeForces346 C. Number Transformation II

    C. Number Transformation II time limit per test 1 second memory limit per test 256 megabytes input s ...

  8. CCPC2018-湖南全国邀请赛 G String Transformation

    G.String Transformation 题目描述 Bobo has a string S = s1 s2...sn consists of letter a , b and c . He ca ...

  9. CodeForces 346C Number Transformation II

    Number Transformation II 题解: 对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数. 也就是说 [ k * x[i] + 1,  (k+1)* x ...

随机推荐

  1. SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理

    SpringBoot中配置字符编码一共有两种方式 方式一: 使用传统的Spring提供的字符编码过滤器(和第二种比较,此方式复杂,由于时间原因这里先不介绍了,后续补上) 方式二(推荐使用) 在appl ...

  2. DCL之单例模式

    所谓的DCL 就是 Double Check Lock,即双重锁定检查,在了解DCL在单例模式中如何应用之前,我们先了解一下单例模式.单例模式通常分为"饿汉"和"懒汉&q ...

  3. Debian中的NVIDIA显卡驱动安装——超简单,一行命令

    其实Debian的non-free固件中包含NVIDIA的显卡驱动,所以没必要在官网下run包一步一步来 sudo apt install nvidia-settings 安装时会提示与X冲突,没关系 ...

  4. vim下在插件emmet

    试了很多种方法,结果都没有用,最后找到了完美的解决方法! 1.方式1 1.1下载emmet并解压 1.2 cd /home/debian8/Downloads/emmet-vim-master/plu ...

  5. 《C++反汇编与逆向分析技术揭秘》--数据类型

      浮点数类型 IEEE标准从逻辑上采用一个三元组{S, E, M}来表示一个数N,它规定基数为2,符号位S用0和1分别表示正和负,尾数M用原码表示,阶码E用移码表示.根据浮点数的规格化方法,尾数域的 ...

  6. 攻防世界 reverse 进阶 9-re1-100

    9.re1-100 1 if ( numRead ) 2 { 3 if ( childCheckDebugResult() ) 4 { 5 responseFalse(); 6 } 7 else if ...

  7. 「免费开源」基于Vue和Quasar的前端SPA项目crudapi后台管理系统实战之自定义组件(四)

    基于Vue和Quasar的前端SPA项目实战之序列号(四) 回顾 通过上一篇文章 基于Vue和Quasar的前端SPA项目实战之布局菜单(三)的介绍,我们已经完成了布局菜单,本文主要介绍序列号功能的实 ...

  8. Starting Tomcat v9.0 Server at localhost' has encountered a problem

    •问题描述 在通过 Eclipse 打开 Tomcat 时报错: •解决方案 找到 Tomcat 的安装位置,打开 tomcat\bin 目录,找到 shutdown.bat,手动关闭 tomcat: ...

  9. 一键部署etcd集群管理脚本

    一.编写脚本 1 #!/bin/sh 2 # 安装 3 # ./run.sh etcd03 etcd01=http://192.168.2.44:2380,etcd02=http://192.168. ...

  10. Redis持久化——内存快照(RDB)

    最新:Redis持久化--如何选择合适的持久化方式 最新:Redis持久化--AOF日志 最新:Redis持久化--内存快照(RDB) 一文回顾Redis五大对象(数据类型) Redis对象--有序集 ...