GT and numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1818    Accepted Submission(s): 490

Problem Description
You are given two numbers N and M.

Every step you can get a new N in the way that multiply N by a factor of N.

Work out how many steps can N be equal to M at least.

If N can't be to M forever,print −1.

 
Input
In the first line there is a number T.T is the test number.

In the next T lines there are two numbers N and M.

T≤1000, 1≤N≤1000000,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 ,n每次乘上某一个约数,问将 n->m 所需最少次数?
题解: n *(m/n) = m ,我们要让 n -> m,所以让n每次乘上最大的公约数,这是每次能够扩大的最大倍数,所以每次找到 d = gcd(n,m/n),n' = n*d ,(m/n)' = m/n/d,一直到 n==m ,当d = 1 时永远不可能到,break 输出 -1.
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef unsigned long long ULL;
ULL n,m;
ULL gcd(ULL a,ULL b){
return b==?a:gcd(b,a%b);
}
int main(){
int tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%llu%llu",&n,&m);
if(m%n!=||n==&&m!=) {
printf("-1\n");
continue;
}
if(m==n) {
printf("0\n");
continue;
}
int ans = ;
ULL t = m/n;
while(m!=n){
ULL d = gcd(n,t);
n = n*d;
t = t/d;
ans++;
if(d==) break;
}
if(m!=n) printf("-1\n");
else printf("%d\n",ans);
}
return ;
}
 

hdu 5505(数论-gcd的应用)的更多相关文章

  1. GCD and LCM HDU 4497 数论

    GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD ...

  2. UVA.12716 GCD XOR (暴力枚举 数论GCD)

    UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...

  3. HDU 1722 Cake (数论 gcd)(Java版)

    Big Number 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1722 ——每天在线,欢迎留言谈论. 题目大意: 给你两个数 n1,n2 . 然后 ...

  4. 【HDU 5382】 GCD?LCM! (数论、积性函数)

    GCD?LCM! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  5. HDU - 5584 LCM Walk (数论 GCD)

    A frog has just learned some number theory, and can't wait to show his ability to his girlfriend. No ...

  6. HDU - 5974 A Simple Math Problem (数论 GCD)

    题目描述: Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least ...

  7. [数论] hdu 5974 A Simple Math Problem (数论gcd)

    传送门 •题意 一直整数$a,b$,有 $\left\{\begin{matrix}x+y=a\\ LCM(x*y)=b \end{matrix}\right.$ 求$x,y$ •思路 解题重点:若$ ...

  8. HDU 4497 数论+组合数学

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4497 解题思路:将满足条件的一组x,z,y都除以G,得到x‘,y',z',满足条件gcd(x',y' ...

  9. HDU 5869 Different GCD Subarray Query (GCD种类预处理+树状数组维护)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 问你l~r之间的连续序列的gcd种类. 首先固定右端点,预处理gcd不同尽量靠右的位置(此时gc ...

随机推荐

  1. javascript里的sleep()方法

    很多编程语言里都有sleep(),delay()等方法,它能让我们的程序不那么着急的去执行下一步操作,而是延迟.等待一段时间.软件开发中经常会遇到需要这样的函数,比如等待几分钟去检查某一事件是否发生. ...

  2. js通过生成临时表单再删除的方式向后台提交数据(模拟ajax的post提交但还要跳转页面不返回数据)以及 struts向前台返回文件下载及防止中文乱码处理

    为了避免发送数据中有特殊字符,发送时用 encodeURIComponent 编码 (其实这个 if中是直接通过浏览器下载文件的方法,else是向后台传数据的方法) struts后台Action处理接 ...

  3. 下载网页视频音频方法(djyeye为例)

    方法一: 三步操作: 选择media即可. m4a即为音频实际地址. 方法二: 方法三: 遨游浏览器 感谢知乎 https://www.zhihu.com/question/26938393

  4. bzoj 2277 [Poi2011]Strongbox 数论

    2277: [Poi2011]Strongbox Time Limit: 60 Sec  Memory Limit: 32 MBSubmit: 527  Solved: 231[Submit][Sta ...

  5. 前端PHP入门-031-文件上传-六脉神剑

    php.ini的设置 php.ini的文件太多,找不到的时候你可以使用 Ctrl+F 搜索相关配置项. 配置项 功能说明 file_uploads on 为开启文件上传功能,off 为关闭 post_ ...

  6. 理解PV操作和信号量

    对于信号量,可以认为是一个仓库,有两个概念,容量和当前的货物个数. P操作从仓库拿货,如果仓库中没有货,线程一直等待,直到V操作,往仓库里添加了货物,为了避免P操作一直等待下去,会有一个超时时间. V ...

  7. JS常用面试题

    一.闭包:   1.函数嵌套函数.内部函数可以引用外部函数的参数和变量. 参数和变量不会被垃圾回收机制所收回   function aaa(a){ var b = 5; function bbb(){ ...

  8. springsecurity 表达式一览

    表达式 描述 hasRole([role]) 当前用户是否拥有指定角色. hasAnyRole([role1,role2]) 多个角色是一个以逗号进行分隔的字符串.如果当前用户拥有指定角色中的任意一个 ...

  9. Codeforces 797 F Mice and Holes

    http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 ...

  10. javascript在IE下不能用 trim函数解决方法

    javascript 的trim 函数在firefox 下面使用没有问题 <script language="javascript"> var test1 = &quo ...