HDU 5505 GT and numbers【思考+GCD】

2349 ワード

GT and numbers
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1839    Accepted Submission(s): 500
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是一直变得,因子也会变,写了又超时,又忘了优化一下GCD,GCD竟然也会超时,搜一下简化版的GCD,提交一下又MLE,这真是一道好题,那一点想不到都不行,必须优化到最优的形式,还是多思考吧;


#include

typedef unsigned long long LL;

LL GCD(LL a,LL b)
{
	return !b?a:GCD(b,a%b);
} 
int main()
{
	LL T,M,N,i;
	scanf("%llu",&T);
	while(T--)
	{
		bool falg=true; LL cnt=0;
		scanf("%llu %llu",&N,&M);
	    if(N>M||M%N) {
	    	falg=false; 
		}
		else if(N==M) falg=true;
		else {
		     while(N