zoukankan      html  css  js  c++  java
  • G

    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

    AC代码:

     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<string.h>
     4 #include<queue>
     5 using namespace std;
     6 
     7 int c[1010], vis[1010];
     8 int a, b, f;
     9 
    10 struct note
    11 {
    12     int x, s;
    13 };
    14 
    15 int prime()
    16 {
    17     memset(c, -1, sizeof(c));
    18     c[0] = 0, c[1] = 0;
    19     for(int i = 2; i*i < 1005; i++)
    20     {
    21         if(c[i])
    22             for(int j = i+i; j < 1005; j += i)
    23                 c[j] = 0;
    24     }
    25 }
    26 
    27 void bfs(int x)
    28 {
    29     queue<note>Q;
    30     note p,q;
    31     p.x = x;
    32     p.s = 0;
    33     vis[x] = 1;
    34     Q.push(p);
    35     while(!Q.empty())
    36     {
    37         p = Q.front();
    38         Q.pop();
    39         if(p.x == b)
    40         {
    41             f = p.s;
    42             break;
    43         }
    44         for(int i = 2; i < p.x; i++)
    45         {
    46             if(c[i] == 0 || p.x + i > b || p.x % i != 0 || vis[p.x+i])
    47                 continue;
    48             q.x = p.x + i;
    49             vis[q.x] = 1;
    50             q.s = p.s + 1;
    51             Q.push(q);
    52         }
    53     }
    54     return ;
    55 }
    56 
    57 int main()
    58 {
    59     int k = 0, flag = 0;
    60     prime();
    61 
    62     int t;
    63     cin >> t;
    64     while(t--)
    65     {
    66         f = -1;
    67         memset(vis, 0, sizeof(vis));
    68         cin >> a >> b;
    69         bfs(a);
    70         cout << "Case " << ++flag << ": " << f << endl;
    71     }
    72 
    73     return 0;
    74 }
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    利用nginx实现负载均衡和动静分离
    Nginx动静分离实现
    php中session 入库的实现
    php文字水印和php图片水印实现代码(二种加水印方法)
    采集图片水印添加
    [安全]PHP能引起安全的函数
    [安全]服务器安全之 PHP权限目录
    Centos下安装git的web服务器
    Centos下抓包
    UVA 10795
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8799057.html
Copyright © 2011-2022 走看看