【lightoj】1141 - Number Transformation

1141 - Number Transformation
Time Limit: 2 second(s)Memory Limit: 32 MB

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

Output for Sample Input

2

6 12

6 13

Case 1: 2

Case 2: -1

 题意:A+s=B,s为A的质因子,最少需要重复多少次可以得到B 注意A''=A+s  s为新得到的A''的质因子,刚开始理解错了。。。

code:

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<queue>
  5. #define inf 0x3f3f3f3f
  6. using namespace std ;
  7. int a [ 1000 ] = { 1, 1 } ;
  8. int num [ 1000 ] ;
  9. void su ( )
  10. {
  11.     memset (a, 0, sizeof (a ) ) ;
  12.     for ( int i = 2 ;i < 1010 ;i ++ ) {
  13.         if (a [i ] ) continue ;
  14.         for ( int j = 2 *i ;j <= 1000 ;j + =i )
  15.         a [j ] = 1 ;
  16.     }
  17. }
  18. void f ( int s, int t )
  19. {
  20.     queue < int >q ;
  21.     memset (num, 0x3f, sizeof (num ) ) ; //初始化为无穷大
  22.     num [s ] = 0 ;
  23.     q. push (s ) ;
  24.     while ( !q. empty ( ) ) {
  25.         int v =q. front ( ) ;
  26.         q. pop ( ) ;
  27.         if (v ==t ) return ;
  28.         for ( int i = 2 ;i <v ;i ++ ) {
  29.         if (v %i == 0 &&a [i ] == 0 ) {
  30.             if (v +i >t ) break ;
  31.             if (num [i +v ] >num [v ] + 1 )
  32.             num [v +i ] =num [v ] + 1,
  33.             q. push (v +i ) ;
  34.         }
  35.     }
  36.     }
  37.    
  38. }
  39. int main ( )
  40. {
  41.     int T,s,t ;
  42.     su ( ) ;
  43.     scanf ( "%d", &T ) ; int k = 1 ;
  44.     while (T -- )
  45.     {
  46.         scanf ( "%d%d", &s, &t ) ;
  47.         f (s,t ) ;
  48.         if (num [t ] ! =inf )
  49.     printf ( "Case %d: %d\n",k ++,num [t ] ) ;
  50.     else printf ( "Case %d: %d\n",k ++, - 1 ) ;
  51.     }
  52.     return 0 ;
  53.   }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值