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


  • 相关阅读:
    boost::asio在VS2008下的编译错误
    Java集合框架——接口
    ACM POJ 3981 字符串替换(简单题)
    ACM HDU 1042 N!(高精度计算阶乘)
    OneTwoThree (Uva)
    ACM POJ 3979 分数加减法(水题)
    ACM HDU 4004 The Frog's Games(2011ACM大连赛区第四题)
    Hexadecimal View (2011ACM亚洲大连赛区现场赛D题)
    ACM HDU 4002 Find the maximum(2011年大连赛区网络赛第二题)
    ACM HDU 4001 To Miss Our Children Time (2011ACM大连赛区网络赛)
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770777.html
Copyright © 2011-2022 走看看