hdu 5505(GT and numbers)

题意:

给你a和b,a每次和它的因子相乘得到一个新的a,求多少次后可以得到b。

 

输入样例
3
1 1
1 2
2 4
输出样例
0
-1
1

 

思路:

每次找出a和b/a的最大公约数(即当前a想得到b能乘的最大数),再进行判断。

第一次直接暴力搞,超时了,发现题意理解错了 T_T。

用unsign按着题意做即可。


 

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <functional>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;

ull gcd(ull a,ull b)
{
    if(a%b)
        return gcd(b,a%b);
    return b;
}

int main()
{
    int T;
    ull a,b;
    scanf("%d",&T);
    while(T--)
    {
        cin>>a>>b;
        if(a==1 && b==1)
        {
            printf("1\n");
            continue;
        }
        if(a==1 && b!=1)
        {
            printf("-1\n");
            continue;
        }
        ull ans = 0;
        while(b != a)
        {
            if(b % a)
            {
                printf("-1\n");
                break;
            }
            ull k = gcd(b/a,a);          //找能乘的最大因子
            if(k == 1)                   
            {
                printf("-1\n");
                break;
            }
            a*=k;
            ans++;
        }

        if(a == b)
            cout<<ans<<endl;
    }
    return 0;
}

  

posted @ 2015-10-26 23:28  Przz  阅读(132)  评论(0编辑  收藏  举报