zoukankan      html  css  js  c++  java
  • hdu 5505 GT and numbers

    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.

    T10001N1000000,1M263.

    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

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<algorithm>
    #define MAX 10100
    #define INF 0x3f3f3f
    #define LL unsigned long long
    using namespace std;
    LL gcd(LL a,LL b)
    {
    	LL c;
    	while(b)
    	{
    		c=a%b;
    		a=b;
    		b=c;
    	}
    	return a;
    }
    int main()
    {
    	LL n,m,j,i,t,k;
    	scanf("%lld",&t);
    	while(t--)
    	{
    		scanf("%lld%lld",&n,&m);
    		if(n>m||n==0||m%n)
    		{
    			printf("-1
    ");
    			continue;
    		}
    		if(n==m)
    		{
    			printf("0
    ");
    			continue;
    		}
    		int flag=1;
    		k=0;
    		while(n!=m)
    		{
    			k++;
    		    if(gcd(m/n,n)==1)
    		    {
    		    	flag=0;
    		    	break;
    		    }
    			n*=gcd(m/n,n);
    		}
    		if(flag)
    		printf("%lld
    ",k);
    		else
    		printf("-1
    ");
    	}
    	return 0;
    } 
    

      

  • 相关阅读:
    C++进程通信之命名管道
    从Win32过渡到MFC工程
    Windows常用消息处理与自定义消息
    Windows窗口开发原理(窗口的创建&消息机制)
    _T、_TEXT、TEXT、L的使用记录
    几种多线程同步方式总结
    异步编程之async&await
    rpc理解
    docker 基础namespace cgroup overlayfs network
    python编程书籍资料整理大全
  • 原文地址:https://www.cnblogs.com/tonghao/p/5040270.html
Copyright © 2011-2022 走看看