Codeforces #660 (Div. 2) B. Captain Flint and a Long Voyage


B. Captain Flint and a Long Voyage
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
Input
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.
Output
For each test case, print the minimum integer x of length n such that obtained by Denis number r is maximum possible.
Example
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 nの正の整数x x xについて、n n nの各ビット数のバイナリ組成であるk kを定義し、x=729 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)}=10009(2)=10001なので、k=111101001 k=11101001 k=11101001はr=111101 r=11101 r=11101 r=11101は、対応するrが最大となるように、n n nを1つ与え、最小のx xを求める.
問題解
r r rを最大にするには、消去されていないk kの前のいくつかの数字は、自然に大きいほど良いので、すべて9 9 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は3 3 3ビットしかなく,r r rを明瞭に減少させる.だから8 8 8 8と9 9 9しか考えられません.消された以上、8 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;
}

ところで、このコードはどうして見えますか.この問題はそんなに簡単ですか.どうしてそんなに長くやったの?深く考えるに値する、よし、料理は原罪だ!!!