GT and numbers(BestCoder Round #60)1002

12 篇文章 0 订阅
8 篇文章 0 订阅

GT and numbers

题意:
给出两个数NNNMMMNNN每次可以乘上一个自己的因数变成新的NNN。
求最初的NNNMMM至少需要几步。
如果永远也到不了输出−1-11
方法:
若是N可以通过乘上自己的因数可以变成M,则M一定可以整除N。
N在通过乘上自己的因数可以变成M的过程中,N通过乘以N,M的最大公约数就可以达到M,若此时的N与M的最大公约数为1,则永远达不到M;

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


#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
#define LL long long
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;


int gcd(LL x,LL y)
{
    return x%y ? gcd(y,x%y) : y;
}

int main()
{
    int T;
    LL N,M;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld%lld",&N,&M);
        int cnt = 0;
        while(N != M)
        {
            if(M % N)
            {
                printf("-1\n");
                break;
            }

            int k = gcd(M/N,N);

            if(k == 1)
            {
                printf("-1\n");
                break;
            }

            N *= k;

            cnt++;
        }

        if(N == M)
            printf("%d\n",cnt);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值