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 consisting of n n digits. After that, he wiped out x x and wrote integer k k instead, which was the concatenation of binary representations of digits x x consists of (without leading zeroes). For example, let x = 729 x=729 , then k = 111101001 k=111101001 ( ( since 7 = 111 7=111 , 2 = 10 2=10 , 9 = 1001 9=1001 ) ) .

After some time, uncle Bogdan understood that he doesn’t know what to do with k k and asked Denis to help. Denis decided to wipe last n n digits of k k and named the new number as r r .

As a result, Denis proposed to find such integer x x of length n n that r r ( ( as number ) ) is maximum possible. If there are multiple valid 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 or k ) k) as numbers ( ( despite what representations they are written in ) ) , so 729 < 1999 729<1999 or 111 < 1000 111<1000 .

Input

The first line contains a single integer t t ( 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 ( 1 n 105 ) (1≤n≤105) — the length of the integer x x you need to find.

It’s guaranteed that the sum of n n from all test cases doesn’t exceed 2 1 0 5 2⋅10^5 .

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

2
1
3

o u t p u t output

8
998

N o t e Note
In the second test case (with n = 3 n=3 ), if uncle Bogdan had x = 998 x=998 then k = 100110011000 k=100110011000 . Denis ( ( by wiping last n = 3 n=3 digits ) ) will obtain r = 100110011 r=100110011 .

It can be proved that the 100110011 100110011 is the maximum possible r r Denis can obtain and 998 998 is the minimum x x to obtain it.

题意

对于长度 n n 的正整数 x x ,定义一个 k k ,是 n n 中的每一位数的二进制组成,假设 x = 729 x=729 ,那么 7 ( 2 ) = 111 7_{(2)}=111 , 2 ( 2 ) = 10 2_{(2)}=10 , 9 ( 2 ) = 1001 9_{(2)}=1001 ,所以 k = 111101001 k=111101001
r = 111101 r=111101
题目要求:给定一个 n n ,求出最小的 x x ,使得对应的r最大。

题解

要使得 r r 最大,那么未被抹除的 k k 前面几个数字,自然是越大越好,所以全部取 9 9
那么我们讨论的就是抹除掉的那几位。
9 ( 2 ) = 1001 9_{(2)}=1001 8 ( 2 ) = 1000 8_{(2)}=1000 7 ( 2 ) = 111 7_{(2)}=111
7 7 只有 3 3 位,很明晰会使得 r r 减小。所以只能考虑 8 8 9 9
既然被抹去了, 8 8 9 9 ,都无所谓了。
要求最小的 x x ,那么自然选 8 8 了。
分析完毕,上代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
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;
}

话说这代码怎么看起来,这道题那么简单呢?我怎么做了那么久呢?值得深思,好吧,菜是原罪!!!

猜你喜欢

转载自blog.csdn.net/m0_46272108/article/details/107933983
今日推荐