Codeforces660 B. Captain Flint and a Long Voyage(思维)

Captain Flint and a Long Voyage

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

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

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

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

It’s guaranteed that the sum of n from all test cases doesn’t exceed 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
input
2
1
3
output
8
998

思路:
1. 保证r最大首选二进制为4位的8(1000)和9(1001)。
2. 保证x最小考虑n,只要n个数删到了该十进制末尾的1,十进制选择8或9是没区别的(换句话说只要删到该数字就选8,删不到则能使r更大,选9)。
代码:

#include
#define ll long long
#define LL long long
#define PB push_back
#define MP make_pair
using namespace std;
const int maxn=2e5+100;
const ll inf=1e18+10;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
        cin>>n;
        int num=(n+3)/4;
        int k=0;
        for(int i=0;i<n-num;i++)cout<<9;
        for(int i=0;i<num;i++)cout<<8;
        puts("");
	}
	return 0;
}

你可能感兴趣的:(二进制,思维)