zoukankan      html  css  js  c++  java
  • HDU

    GT and numbers

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1846    Accepted Submission(s): 502

    Problem Description
    You are given two numbers N and M.

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

    Work out how many steps can N be equal to M at least.

    If N can't be to M forever,print 1.
     
    Input
    In the first line there is a number T.T is the test number.

    In the next T lines there are two numbers N and M.

    T10001N1000000,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
     
    Source
     
    Recommend
    hujie   |   We have carefully selected several similar problems for you:  5932 5931 5930 5929 5928 
     
     题意:
    给你两个数N和M,求是否能进行操作使N乘上他的因子与M相等。如果可以输出最小的操作次数,否则输出“-1”。
    思路:
    如果N可以得到M,说明M的因子都包等于N的因子中。M中去掉N剩下的因子和N的最大公因式为min(N, M/N)。这样每次乘以最大公因式,可以保证每次乘的因子是最大的。
     
    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    typedef unsigned long long LL; // long long会爆
    //typedef __int64 Int;
    typedef pair<int, int> paii;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-5;
    const double PI = acos(-1.0);
    const long long MOD = 1e9 + 7;
    const int MAXN = 100 + 10;
    LL gcd(LL x, LL y) {return y? gcd(y, x%y): x;}
    int main() {
        int T;
        LL N, M, t;
        scanf("%d", &T);
        while (T--) {
            scanf("%I64u%I64u", &N, &M);
            int ans = 0; t = M;
            while (N != M) {
                LL d = gcd(N, M/N);
                if (N > M || d == 1) {ans = -1; break;}
                N *= d; t /= d;
                ans++;
            }
            printf("%d
    ", ans);
        }
        return 0;
    }


  • 相关阅读:
    LeetCode_143. 重排链表
    LeetCode_844. 比较含退格的字符串
    LeetCode116. 填充每个节点的下一个右侧节点指针
    1002. 查找常用字符
    贝叶斯定理(Bayes' theorem)的理解笔记
    汉明码
    超级好用 音乐可视化软件-Specinker
    安卓安装aidlearning
    windows下gcc的安装和使用
    ubuntu桌面配置
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770777.html
Copyright © 2011-2022 走看看