hdu5505 GT and numbers
来源:互联网 发布:淘宝卖家申请电子面单 编辑:程序博客网 时间:2024/04/30 22:17
看的大神的代码。。。
思路是,读入n,m,若m能整除n,求m / n与n的最大公约数(通过最大公约数这个因子n增长最快,用的次数最少),如果是1,说明和n互质,不可能完成。
如果最大公约数不是1,则n乘以这个最大公约数,开始迭代。
若刚开始m就不能整除n,则不可能完成。
#include <cstring>#include <cstdio>#include <cstdlib>#include <ctype.h>#include <iostream>#include <queue>#include <stack>#include <map>#include <algorithm>#include <math.h>#define ULL unsigned long longusing namespace std;ULL gcd(ULL a, ULL b) { if (a % b) { return gcd(b, a % b); } else { return b; }}int main(){ int T, ans; ULL m, n; while (~scanf("%d", &T)) { while (T--) { ans = 0; scanf("%I64u%I64u", &n, &m); while (m != n) { if (m % n) { puts("-1"); break; } ULL k = gcd(m / n, n); if (k == 1) { puts("-1"); break; } n *= k; ans++; } if (m == n) { printf("%d\n", ans); } } } return 0;}
0 0
- hdu5505 GT and numbers
- hdu5505 GT and numbers(BestCoder Round #60)
- BestCoder Round #60 HDU5505 GT and numbers
- hdu5505 GT and numbers(贪心)
- hdoj-GT and numbers
- HDU 5505 GT and numbers
- HDU 5505 GT and numbers
- GT and numbers(gcd)
- HDOJ 5505 GT and numbers
- HDOJ 5505-GT and numbers
- HDU 5505 GT and numbers
- hdoj 5505 GT and numbers
- HDU 5505 GT and numbers
- hdu(5505)——GT and numbers
- BestCoder Round #60 5505GT and numbers
- HDU 5505 GT and numbers(gcd)
- GT and numbers(BestCoder Round #60)1002
- hdu 5505(GT and numbers)
- Unable to add window -- token null is not for an application---各种bug
- ubuntu安装和查看已安装
- Clojure语言十三: 宏的基本概念
- Android Canvas中rotate()和translate()方法详解
- 测试工具有哪些
- hdu5505 GT and numbers
- VBS代码2
- 顺序查找
- 再谈PostMessage和SendMessage(如何使用 LParam 和 RParam两个参数)
- OC中的两种枚举解释
- C++的重写和隐藏
- AsyncTask使用原则
- alpha测试、beta测试
- HDU 2859 Phalanx(DP)