hdu 5505 GT and numbers

GT and numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1520    Accepted Submission(s): 381


Problem Description
You are given two numbers N and M.

Every step you can get a new N in the way that multiply N by a factor of N.

Work out how many steps can N be equal to M at least.

If N can't be to M forever,print 1.
 

 

Input
In the first line there is a number T.T is the test number.

In the next T lines there are two numbers N and M.

T10001N1000000,1M263.

Be careful to the range of M.

You'd better print the enter in the last line when you hack others.

You'd better not print space in the last of each line when you hack others.
 

 

Output
For each test case,output an answer.
 

 

Sample Input
3
1 1
1 2
2 4
 

 

Sample Output
0
-1
1

 题意:给出n和m,n每次和自身的因子相乘得到一个新的n,问最少多少次可使n==m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define MAX 10100
#define INF 0x3f3f3f
#define LL unsigned long long
using namespace std;
LL gcd(LL a,LL b)
{
    LL c;
    while(b)
    {
        c=a%b;
        a=b;
        b=c;
    }
    return a;
}
int main()
{
    LL n,m,j,i,t,k;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld%lld",&n,&m);
        if(n>m||n==0||m%n)
        {
            printf("-1\n");
            continue;
        }
        if(n==m)
        {
            printf("0\n");
            continue;
        }
        int flag=1;
        k=0;
        while(n!=m)
        {
            k++;
            if(gcd(m/n,n)==1)
            {
                flag=0;
                break;
            }
            n*=gcd(m/n,n);
        }
        if(flag)
        printf("%lld\n",k);
        else
        printf("-1\n");
    }
    return 0;
}

  

posted @   非我非非我  阅读(138)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
点击右上角即可分享
微信分享提示