HDOJ 5505-GT and numbers

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 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 .

T1000 , 1N1000000 , 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
 

Source
BestCoder Round #60
 解题思路:
辗转相除法,注意定义不要溢出就行了。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
unsigned long long i,j,k,l,m,n,help;
int gcd(unsigned long long m,unsigned long long n)
{//求最大公约数 
	help=1;
	while(help!=0)
	{
		help=m%n;
		m=n;
		n=help;
	}
	return m;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int flag=0;
		scanf("%I64d%I64d",&n,&m);
		if(m<n||m%n||n==0)
		{
			printf("-1\n");
			continue; 
		}
		if(n==m)
		{
			printf("0\n");
			continue;
		}
		int ans=0;
		while(n!=m)
		{
			if(gcd(m/n,n)==1)
			{
				flag=1;
				break;
			}
			n=n*(gcd(m/n,n));
			ans++;
		}
		if(flag==0)
		printf("%d\n",ans);
		else
		printf("-1\n");
	}
	return 0;
}

你可能感兴趣的:(数学,hdoj)