您的位置:首页 > 其它

HDU 5505 GT and numbers(求一个数乘以它的因子得到另一个数的最小步数)

2015-10-18 18:07 309 查看
题目地址:点击打开链接

思路:参考大神A的,注意一下long long 能表示的最大数为2^63-1,而unsigned long long 能表示的最大数为2^63,自己比赛的时候没A出来理解不深

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

using namespace std;

unsigned long long gcd(unsigned long long a,unsigned long long b)
{
return b == 0 ? a : gcd(b,a%b);
}

int main()
{
unsigned long long m,n,k;
int t;
scanf("%d",&t);
while(t--)
{
int sum = 0;
scanf("%I64u%I64u",&n,&m);
if(n == m)
{
printf("0\n");
continue;
}
if(m % n)
{
printf("-1\n");
continue;
}
m /= n;//这里事先要除一下
while(m != 1)
{
k = gcd(m,n);
m /= k;
n *= k;
sum++;
if(k == 1)
break;
}
if(m == 1)
{
printf("%d\n",sum);
}
else
{
printf("-1\n");
}
}
return 0;
}


大神地址:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: