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. 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "XXXView" nib but the view outlet was not set.' 崩溃问题

    先说下我遇到这个崩溃问题的原因: 自定义的Viewxib和系统的 View重名,导致崩溃 我的理解是我这里加载YJLoginViewController 的时候,YJLoginViewControll ...

  2. Java中的while循环——通过示例学习Java编程(10)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=20 在上一个教程中,我们讨论了for循环的用法.在本 ...

  3. 牛客NOIP提高组(三)题解

    心路历程 预计得分:$30 + 0 + 0 = 30$ 实际得分:$0+0+0= 0$ T1算概率的时候没模爆long long了... A 我敢打赌这不是noip难度... 考虑算一个位置的概率,若 ...

  4. 菜鸟 学注册机编写之 “RSA”

    测试环境  系统: xp sp3 调试器 :od 1.10 RSA简单介绍 选取两个别人不知道的大素数p, q. 公共模n = p*q 欧拉值φ(n) = (p-1)(q-1) 选取公匙(加密匙) e ...

  5. Myeclipse 突然打不开的问题

    用的好好的Myeclipse今天突然打不开了,打开myeclipse提示 :an error has occurred  see the log file 然后我打开日志文件,看到如下的报错信息: ! ...

  6. 在ActionBar中,即便设置showAsAction="always",items仍然在overflow中显示的问题

    今天很是苦恼,明明设置了android:showAsAction="always",但是所有的items全部都显示在overflow中,然后在官网发现了答案. 如果你为了兼容 An ...

  7. Node.js | 你的物联网系统,有个管家待认领

    很多时候,专业的事情都要交给专业的人来做,才会更放心. 例如买了套房,交房装修完毕,欢天喜地入住后,房子的日常养护和维护之类的事情,都由谁来负责呢? 物业呗~买了房子就自然需要房子所在小区提供的物业服 ...

  8. 打包ios软件并发布到应用商店

    真心感慨程序员是一个神奇的动物. 昨天接到任务,将项目打包并发布到apple商店.于是乎... 利用Hbuilder打包 需要的3个文件: AppId,描述文件profile,以及私钥证书 必须条件: ...

  9. Aizu 2456 Usoperanto (贪心)

    贪心,对于一个修饰关系可以连一条有向边,在合并的时候,子节点的序列一定是连续安排的,因为如果有交叉,交换以后一定更优. 然后一个序列一个序列的考虑,长度短的应该在前面,否则同样交换以后更优.因此排序以 ...

  10. Aizu 2304 Reverse Roads(无向流)

    把有向图修改成无向图,并保证每条边的流量守恒并满足有向容量(即abs(flow(u,v) - flow(v,u)) <= 1)满足限制. 得到最大流,根据残流输出答案. 因为最后少了'\n'而W ...