G - Number Transformation(BFS+素数)
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+素数)的更多相关文章
- G - Number Transformation BFS
In this problem, you are given an integer number s. You can transform any integer number A to anothe ...
- LightOJ 1141 Number Transformation
Number Transformation In this problem, you are given an integer number s. You can transform any inte ...
- Codeforces 251C Number Transformation
Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define L ...
- hdu4952 Number Transformation (找规律)
2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...
- bzoj 3858: Number Transformation 暴力
3858: Number Transformation Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 82 Solved: 41[Submit][Sta ...
- HDU-4952 Number Transformation
http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/ ...
- CodeForces346 C. Number Transformation II
C. Number Transformation II time limit per test 1 second memory limit per test 256 megabytes input s ...
- CCPC2018-湖南全国邀请赛 G String Transformation
G.String Transformation 题目描述 Bobo has a string S = s1 s2...sn consists of letter a , b and c . He ca ...
- CodeForces 346C Number Transformation II
Number Transformation II 题解: 对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数. 也就是说 [ k * x[i] + 1, (k+1)* x ...
随机推荐
- python爬虫模拟登录验证码解决方案
[前言]几天研究验证码解决方案有三种吧.第一.手工输入,即保存图片后然后我们手工输入:第二.使用cookie,必须输入密码一次,获取cookie:第三.图像处理+深度学习方案,研究生也做相关课题,就用 ...
- vue3使用路由
下载 npm install vue-router@4 配置路由 暴露出一个createRouter方法,用来创建路由对象 通过defineAsyncComponent方法来实现路由的懒加载(文章1. ...
- 这是你没见过的不一样的redis
转: 这是你没见过的不一样的redis 提到Redis,大家一定会想到的几个点是什么呢? 高并发,KV存储,内存数据库,丰富的数据结构,单线程(6版本之前) 那么,接下来,上面提到的这些,都会一一给大 ...
- HDOJ-1560(迭代加深搜索问题)
DNA sequence HDOJ-1560 *本题是迭代加深搜索问题,主要是要理解题目,题目中一定是有解的,所以为了找最小的解,可以从小的搜索深度开始逐渐增加. *这里有个技巧就是,如果本次指定开始 ...
- Linux速通04 用户、群组、权限
用户及passwd文件 # /etc/passwd文件的功能:存储所有用户的相关信息,实际上是存放用户信息的数据库(database) # 各个字段的含义: * 第一个字段(列)记录的是这个用户的名字 ...
- 【Azure 微服务】PowerShell中,用Connect-ServiceFabricCluster命令无法连接到sf-test.chinaeast2.cloudapp.chinacloudapi.cn:19000 问题分析
问题描述 Azure Service Fabric提供了PowerShell的指令来进行创建,管理资源,如Get-ServiceFabricClusterHealth 获取当前集群的健康状态,但这些命 ...
- [HEOI2014] 人人尽说江南好
[HEOI2014] 人人尽说江南好 题目大意:一个博弈游戏,地上\(n\)堆石子,每堆石子有\(1\)个,每次可以合并任意两个石子堆\(a,b\),要求\(a + b \leq m\),问先手赢还是 ...
- 危害api收集
以下每一条代码,无论其通过什么方式被调用,在哪个类里被调用,传入什么参数,都具有唯一不变性(在逆向出来的的smali文件里),故可以作为匹配的凭证. 网络操作相关: Ljava/net/URL ...
- MySQL入门(5)——运算符
MySQL入门(5)--运算符 算术运算符 MySQL支持的算数运算符包括加.减.乘.除.求余. 符号 作用 + 加法运算 - 减法运算 * 乘法运算 / 除法运算 % 求余运算 DIV 除法运算,返 ...
- python的迭代
迭代 1:并行迭代 程序可以同时迭代两个序列 names["zhangsan","lisi","zhaosi"] age[12,13,14] ...