zoukankan      html  css  js  c++  java
  • 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
    ");
                continue;
            }
            if(a==1 && b!=1)
            {
                printf("-1
    ");
                continue;
            }
            ull ans = 0;
            while(b != a)
            {
                if(b % a)
                {
                    printf("-1
    ");
                    break;
                }
                ull k = gcd(b/a,a);          //找能乘的最大因子
                if(k == 1)                   
                {
                    printf("-1
    ");
                    break;
                }
                a*=k;
                ans++;
            }
    
            if(a == b)
                cout<<ans<<endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    构建之法阅读笔记
    人月神话阅读笔记
    人月神话阅读笔记2
    人月神话阅读笔记
    第十周总结
    第九周总结
    进度日报10
    进度日报8
    进度日报7
    进度日报6
  • 原文地址:https://www.cnblogs.com/Przz/p/5409700.html
Copyright © 2011-2022 走看看