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;
}
大神地址:点击打开链接
思路:参考大神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;
}
大神地址:点击打开链接
相关文章推荐
- Java基础--面向对象
- Sqrt(x)
- 信息安全程序设计基础第五周学习总结
- linux-ht开启
- Android开发规范
- HDU——2087 剪花布条
- eclipse -- 工作中经常用到的快捷键(不断完善)
- ubuntu无法登陆问题收集
- 境外自然人不得以实际出资为由主张转让其在国内公司中享有的股权
- 九度OJ 1043:Day of Week(星期几) (日期计算)
- 九度OJ 1043:Day of Week(星期几) (日期计算)
- CGI程序里调用system、popen运行命令需要root权限---boa配置需要注意的问题
- Android按两次返回键退出程序
- 微信企业号api调用频率
- BroadcastReceiver
- JVM调优总结
- [转]Intent和PendingIntent的区别
- 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题解决方案参考
- Android Studio 真正国内SDK在线更新说明
- Android Studio 真正国内SDK在线更新说明