Number Transformation

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;

题解 bfs ,一开始老是想dfs,并且题意还理解的有偏差。以下是代码。

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
const int MAX=1000000;
int pri[MAX],dis[MAX];
int kk=0;
void pirme()//打表求素数
{
memset(pri,0,sizeof(pri));
pri[0]=pri[1]=1;
for(int i=2;i<MAX;i++)
{
if(pri[i]==0)
{
for(int j=2;j*i<MAX;j++)
pri[i*j]=1;
}
}
} void bfs(int a,int b)
{
memset(dis,0x3f,sizeof(dis));//将数组都存为0x3f3f3f3f
queue<int>qu;
qu.push(a) ;
dis[a]=0;//起始位置为0;
while(!qu.empty() )
{
int x=qu.front() ;
qu.pop() ;
if(x==b) return ;
for(int i=2;i<x;i++)//求质因子;
{
if(x%i==0&&pri[i]==0)
{
if(x+i>b) break;
if(dis[x+i]>dis[x]+1)//更改步数
{
dis[x+i]=dis[x]+1;
qu.push(x+i);
}
}
}
}
} int main()
{
int T;
scanf("%d",&T);
pirme();
int Case=1;
while(T--)
{
int s,t;
scanf("%d%d",&s,&t);
bfs(s,t);
if(dis[t]!=0x3f3f3f3f) printf("Case %d: %d\n",Case++,dis[t]);
else printf("Case %d: -1\n",Case++); }
return 0;
}

代码水平还是差啊,

LightOJ 1141 Number Transformation的更多相关文章

  1. hdu4952 Number Transformation (找规律)

    2014多校 第八题 1008 2014 Multi-University Training Contest 8 4952 Number Transformation Number Transform ...

  2. bzoj 3858: Number Transformation 暴力

    3858: Number Transformation Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 82  Solved: 41[Submit][Sta ...

  3. HDU-4952 Number Transformation

    http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/ ...

  4. CodeForces346 C. Number Transformation II

    C. Number Transformation II time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. Codeforces 251C Number Transformation

    Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define L ...

  6. CodeForces 346C Number Transformation II

    Number Transformation II 题解: 对于操作2来说, a - a % x[i] 就会到左边离a最近的x[i]的倍数. 也就是说 [ k * x[i] + 1,  (k+1)* x ...

  7. Number Transformation

    Description In this problem, you are given a pair of integers A and B. You can transform any integer ...

  8. LightOJ 1141 Program E

    Description In this problem, you are given an integer number s. You can transform any integer number ...

  9. LightOj 1065 - Number Sequence (矩阵快速幂,简单)

    题目 和 LightOj 1096 - nth Term 差不多的题目和解法,这道相对更简单些,万幸,这道比赛时没把模版给抽风坏. #include<stdio.h> #include&l ...

随机推荐

  1. EF增删查改加执行存储过程和sql语句,多种方法汇总

    ActionUrl c = new ActionUrl() { ActionName="test", RequestUrl="/123/123", SubTim ...

  2. Spring Cloud config中,使用数据库存储配置信息

    主要内容 在springcloud config中,使用数据库存储配置信息. 系统默认采用git的方式,此处我们介绍使用jdbc的方式存储配置信息 准备数据库 数据库我们使用mysql. 新建库 p- ...

  3. Android客户端与PC服务端、android服务端通过WiFi通信

    前期准备:我的是Linux Mint操作系统(总之折腾的过程中怀疑过是不是系统的问题),首先是要创建wifi热点给android手机使用,这个时候笔记本作为通信的服务器端,android手机作为客户端 ...

  4. 配置百度云盘python客户端bypy上传备份文件

    要求:安装python2.7,安装git 1.git clone https://github.com/houtianze/bypy.git 2.cd bypy 3.sudo python setup ...

  5. SID1190471 / 烦人的幻灯片 暴力出奇迹 !!!!!!!!!!!!!!!!!!

    PID221 / 烦人的幻灯片 ☆ 提交你的代码 查看讨论和题解 你还木有做过哦 我的状态         查看最后一次评测记录 质量还不能统计出来哦~ 题目评价 质量 无 ★★★★★ ★★★★☆ ★ ...

  6. Unity中的输入

    目录 移动平台的输入 触摸 触摸相关的函数 触摸的一个示例 重力加速器 在Unity中访问重力加速器的信息 重力加速器示例 虚拟键盘 其他输入 传统的输入 鼠标,键盘,控制杆,手柄 虚拟控制轴(Vir ...

  7. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:0.概述

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 序言 帖主和队友仿制了一个简单版的微信,其中,队友是用Unity3D做前段,帖主用Java的Mina.Hiberna ...

  8. ansible基本操作

    ansible优点:redhat自带工具,可通过rpm或yum直接安装:客户端免安装:操作通过ssh验证操作:可以通过自定义hosts文件对可操作主机进行分类,方便批量操作 #ansible操作格式, ...

  9. php使用GD库实现图片水印和缩略图——封装成类

    学完了如何使用GD库来实现对图片的各种处理,那么我们可以发现,不管哪种方法,都有相似之处,如果我们把这些相似的地方和不相似的地方都封装成类,这样就可以提升代码的速度,而且节省了很多时间,废话不多说,来 ...

  10. pat甲级1044二分查找

    1044 Shopping in Mars(25 分) Shopping in Mars is quite a different experience. The Mars people pay by ...