Accepts: 47
Submissions: 939
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description
You are given two numbers NNN and MMM.
Every step you can get a new NNN in the way that multiply NNN by a factor of NNN.
Work out how many steps can NNN be equal to MMM at least.
If N can't be to M forever,print −1-1−1.
Input
In the first line there is a number TTT.TTT is the test number.
In the next TTT lines there are two numbers NNN and MMM.
T≤1000T\leq1000T≤1000, 1≤N≤10000001\leq N \leq 10000001≤N≤1000000,1≤M≤2631 \leq M \leq 2^{63}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
题意:N乘上它因子构成的数,该数再乘上它的因子,最后等于 M,最小要几步;
首先 M必须能整除 N 否则输出-1;然后 求 M/N 和 N 的最大公约数K,N每一步*K得到新N
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int gcd (__int64 x,__int64 y) { if(x%y) return gcd(y,x%y); return y; } int main() { __int64 n,m; int t; scanf("%d",&t); while(t--) { scanf("%I64d%I64d", &n,&m); int ans = 0; while(m != n) { if(m%n) { printf("-1\n"); break; } int k = gcd(m/n,n); if(k == 1) { printf("-1\n"); break; } n = n * k; ans ++ ; } if(n == m) printf("%d\n", ans); } return 0; }
本博客探讨了如何通过数值转换与因子乘法计算,实现从初始数值到目标数值所需的最少步骤,并讨论了特殊情况下的输出逻辑。

384

被折叠的 条评论
为什么被折叠?



