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 ...
随机推荐
- ubuntu 下python出现pkg: error processing package *python* 解决之道
1.linux有些自带程序很多是python写的,自带的python2也最好不要升级,不然会有很多问题 2.如果遇到 pkg: error processing package *python* (- ...
- NeurIPS审稿引发吐槽大会,落选者把荒唐意见怼了个遍:“我谢谢你们了”
七月份的尾巴,机器学习顶会NeurIPS 2019的初步结果已经来了. 一年一度的吐槽盛会也由此开始. "有评审问我啥是ResNet." "有评审问我为啥没引用X论文.我 ...
- 二维线段树 poj-2155
题意:t组样例 ,输入 n,m,表示n*n的矩阵进行m次操作 ,C: 输入两个坐标 ,组成的矩形 进行取反操作 ,Q:对输的坐标位置输入其值. 思路:一开始想的是用1000(表示x轴)个线段树(对每段 ...
- GCDAsyncSocket 笔记
https://www.dzliving.com/2019/03/26/gcdasyncsocket-%E7%9A%84%E5%B0%81%E8%A3%85%E4%B8%8E%E4%BD%BF%E7% ...
- Vertica的这些事(四)——-vertica加密数据
通过创建 Secure Access Policies可以对vertica中的某一列数据进行加密: CREATE ACCESS POLICY ON [schema][tablename] FOR CO ...
- 《Three.js 入门指南》2.4.1- 照相机 - 透视投影demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 1031 Hello World for U (20分)
Given any string of N (≥) characters, you are asked to form the characters into the shape of U. For ...
- Kylin 新定位:分析型数据仓库
亲爱的各位社区朋友: Apache Kylin 在 2014 年 10 月开源并加入 Apache 软件基金会的孵化器,一年后从孵化器毕业成为 Apache 顶级项目.从第一天起,Kylin 的标语是 ...
- Shell:homework
1.判断/etc/inittab文件是否大于100行,如果大于,则显示”/etc/inittab is a big file.”否则显示”/etc/inittab is a small file.”# ...
- 微信小程序wx.setStorage(OBJECT)
关于微信小程序的:wx.setStorage(OBJECT)在官网API介绍到: