Alice got a hold of the calculator that can display n digits. She was bored enough to come up with the following time waster.
She enters a number k then repeatedly squares it until the result overflows. When the result is overflows, only the n most significant digits is displayed on the screens and an error flag appears. Alice can clear the error and continue squaring the displayed number. She got bored by this soon enough, but wondered:
"Given N and K, what is the largest number I can get to wasting time in this manner?"
Input
The first line of the input contains an integer t (1≤t≤200), the number of test cases. Each test case contains-integers n (1≤n≤9) and K (0≤k < 10n) where n is the number of digits this calculator Can display k is the starting number.
Output
For each test case, print the maximum number, which Alice can get by repeatedly squaring the starting number as described.
Sample Input
2
1 6
2 99
Sample Output
9
99
problem-solving ideas: The title means: There is an old computer, can only display n digits. One day, you are bored, so enter an integer k, and then repeat the square until the overflow. Each time the overflow occurs, the computer displays the highest n bits of the result and an error mark. Then clear the mark and continue the square. If you keep doing this, what is the maximum number you can get? For example, when n=1,k=6, the computer will show 6, 3 (the highest bit of 36), 9, 8 (the highest bit of 81), 6 (the highest bit of 64), 3, ...
Program code:
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<set>UsingNamespace Std;IntNext(int n,int k){LongLong Res=(LongLong) K*k;While(Res>= N) Res/=10;return res;}IntMain(){int T;scanf("%d",&t);For(int I=0; I< (T); I++){int nK;scanf("%d%d",&n,&k); N=(Int)Pow(10.0N);Computes the N power assignment of 10 to Nint ans=1<<31;Set<int>Ss;While(1){ans=Max(ansK);If(SS.Countbreakelse ss. Insert= next (n ); printf ( "% D\n ") ; return 0;< Span class= "Sh-cbracket" >
designing efficient algorithms [examples]~c-calculator conundrum