GT and numbers(BestCoder Round #60)1002

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;
}


你可能感兴趣的:(GT and numbers(BestCoder Round #60)1002)