HDU - 5505 GT and numbers 【GCD】

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 .

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
 

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\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值