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
思路:一开始理解错了, 这个题目的就是给你个整数s, 加他的素质数,以最小的次数转换到t,能转换为t输出叠加次数,否则输出-1;注意:这里的是素质数会随着s的而改变,即素质数的数组是变化的。
这个题目可以理解为 一个x数轴从S点,每次加上他的素质数,是否能得到t点;
AC代码
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int N=;
vector<int >arr; struct stu{
int a;
int s;
}e1,e2,e3; int pri[N]={,,};
int n,m;
int mark[N]={}; int prime(){//将1010以内的素数打个表
for(int i=;i*i<=N;i++){
if(!pri[i])
for(int j=i+i;j<=N;j+=i){
pri[j]=;
}
}
} void f(int x){//寻找素质数
for(int i=;i<x;i++){
if(x%i==&&pri[i]==){
arr.push_back(i);
}
}
} int bfs(int n,int m){// 起点与终点
memset(mark,,sizeof(mark));
queue<stu>que;
e1.a=n;
e1.s=;
que.push(e1);
mark[n]=;
while(que.size()){
e2=que.front();
que.pop();
arr.clear();
f(e2.a);//更新素质数的数组
if(arr.size()==)
continue ;
for(int i=;i<arr.size();i++){
e3.a=e2.a+arr[i];
if(mark[e3.a]!=&&e3.a>=&&e3.a<=m){
mark[e3.a]=;
if(e3.a==m) return e2.s+;
else {
e3.s=e2.s+;
que.push(e3);
}
}
}
}
return -;
} int main()
{
prime();
int t;
cin>>t;
for(int i=;i<=t;i++){
cin>>n>>m;
if(m-n==)
{
int a=;
printf("Case %d: %d\n",i,a);
continue ;
}
else if(n>m||m-n==)//n若比M小或者相差为1 直接 -1;
{
int a=-;
printf("Case %d: %d\n",i,a);
continue ; }
int x=bfs(n,m);
if(x==-)
{
printf("Case %d: %d\n",i,x);
}
else {
printf("Case %d: %d\n",i,x);
}
} return ;
}
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 ...
- Codeforces 251C Number Transformation
Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define L ...
- LightOJ 1141 Number Transformation
Number Transformation In this problem, you are given an integer number s. You can transform any inte ...
- 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 ...
随机推荐
- hdu3294 Manacher算法模板
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3294/ 回文长度如果是mxx,回文中心是id的话,在扩展串中(id-mxx+1,id+mxx-1)的这段中去除标 ...
- hdu1686kmp果题
kmp字符串匹配原理参考博客:https://blog.csdn.net/bqw18744018044/article/details/90516750 代码如下:(写一遍模板) #include&l ...
- Building Applications with Force.com and VisualForce Dev 401-001(一):Introduction
Dev 401-001:Introduction Module Objectives1.Orient yourselves to the training location.2.Understan ...
- Python第六章-函数04-递归函数和拉姆达表达式
五.递归函数 什么叫递归(recusive)? 你拿两个镜子互相面对着, 然后去看镜子, 会发现每个镜子中很多个镜子, 层层的嵌套, 无穷尽, 这就是一种递归! 从前有坐山, 山里有座庙, 庙里有个老 ...
- Colab笔记本能用英伟达Tesla T4了,谷歌的羊毛薅到酸爽
谷歌出品的Colab笔记本,机器学习界薅羊毛神器,如今又有了新福利: 连英伟达最新一代机器学习GPU:Tesla T4都能免费蹭,穷苦羊毛党也顿时高端了起来. 英伟达的Tesla T4,是去年秋天才发 ...
- .NET 5.0 Preview 2发布
2020年4月2日微软.NET 团队的项目经理 Richard 在博客上 发布了.NET 5 Preview 2:https://devblogs.microsoft.com/dotnet/annou ...
- HashMap中使用自定义类作为Key时,为何要重写HashCode和Equals方法
之前一直不是很理解为什么要重写HashCode和Equals方法,才只能作为键值存储在HashMap中.通过下文,可以一探究竟. 首先,如果我们直接用以下的Person类作为键,存入HashMap中, ...
- css指示箭头两种实现方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- twoway 系列命令绘制地图
twoway 系列命令绘制地图 ❝ 在谈到用stata绘制地图时,很多人首先想到的是spmap命令.其实,最常见的twoway系列命令就可以完成我们的大多数绘图需求. 对于spmap命令的使用,在线资 ...
- P1044 栈(递归、递推、卡特兰、打表)
P1044 栈 题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进栈). 栈的重要 ...