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 ...
随机推荐
- VScode+phpStudy搭建php代码调试环境
一.安装Visual Studio Code 官网:https://code.visualstudio.com/ 下载安装包后,按照默认安装即可 安装中文语言环境 点击左侧工具栏的 extension ...
- 关于js中iframe 中 location.href的用法
关于js中"window.location.href"."location.href"."parent.location.href".&qu ...
- 1.NET Core 概述
.NET Core 概述 .NET Core是一个免费的.开源的.跨平台的.广泛使用的Web框架:它是由微软维护的.社区广泛参与支持的一个框架..NET Core可以运行在:Windows.MacOS ...
- Oracle 11g服务端的安装和配置
1.双击Oracle11g_database安装目录下的Setup.exe. 2.选择“基本安装”,设置“安装位置”,填写“数据库名”和“口令”,点击“下一步”. 3.点击“下一步”. 4.一般会出现 ...
- 倒计时器CountDownLatch
1.背景: countDownLatch是在java1.5被引入,跟它一起被引入的工具类还有CyclicBarrier.Semaphore.concurrentHashMap和BlockingQueu ...
- WeChat-SmallProgram:微信小程序中使用Async-await方法异步请求变为同步请求
微信小程序中有些 Api 是异步的,无法直接进行同步处理.例如:wx.request.wx.showToast.wx.showLoading 等.如果需要同步处理,可以使用如下方法: 提示:Async ...
- iOS pch
Xcode6 之前会在 Supporting Files 文件夹下自动生成一个"工程名-PrefixHeader.pch"的预编译头文件,pch 头文件的内容能被项目中的其他所有源 ...
- CtenOS开放3306端口
1.查看防火墙状态 2. 关闭防火墙firewall 3. 开启端口 4. 重启防火墙 5. 常用命令介绍 在 Centos 7 中防火墙由 firewalld 来管理,而不是 iptables. 1 ...
- iOS 13DarkMode暗黑模式
iOS 13系统的iPhone 在设置-->显示与亮度 -->选择深色 即开启暗黑模式 1.暗黑模式关闭 1.1 APP开发未进行暗黑适配,出现顶部通知栏字体颜色无法改变始终为白色.可以全 ...
- Java数据结构与排序
一.引子:想要给ArrayList排序却发现没有排序方法?你有两种选择: 1.换用TreeSet: 2.使用Collection.sort(List<T> list) ...