LightOJ
来源:互联网 发布:python数据处理怎么样 编辑:程序博客网 时间:2024/05/22 04:51
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。 您可以将任何整数A转换为另一个整数B,将x添加到A.这个x是一个整数,它是A的素因子(请注意,1和A不被视为A的因子)。 现在,您的任务是找到将s转换为另一整数t所需的最小转换次数。(谷歌版)
思路:
普通队列就可以,没必要优先,主要是每次转换后的B就是下一个A,题意明确点,接下来就是搜了;
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#define INF 0x3f3f3f3fusing namespace std;int vis[1010],p[1010];int s,t,cnt,ans;struct node{ int x; int step; bool friend operator<(node a,node b) { return a.step>b.step; }};void getp(int n){ cnt=0; int m=n; for(int i=2;i*i<=n;i++) { if(n%i==0) { p[cnt++]=i; while(n%i==0) n/=i; } } if(n>1 && n!=m) p[cnt++]=n;}void bfs(){ bool flag=false; priority_queue<node> q; node now,next; now.x=s; vis[s]=1; now.step=0; q.push(now); while(!q.empty()) { next=q.top(); q.pop(); if(next.x==t) { ans=min(ans,next.step); flag=true; continue; } memset(p,0,sizeof(p)); getp(next.x); for(int i=0;i<cnt;i++) { now.x=next.x+p[i]; now.step=next.step+1; if(now.x<=t && !vis[now.x]) { vis[now.x]=1; q.push(now); } } } if(flag) printf("%d\n",ans); else printf("-1\n");}int main(){ int T; scanf("%d",&T); while(T--) { static int res=1; ans=INF; memset(vis,0,sizeof(vis)); scanf("%d %d",&s,&t); printf("Case %d: ",res++); if(s==t) { printf("0\n"); continue; } if(s>t) { printf("-1\n"); continue; } bfs(); } return 0;}
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- [LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- AngularJS入门-(11)表单
- Mybatis一对多和多对一的映射
- java代码实现外网IP的获取
- LeetCode 2.Add Two Numbers
- Protobuf详解(.Java文件)
- LightOJ
- 将Android Studio工程内置到源码工程编译并设置成默认Launcher
- 使用DelayQueue 和 FutureTask 实现java中的缓存
- What is an abstract class, and when should it be used?
- Android面试题基础集锦《一》
- 利用java对基本汉字(unicode编码4E00-9FA5)进行笔画排序
- linux注销登录用户
- 面向对象特征-多态
- redis命中率计算