Bingway original works. for reprinting, please indicate the author and source.
I wrote a fun article before. Algorithm The returned results do not contain multiple numbers, which leads to the technical itch of many algorithm experts in the garden. What I see about the returned results that do not contain multiple numbers Article There are several articles. This makes me more confident that garden is a good technology exchange platform. I wrote another algorithm two days ago. The original question is in English. I am not very good at English. I have translated it for a while. I think the effect is still good, but I am afraid that the translation will mislead you, I would like to thank Kun and his English man for their translation. Well, let's talk about it. The previous question is: a number is called a perfect power if it can be written in the Form M ^ K, where M and K are positive integers, and k> 1. given two positive integers A and B, find the two perfect powers between A and B, inclusive, that are closest to each other, and return the absolute difference between them. if less than two perfect powers exist in the interval, return-1 instead. A will be between 1 And 10 ^ 18, inclusive. B will be between a + 1 and 10 ^ 18, inclusive. if a number is in the format of m ^ K, when m and K are both positive integers and K is greater than 1, this number can be called a full power. Given two positive integers A and B, we find that the two full powers are contained between A and B, and the two digits are the closest. And returns an absolute difference between them. If the total power in the range is less than two, the range of-1. A is 1 to 10 ^ 18, and the range of B is a + 1 to 10 ^ 18. Test data: Returns: 1 (1 is full power) Returns:-11,100000000000000 returns: 1 (maximum test range), returns: 80 test data and returned results have certain rules to see which one can find the operation rule. My algorithms: Algorithm
Static Long INF = 2000000000000000000 ;
Static Long Nearestcouple ( Long A, Long B)
{
Long Res = INF;
List < Long > All = New List < Long > ();
If (B = a + 1)
{
Return res = 1;
}
For ( Int K = 2 ;; ++ K)
{
Long Left = Math. Abs (root (A, K )); // Returns the absolute value.
Long Right = Math. Abs (root (B + 1 , K )) - 1 ;
If (Right < 2 )
Break ;
If (K = 2 )
{
If (Left < Right)
{
Res = Math. Min (Res, 2 * Left + 1 );
}
Continue ;
}
For ( Long X = Left; x <= Right; ++ X)
{
Long V = Pow (x, k );
If (V < A | V > B)
Throw New Exception ();
All. Add (v );
Long U = Math. Abs (root (V, 2 ));
Long U2 = Pow (u, 2 );
Long Uu2 = Pow (math. Max ( 1 , U - 1 ), 2 );
Long Uuu2 = Pow (u + 1 , 2 );
If (U2 > A && U2 < B && U2 ! = V)
Res = Math. Min (Res, math. Abs (U2 - V ));
If (Uu2 > A && Uu2 < B && Uu2 ! = V)
Res = Math. Min (Res, math. Abs (uu2 - V ));
If (Uuu2 > A && Uuu2 < B && Uuu2 ! = V)
Res = Math. Min (Res, math. Abs (uuu2 - V ));
}
}
All. Sort ();
For ( Int I = 0 ; I < All. Count - 1 ; ++ I)
If (ALL [I] ! = All [I + 1 ])
Res = Math. Min (Res, math. Abs (ALL [I] - All [I + 1 ]); // Absolute Difference
Return Res = INF ? - 1 : Res; // Determines whether it is less than the full power. If it is less than the full power,-1 is returned; otherwise, RES is returned.
}
Static Long Root ( Long N, Long P)
{
Long Z = Math. Max ( 1 ,( Long ) Math. Pow (n, 1.0 / P) - 2 ); // Returns a large number for comparison.
While (POW (z, P) < N) // Is the P power of Z less than n?
{
++ Z;
}
If (POW (z, P) > N)
{
Return - Z;
}
Else
Return Z;
}
Static Long Pow ( Long A, Long K)
{
If (K = 0 )
{
Return 1 ;
}
Else If (K % 2 = 0 )
{
Long Z = Pow (A, K / 2 );
Return Mul (z, Z );
}
Else
{
Return Mul (A, POW (A, K - 1 ));
}
}
Static Long Mul ( Long A, Long B)
{
If (INF / A < B)
Return INF;
Else
Return A * B;
}< br>