HDOJ 5505-GT and numbers

ゝ一世哀愁。 2022-08-20 05:16 25阅读 0赞

GT and numbers

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1625 Accepted Submission(s): 409

Problem Description

You are given two numbers N![Image 1][] and M![Image 1][].

Every step you can get a new N![Image 1][] in the way that multiply N![Image 1][] by a factor of N![Image 1][].

Work out how many steps can N![Image 1][] be equal to M![Image 1][] at least.

If N can’t be to M forever,print −1![Image 1][].

Input

In the first line there is a number T![Image 1][]. T![Image 1][] is the test number.

In the next T![Image 1][] lines there are two numbers N![Image 1][] and M![Image 1][].

T≤1000![Image 1][], 1≤N≤1000000![Image 1][], 1≤M≤2![Image 1][]63![Image 1][]![Image 1][].

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

  1. 3
  2. 1 1
  3. 1 2
  4. 2 4

Sample Output

  1. 0
  2. -1
  3. 1

Source

BestCoder Round #60

解题思路:

辗转相除法,注意定义不要溢出就行了。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. unsigned long long i,j,k,l,m,n,help;
  6. int gcd(unsigned long long m,unsigned long long n)
  7. {//求最大公约数
  8. help=1;
  9. while(help!=0)
  10. {
  11. help=m%n;
  12. m=n;
  13. n=help;
  14. }
  15. return m;
  16. }
  17. int main()
  18. {
  19. int T;
  20. scanf("%d",&T);
  21. while(T--)
  22. {
  23. int flag=0;
  24. scanf("%I64d%I64d",&n,&m);
  25. if(m<n||m%n||n==0)
  26. {
  27. printf("-1\n");
  28. continue;
  29. }
  30. if(n==m)
  31. {
  32. printf("0\n");
  33. continue;
  34. }
  35. int ans=0;
  36. while(n!=m)
  37. {
  38. if(gcd(m/n,n)==1)
  39. {
  40. flag=1;
  41. break;
  42. }
  43. n=n*(gcd(m/n,n));
  44. ans++;
  45. }
  46. if(flag==0)
  47. printf("%d\n",ans);
  48. else
  49. printf("-1\n");
  50. }
  51. return 0;
  52. }

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,25人围观)

还没有评论,来说两句吧...

相关阅读