Number Transformation
来源:互联网 发布:唐小僧 卓软件 编辑:程序博客网 时间:2024/05/20 01:39
Number Transformation
In this problem, you are given an integer number s. You can transform any integer number A to another integer numberB 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 sto another integer number t.
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).
For each case, print the case number and the minimum number of transformations needed. If it's impossible, then print-1.
2
6 12
6 13
Case 1: 2
Case 2: -1
题意:一个数与它的质因数相加等于t需要几步#include<cstdio>#include<queue>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int MAX=1e4;const int INF= 0x3f3f3f3f / 2;int a[MAX]={1,1};int vis[MAX];int s,t,ok,cut;vector<int > v[MAX];void search(){for(int i = 2; i < MAX ; i++) { if(a[i]) continue; for(int j= i*i ; j< MAX ;j+=i) { a[j]=1;} } for(int i=2;i<MAX;i++) for(int j=2 ; j< MAX ;j++) { if(i%j==0&&a[j]==0) v[i].push_back(j); }}struct node{ int x,y;} st,pt;void bfs(){ memset(vis,0,sizeof(vis)); queue<node> q; vis[s]=1; st.x=s,st.y=0; q.push(st); while(!q.empty()) { st=q.front(); q.pop(); if(st.x==t) { ok=1; cut=st.y; } for(int i=0;i<v[st.x].size();i++) { pt.x=st.x+v[st.x][i], pt.y=st.y+1; if(pt.x <= t&&!vis[pt.x]) { vis[pt.x]=1; q.push(pt);}} }}int main(){ search(); int n; scanf("%d",&n); int w=n; while(n--) { scanf("%d %d",&s,&t); if(s == t) printf("Case %d: %d\n",w-n,0); else if (!a[s] || !a[t]) printf("Case %d: %d\n",w-n,-1); else { ok=0; cut=INF; bfs(); if(ok) printf("Case %d: %d\n",w-n,cut); else printf("Case %d: %d\n",w-n,-1); } }return 0;}
阅读全文
0 0
- Number Transformation
- Number Transformation
- Number Transformation
- HDU - 4952 Number Transformation
- HDU4952-Number Transformation
- HDU 4952 Number Transformation
- HDOJ 4952 Number Transformation
- hdu 4952 Number Transformation
- hdu 4952 Number Transformation
- hdu 4952Number Transformation
- hdu 4952 Number Transformation
- 【HDU4952】Number Transformation(数学)
- HDU4952:Number Transformation
- hdu 4952 Number Transformation
- Hdu 4952 Number Transformation
- 数论 Number Transformation HDU4952
- codeforces Number Transformation II
- bzoj 3858: Number Transformation
- js中的事件
- A
- BIOS简介
- 2017 Multi-University Training Contest
- LeetCode 62. Unique Paths (Python)
- Number Transformation
- spring boot集成JdbcTemplate
- HDU 【1087】Super Jumping! Jumping! Jumping
- Python 里边yield和全局变量global的用法
- Debug日志:C++读取任意磁盘、任意路径下的文件
- HDU 6095 思维题
- 利用USB RUBBER DUCKY(USB 橡皮鸭)在目标机器上启动Empire或Meterpreter会话
- QT
- 当子类继承父类,其子类和父类中静态代码块,构造代码块,无参构造之间的输出顺序