Acm hdu 3353 not so flat after all (simple question)

Source: Internet
Author: User
Not so flat after all

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 517 accepted submission (s): 209

Problem descriptionany positive integer v can be written as p1a1 * P2 A2 *... PNAN where Pi is a prime number and AI> = 0. For example: 24 = 23*31.
Pick any two prime numbers P1 and P2 where P1 <> P2. imagine a two dimen1_plane where the powers of P1 are plotted on the X-axis and the powers of P2 on the yaxis. now any number that can be written as p1a1 * P2 A2 can be plotted on this plane at location (x, y) = (a1, a2 ). the figure on the right shows few examples where p1 = 3 and P2 = 2.

This idea can be extended for any n-dimen1_space where each of the N axes is assigned a unique prime number. each N-dimen1_space has a unique set of Primes. we call such set the space identification set or S for short. | S | (the ordinal of S) is N.
Any number that can be expressed as a multiplication of pi s (each raised to a power (AI> = 0) can be plotted in this | S |-dimen1_space. the figure at the bottom strates this idea for n = 3 and S = {2, 3, 7 }. needless to say, any number that can be plotted on space a can also be plotted on Space B as long as SA sb.
We define the distance between any two points in a given n-dimen1_space to be the sum of units traveled to get from one point to the other while following the grid lines (I. e. movement is always parallel to one of the axes .) for example, in the figure below, the distance between 168 and 882 is 4.
Given two positive integers, write a program that determines the minimum ordinal of a space where both numbers can be plotted in. The program also determines the distance betwethese two integers in that space.

Inputyour program will be tested on one or more test cases. Each test case is specified on a line with two positive integers (0 <a, B <1,000,000) where a * B> 1.
The last line is made of two zeros.

Outputfor each test case, print the following line:
K. X: d
Where k is the test case number (starting at one,) x is the minimum ordinal needed in a space that both A and B can be plotted in. D is the distance between these two points.
Note: There is a blank space before X.

Sample input168 882
770 792
0 0

Sample output1. 3: 4
2. 5: 6

Source2009 anarc

Recommendlcy
 # Include  <  Stdio. h  >  
# Include < Math. h >
# Include < Iostream >
# Include < String . H >
Using Namespace STD;
# Define Maxn 1000000
Int Prime [maxn], num;
Bool Notprime [maxn];
Int Pa [maxn], ma [maxn];
Int Pb [maxn], MB [maxn];
Void Prime ()
{
Int I, J;
Num = 0 ;
Memset (notprime, False , Sizeof (Notprime ));
For (I = 2 ; I < Maxn; I ++ )
If ( ! Notprime [I])
{
Prime [num ++ ] = I;
For (J = I + I; j < Maxn; j + = I)
Notprime [J] = True ;
}
}
Int Main ()
{
Int A, B;
Int Na, NB;
Int I, j, T;
Int Icase = 0 ;
Prime ();
While (Scanf ( " % D " , & A, & B ))
{
If ( = 0 && B = 0 ) Break ;
Icase ++ ;
Na = NB = 0 ;
For (I = 0 ; I < Num && A > 0 ; I ++ )
{
If ( % Prime [I] = 0 )
{
T = 0 ;
While ( % Prime [I] = 0 )
{
T ++ ;
A /= Prime [I];
}
Pa [Na] = Prime [I];
Ma [Na ++ ] = T;
}
}
For (I = 0 ; I < Num && B > 0 ; I ++ )
{
If (B % Prime [I] = 0 )
{
T = 0 ;
While (B % Prime [I] = 0 )
{
T ++ ;
B /= Prime [I];
}
Pb [Nb] = Prime [I];
MB [Nb ++ ] = T;
}
}
Int X = 0 , D = 0 ;
I = 0 ; J = 0 ;
While (I < Na && J < NB)
{
If (PA [I] = Pb [J])
{
X ++ ;
D + = ABS (MA [I] - MB [J]);
I ++ ;
J ++ ;
}
Else If (PA [I] < Pb [J])
{
X ++ ;
D + = Ma [I];
I ++ ;
}
Else
{
X ++ ;
D + = MB [J];
J ++ ;
}
}
While (I = Na && J < NB)
{
X ++ ;
D + = MB [J];
J ++ ;
}
While (I < Na && J = NB)
{
X ++ ;
D + = Ma [I];
I ++ ;
}
Printf ( " % D. % d: % d \ n " , Icase, x, D );
}
Return 0 ;< BR >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.