博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LightOJ 1141 Number Transformation
阅读量:4311 次
发布时间:2019-06-06

本文共 1587 字,大约阅读时间需要 5 分钟。

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

26 126 13

Sample Output

Case 1: 2Case 2: -1

题意 每次s加上质因数,然后再求加过后结果的质因数,再加上去,到最后是否可以等于t;

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

#include
#include
#include
#include
#include
#include
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
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
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;}

代码水平还是差啊,

转载于:https://www.cnblogs.com/-xiangyang/p/9220266.html

你可能感兴趣的文章
android Manifest.xml选项
查看>>
Cookie/Session机制具体解释
查看>>
ATMEGA16 IOport相关汇总
查看>>
有意思的cmd命令
查看>>
js正則表達式语法
查看>>
JVM-垃圾回收
查看>>
ubuntu-14.04.1-desktop上安装配置JDK1.8的环境变量
查看>>
VS2013 添加已有文件夹
查看>>
摄影扫盲
查看>>
POJ 2388 - Who's in the Middle
查看>>
python 计时程序运行时间
查看>>
【最小生成树+贪心】BZOJ1821: [JSOI2010]Group 部落划分 Group
查看>>
ios-自动布局指南:入门
查看>>
【Shell脚本学习4】几种常见的Shell
查看>>
DataStructure part1 基础概念
查看>>
201521123007《Java程序设计》第11周学习总结
查看>>
BitLocker 加密工具挂起和恢复命令行(windows7)
查看>>
VMware下centos7安装VMware Tools
查看>>
Eclipse下Android开发的问题:Failed to install AndroidPhone.apk on device 'emulator-5554': timeout 解决办法...
查看>>
[luogu_P2045]方格取数加强版
查看>>