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.
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.
T≤1000, 1≤N≤1000000, 1≤M≤263.
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.
In the next T lines there are two numbers N and M.
T≤1000, 1≤N≤1000000, 1≤M≤263.
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
题目大意;
题描述
给出两个数N和M。 N每次可以乘上一个自己的因数变成新的N。 求最初的N到M至少需要几步。 如果永远也到不了输出−1。
输入描述
第一行读入一个数T表示数据组数。 接下来T行,每行两个数N和M。 T≤1000, 1≤N≤1000000,1≤M≤263. 注意M的范围。hack时建议输出最后一行的行末回车;每一行的结尾不要输出空格。
输出描述
对于每组数据,输出一个数表示答案。官方题解:
如果A大于B那么显然无解。
考虑把A和B分解质因数。
若B存在A没有的质因数也显然无解。
对于某一个A的质因数的次数。为了加速接近B,它一定是每次翻倍,最后一次的时候把剩下的加上。
那么答案就是最小的k使得2k∗Anum≥Bnum。
最后把每个质因数的答案max起来即可。
感觉本场比赛没有trick(雾~),我就打算加入一个很经典的trick:
B可以刚好等于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; }