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 ...
随机推荐
- spring boot的 yml和properties的对比
Spring Boot 虽然做了大量的工作来简化配置,但其配置依然是相当的复杂!支持的外部配置方式就有很多种,笔者没有去统计,也许是为了灵活使用吧. application.yml 和 appli ...
- 微信小程序滚动条设置
隐藏滚动条 ::-webkit-scrollbar { width: 0rpx; height: 0rpx; background-color: transparent; } 其实设置为宽高为 0 或 ...
- CentOS6.4 Install oh-my-zsh
先安装zsh yum -y install zsh # 查看是否安装完成 cat /etc/shells /bin/sh /bin/bash /sbin/nologin /bin/dash /bin/ ...
- URL 地址解析
//虚拟目录的路径 Response.Write("<strong>Request.ApplicationPath:</strong>" + Request ...
- 大牛带你学会java类加载机制,不要错过,值得收藏!
很多人对java类加载机制都是非常抗拒的,因为这个太难理解了,但是我们作为一名优秀的java工程师,还是要把java类加载机制研究和学习明白的,因为这对于我们在以后的工作中有很大的帮助,因为它在jav ...
- python学习之基础内容
python基础内容① 什么是python? -一种计算机语言,计算机语言分为 -高级语言:python.java.Ruby.C#.C++...... -基础语言:C语言.汇编 -计算机可以直接执行基 ...
- 在CentOS上安装Nginx配置HTTPS并设置系统服务和开机启动(最全教程)
友情提示:全部配完大约需要20分钟,本教程配合 xshell 和 xftp 使用更佳. 系统配置:CentOS 7.5 本教程 摘繁华 版权所有. 操作按键 常用按键: 复制操作:Shift+Ins ...
- GreenDao3.2使用详解(增,删,改,查,升级)
首先看一下效果图: 项目结构如下图所示: 第一步:在build中添加配置如下: projet 目录下的build.gradle dependencies { classpath 'org.greenr ...
- Bug调试专项练习三笔记
前言:大家需要将文件夹中"有问题的代码" 导入到自己的工作空间中一. 训练一: 正确效果:首先要求大家导入给大家的项目, 给项目的"虚拟路径" 设定为" ...
- 基础篇:JAVA引用类型和ThreadLocal
前言 平时并发编程,除了维护修改共享变量的场景,有时我们也需要为每一个线程设置一个私有的变量,进行线程隔离,java提供的ThreadLocal可以帮助我们实现,而讲到ThreadLocal则不得不讲 ...