hdu 5505 GT and numbers【BestCoder Round #60】

简介:

GT and numbers

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


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.

T1000 1N1000000, 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
 
题目大意;
题描述
给出两个数NNMMNN每次可以乘上一个自己的因数变成新的NN。
求最初的NNMM至少需要几步。
如果永远也到不了输出-11
输入描述
第一行读入一个数TT表示数据组数。
接下来TT行,每行两个数NNMMT\leq1000T1000, 1\leq N \leq 10000001N1000000,1 \leq M \leq 2^{63}1M263.

注意M的范围。hack时建议输出最后一行的行末回车;每一行的结尾不要输出空格。
输出描述
对于每组数据,输出一个数表示答案。
官方题解:

如果AA大于BB那么显然无解。

考虑把AABB分解质因数。

BB存在AA没有的质因数也显然无解。

对于某一个AA的质因数的次数。为了加速接近BB,它一定是每次翻倍,最后一次的时候把剩下的加上。

那么答案就是最小的kk使得2^{k}*A_{num} \geq B_{num}2kAnumBnum

最后把每个质因数的答案max起来即可。

感觉本场比赛没有trick(雾~),我就打算加入一个很经典的trick:

BB可以刚好等于2^{63}263,这样就要开unsigned long long。

同时我还在题目里特别提醒了“注意M的范围”


我的思路:
这个题其实很简单,但是要注意的是范围的问题, 一定要使用  unsigned long long。
然后只需要求一下最大公约数,每次都乘以n ,m/n 的最大公约数。。。如果他们
取余之后 不是0 的话,就输出-1.。。
上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e4+5;
const int mod = 1000000007;
const double eps = 1e-7;

ULL gcd(ULL a, ULL b)
{
    if(b == 0)
        return a;
    return gcd(b, a%b);
}
int main()
{
    int T;
    ULL m, n;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        if(m%n != 0)
        {
            puts("-1");
            continue;
        }
        ULL ret = 0;
        bool ok = 0;
        ULL kk =gcd(m, n);
        while(m != n)
        {
            if(m%n != 0 || kk == 1)
            {
                ok = 1;
                break;
            }
            ret++;
            kk = gcd(n, m/n);
            n *= kk;
        }
        if(ok)
            puts("-1");
        else
            cout<<ret<<endl;
    }
    return 0;
}



目录
相关文章
|
11月前
Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) D2
Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) D2
56 0
|
机器学习/深度学习 算法 C++
|
人工智能
HDOJ/HDU 2710 Max Factor(素数快速筛选~)
HDOJ/HDU 2710 Max Factor(素数快速筛选~)
80 0
Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】
B. Which floor? time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output...
1273 0