<html>
题目链接:点击打开链接
题意:给两个数n和m, n每次乘以它的因子变成一个新的值, 求最少乘几次能够变成m。
思路:每次乘以的整数v有两个要求:1.它是n的因子;2.它要尽量大。
又由于假设n能终于到达m。一定是乘以n的k倍, 所以仅仅要n能被m整除。 那么每次取gcd(n, m/n)即可了。
细节參见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef unsigned long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 1000000000 + 7;
const int INF = 1000000000;
const int maxn = 100;
int T;
ll n,m;
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%I64u%I64u",&n,&m);
int ans = 0;
if(n > m || m % n != 0 || n == 1 && m > 1) {
printf("-1\n");
}
else {
while(n != m) {
ll v = gcd(n,m/n);
if(v == 1) break;
n *= v;
++ans;
}
if(n == m) printf("%d\n",ans);
else printf("-1\n");
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主同意不得转载。
举报
- 本文已收录于下面专栏:
- ACM竞赛
相关文章推荐
-
hdu 4722 Good Numbers(dp)
题目链接:hdu 4722 Good Numbers 题目大意:给出a。b,问从a到b之间。有多少个好数字,好数字的定义为:每一个位的数字相加是10的倍数。 </![阿尔萨斯 阿尔萨斯]()
- 阿尔萨斯
- 2014-05-04 21:05
- 41
-
HDU - 5505 GT and numbers 【GCD】
GT and numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...![lututu123 lututu123]()
- lututu123
- 2016-10-13 19:40
- 97
-
hdu 4983 Goffi and GCD(数论)
<a target="_blank" href="http://acm.hdu.edu.cn/showproblem.![阿尔萨斯 阿尔萨斯]()
- 阿尔萨斯
- 2014-08-24 23:56
- 71
-
HDU 5505 GT and numbers(gcd)
题目链接:HDU 5505 GT and numbers 题目大意: 给你两个数 N, M 。N能够通过不断乘 N的因子,来达到 N = M,输出最少须要乘几次N的因子,假设如何都...![chaiwenjun000 chaiwenjun000]()
- chaiwenjun000
- 2015-10-20 10:40
- 335
-
HDU 1695 GCD 欧拉函数+容斥原理
转载请注明出处,谢谢<a hr![java-mans java-mans]()
- java-mans
- 2012-08-04 12:59
- 326
-
hdoj5505GT and numbers【gcd】
GT and numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...![R1986799047 R1986799047]()
- R1986799047
- 2016-01-24 23:33
- 237
-
hdu 4497 GCD and LCM(组合数学)
题目链接:hdu 4497 GCD and LCM 题目大意:给出三个数的最大公约数和最小公倍数,问说有多少种三个数满足。<s
![阿尔萨斯 阿尔萨斯]()
- 阿尔萨斯
- 2014-04-25 23:21
- 46
-
HDU 5505 GT and numbers(对于被我无意识坑到的3个人我表示抱歉)——BestCoder Round #60
HDU 5505 GT and numbers(对于被我无意识坑到的3个人我表示抱歉)——BestCoder Round #60![queuelovestack queuelovestack]()
- queuelovestack
- 2015-10-18 00:06
- 1097
-
HDU 4676 Sum Of Gcd【数论。数据结构(分块)】
静态区间查询,没有更新操作,瞬间就想起来了哪个O(n*sqrt(n))的做法。关键是区间转移的时候不会处理了。仅仅能说数学拙计了…… 对于此类问题的时间复杂度分析。详见:http://blog.csdn.net/yang_7_46/article/details/9618637<![444878909 444878909]()
- 444878909
- 2013-08-15 21:10
- 64
-
hdu5505 GT and numbers(BestCoder Round #60)
GT and numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To...![d_x_d d_x_d]()
- d_x_d
- 2015-10-18 14:31
- 904
收藏助手
不良信息举报

浙公网安备 33010602011771号
0条评论