GT and numbers

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

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,问最少多少次可使n==m

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<algorithm>
  5. #define MAX 10100
  6. #define INF 0x3f3f3f
  7. #define LL unsigned long long
  8. using namespace std;
  9. LL gcd(LL a,LL b)
  10. {
  11. LL c;
  12. while(b)
  13. {
  14. c=a%b;
  15. a=b;
  16. b=c;
  17. }
  18. return a;
  19. }
  20. int main()
  21. {
  22. LL n,m,j,i,t,k;
  23. scanf("%lld",&t);
  24. while(t--)
  25. {
  26. scanf("%lld%lld",&n,&m);
  27. if(n>m||n==0||m%n)
  28. {
  29. printf("-1\n");
  30. continue;
  31. }
  32. if(n==m)
  33. {
  34. printf("0\n");
  35. continue;
  36. }
  37. int flag=1;
  38. k=0;
  39. while(n!=m)
  40. {
  41. k++;
  42. if(gcd(m/n,n)==1)
  43. {
  44. flag=0;
  45. break;
  46. }
  47. n*=gcd(m/n,n);
  48. }
  49. if(flag)
  50. printf("%lld\n",k);
  51. else
  52. printf("-1\n");
  53. }
  54. return 0;
  55. }

  

hdu 5505 GT and numbers的更多相关文章

  1. HDU 5505——GT and numbers——————【素数】

    GT and numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  2. HDU 5505 - BestCoder Round #60 - GT and numbers

    题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1002 思路 : N有若 ...

  3. hdu 5505(GT and numbers)

    题意: 给你a和b,a每次和它的因子相乘得到一个新的a,求多少次后可以得到b. 输入样例 3 1 1 1 2 2 4 输出样例 0 -1 1 思路: 每次找出a和b/a的最大公约数(即当前a想得到b能 ...

  4. hdu 5272 Dylans loves numbers

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5272 Dylans loves numbers Description Who is Dylans?Y ...

  5. hdu 5272 Dylans loves numbers 水题

    Dylans loves numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem. ...

  6. hdu 1058 dp.Humble Numbers

    Humble Numbers Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  7. 数论 - 筛法暴力打表 --- hdu : 12876 Quite Good Numbers

    Quite Good Numbers Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit ...

  8. hdu 5505(数论-gcd的应用)

    GT and numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  9. BestCoder Round #60/HDU 5505 暴力数学

    GT and numbers 问题描述 给出两个数NN和MM. NN每次可以乘上一个自己的因数变成新的NN. 求最初的NN到MM至少需要几步. 如果永远也到不了输出-1−1. 输入描述 第一行读入一个 ...

随机推荐

  1. hdu 4664 Triangulation 博弈论

    看到这题时,当时还不会做,也没搞懂sg函数,于是狠狠的钻研了下博弈论,渐渐的知道了sg函数…… 现在在来做这题就很容易了,1A 打表容易发现在80左右的时候就出现循环节了 代码如下: #include ...

  2. http://blog.csdn.net/majian_1987/article/details/44939911

    http://blog.csdn.net/majian_1987/article/details/44939911

  3. 网上图书商城项目学习笔记-037工具类之BaseServlet及统一中文编码

    1.统一中文编码分析 tomcat默认esetISO-8859-1编码,在servlet中,可能通过request的setCharacterEncoding(charset)和response.set ...

  4. Android:监听ListView

    本文目录 监听ListView点击事件 监听ListView滚动事件 监听ListView点击事件 使用监听器OnItemClickListener package com.example.tests ...

  5. Apache Tomcat下载、安装、配置图文教程

    本文已迁移到我的个人网站 http://www.wshunli.com 文章地址: http://www.wshunli.com/2016/03/19/Tomcat安装配置/ (整理截图.安装过程更加 ...

  6. CenOS7.1安装VNC——让win7远程桌面linux

    参考:http://wic.xingning.gov.cn/blog/29 https://linux.cn/article-5335-1.html 1.检查是否安装VNC, rpm -q tiger ...

  7. 转载:最小生成树-Prim算法和Kruskal算法

    本文摘自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/30/2615542.html 最小生成树-Prim算法和Kruskal算法 Prim算 ...

  8. ZJOI2008泡泡堂BNB

    1034: [ZJOI2008]泡泡堂BNB Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1305  Solved: 676[Submit][Sta ...

  9. Android使用Webview加载网页

    安卓使用Webview来加载和显示网页内容,首先在layout文件中定义Webview <?xml version="1.0" encoding="utf-8&qu ...

  10. LeetCode Binary Tree Level Order Traversal (按层收集元素)

    题意:按层,将元素收集在一个二维数组中. 思路:广搜应该是普遍的方法了.还能避免栈溢出,多好用.搭配deque,因为要经常删除. /** * Definition for a binary tree ...