BestCoder Round #60 5505GT and numbers




Accepts: 47
Submissions: 939
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description

You are given two numbers NNN and MMM.

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

Work out how many steps can NNN be equal to MMM at least.

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

Input

In the first line there is a number TTT.TTT is the test number.

In the next TTT lines there are two numbers NNN and MMM.

T≤1000T\leq1000T1000, 1≤N≤10000001\leq N \leq 10000001N1000000,1≤M≤2631 \leq M \leq 2^{63}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,最小要几步;
 
   
首先 M必须能整除 N 否则输出-1;然后 求 M/N 和 N 的最大公约数K,N每一步*K得到新N
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int gcd (__int64 x,__int64 y)
{
    if(x%y)
    return gcd(y,x%y);
    return y;

}
int main()
{
    __int64 n,m;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d%I64d", &n,&m);
        int ans = 0;
        while(m != n)
        {
            if(m%n)
            {
                printf("-1\n");
                break;
            }
            int k = gcd(m/n,n);
            if(k == 1)
            {
                printf("-1\n");
                break;
            }
            n = n * k;
            ans ++ ;
        }
        if(n == m)
        printf("%d\n", ans);

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值