Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.
In the beginning, uncle Bogdan wrote on a board a positive integer x x x consisting of n n n digits. After that, he wiped out x x x and wrote integer k k k instead, which was the concatenation of binary representations of digits x x x consists of (without leading zeroes). For example, let x = 729 x=729 x=729, then k = 111101001 k=111101001 k=111101001 ( ( (since 7 = 111 7=111 7=111, 2 = 10 2=10 2=10, 9 = 1001 9=1001 9=1001 ) ) ).
After some time, uncle Bogdan understood that he doesn’t know what to do with k k k and asked Denis to help. Denis decided to wipe last n n n digits of k k k and named the new number as r r r.
As a result, Denis proposed to find such integer x x x of length n n n that r r r ( ( (as number ) ) ) is maximum possible. If there are multiple valid x x x then Denis is interested in the minimum one.
All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?
Note: in this task, we compare integers ( x (x (x or k ) k) k) as numbers ( ( (despite what representations they are written in ) ) ), so 729 < 1999 729<1999 729<1999 or 111 < 1000 111<1000 111<1000.
The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 ) (1≤t≤1000) (1≤t≤1000) — the number of test cases.
Next t lines contain test cases — one per test case. The one and only line of each test case contains the single integer n n n ( 1 ≤ n ≤ 105 ) (1≤n≤105) (1≤n≤105) — the length of the integer x x x you need to find.
It’s guaranteed that the sum of n n n from all test cases doesn’t exceed 2 ⋅ 1 0 5 2⋅10^5 2⋅105.
For each test case, print the minimum integer x of length n such that obtained by Denis number r is maximum possible.
i n p u t input input
2
1
3
o u t p u t output output
8
998
N o t e Note Note
In the second test case (with n = 3 n=3 n=3), if uncle Bogdan had x = 998 x=998 x=998 then k = 100110011000 k=100110011000 k=100110011000. Denis ( ( (by wiping last n = 3 n=3 n=3 digits ) ) ) will obtain r = 100110011 r=100110011 r=100110011.
It can be proved that the 100110011 100110011 100110011 is the maximum possible r r r Denis can obtain and 998 998 998 is the minimum x x x to obtain it.
对于长度 n n n的正整数 x x x,定义一个 k k k,是 n n n中的每一位数的二进制组成,假设 x = 729 x=729 x=729,那么 7 ( 2 ) = 111 7_{(2)}=111 7(2)=111, 2 ( 2 ) = 10 2_{(2)}=10 2(2)=10, 9 ( 2 ) = 1001 9_{(2)}=1001 9(2)=1001,所以 k = 111101001 k=111101001 k=111101001
则 r = 111101 r=111101 r=111101
题目要求:给定一个 n n n,求出最小的 x x x,使得对应的r最大。
要使得 r r r 最大,那么未被抹除的 k k k 前面几个数字,自然是越大越好,所以全部取 9 9 9。
那么我们讨论的就是抹除掉的那几位。
9 ( 2 ) = 1001 9_{(2)}=1001 9(2)=1001、 8 ( 2 ) = 1000 8_{(2)}=1000 8(2)=1000、 7 ( 2 ) = 111 7_{(2)}=111 7(2)=111
7 7 7只有 3 3 3位,很明晰会使得 r r r 减小。所以只能考虑 8 8 8和 9 9 9;
既然被抹去了, 8 8 8 和 9 9 9,都无所谓了。
要求最小的 x x x,那么自然选 8 8 8了。
分析完毕,上代码:
#include
#include
#include
#include
#include
typedef long long ll;
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
int T, i;
cin >> n;
T = n / 4;
if (n % 4)
T++;
for (i = 1; i < n - T; ++i)
printf("9");
for (i = 1; i <= T; ++i)
printf("8");
cout << endl;
}
return 0;
}
话说这代码怎么看起来,这道题那么简单呢?我怎么做了那么久呢?值得深思,好吧,菜是原罪!!!