結果

提出番号 1365
提出者 E869120
言語 C++
提出日時 2018-07-20 17:08:33
問題名 (65)Small Grid and Score
結果 AC
点数 100%

テストケース

テストケース 結果 得点 実行時間 メモリ使用量
1 AC 92% 2ms 8512KB
2 AC 92% 3ms 8432KB
3 AC 92% 2ms 8432KB
4 AC 92% 4ms 8352KB
5 AC 92% 3ms 8416KB
6 AC 92% 4ms 7792KB
7 AC 92% 4ms 8448KB
8 AC 92% 2ms 8720KB
9 AC 92% 4ms 8400KB
10 AC 92% 4ms 7920KB
11 AC 92% 2ms 8416KB
12 AC 92% 2ms 8064KB
13 AC 92% 2ms 8688KB
14 AC 92% 3ms 8048KB
15 AC 92% 2ms 8032KB
16 AC 92% 2ms 8448KB
17 AC 92% 5ms 8416KB
18 AC 92% 2ms 8560KB
19 AC 92% 2ms 8656KB
20 AC 92% 2ms 8688KB
21 AC 92% 2ms 8624KB
22 AC 92% 2ms 8448KB
23 AC 92% 2ms 8400KB
24 AC 92% 2ms 8128KB
テストケース 結果 得点 実行時間 メモリ使用量
25 AC 92% 107ms 8416KB
26 AC 92% 144ms 7520KB
27 AC 92% 71ms 7504KB
28 AC 92% 91ms 8672KB
29 AC 92% 76ms 8672KB
30 AC 92% 16ms 8720KB
31 AC 92% 122ms 8400KB
32 AC 92% 15ms 7824KB
33 AC 92% 171ms 8720KB
34 AC 92% 157ms 8432KB
35 AC 92% 38ms 8448KB
36 AC 92% 17ms 7808KB
37 AC 92% 90ms 8560KB
38 AC 92% 147ms 7632KB
39 AC 92% 36ms 8336KB
40 AC 92% 78ms 8400KB
41 AC 92% 161ms 8096KB
42 AC 92% 26ms 8416KB
43 AC 92% 162ms 8448KB
44 AC 92% 42ms 8096KB
45 AC 92% 56ms 8352KB
46 AC 92% 12ms 8048KB
47 AC 92% 48ms 8720KB
48 AC 92% 50ms 8416KB
49 AC 92% 10ms 8416KB
50 AC 92% 146ms 7808KB
51 AC 92% 14ms 8080KB
52 AC 92% 172ms 7824KB
53 AC 92% 10ms 8016KB
54 AC 92% 138ms 8176KB
55 AC 92% 37ms 7824KB
56 AC 92% 42ms 8080KB
57 AC 92% 29ms 8432KB
58 AC 92% 107ms 8688KB
59 AC 92% 17ms 7552KB
60 AC 92% 45ms 7632KB
61 AC 92% 35ms 8640KB
62 AC 92% 72ms 8656KB
63 AC 92% 42ms 8656KB
64 AC 92% 80ms 8432KB
65 AC 92% 118ms 8448KB
66 AC 92% 188ms 7824KB
67 AC 92% 58ms 8656KB
68 AC 92% 58ms 8704KB
69 AC 92% 140ms 7792KB
70 AC 92% 8ms 8464KB
71 AC 92% 80ms 8096KB
72 AC 92% 30ms 8720KB
73 AC 92% 16ms 8720KB
74 AC 92% 50ms 8448KB
75 AC 92% 22ms 8720KB
76 AC 92% 44ms 8416KB
77 AC 92% 28ms 8464KB
78 AC 92% 102ms 8160KB
79 AC 92% 196ms 8720KB
80 AC 92% 30ms 8448KB
81 AC 92% 25ms 8240KB
82 AC 92% 128ms 8720KB
83 AC 92% 93ms 8704KB
84 AC 92% 130ms 8240KB
85 AC 92% 95ms 8736KB
86 AC 92% 127ms 7504KB
87 AC 92% 86ms 8064KB
88 AC 92% 157ms 8432KB
89 AC 92% 114ms 8400KB
90 AC 92% 166ms 7968KB
91 AC 92% 98ms 8400KB
92 AC 92% 9ms 8096KB
93 AC 92% 51ms 7824KB
94 AC 92% 79ms 8016KB
95 AC 92% 16ms 8064KB
96 AC 92% 29ms 8400KB
97 AC 92% 139ms 8656KB
98 AC 92% 94ms 8256KB
99 AC 92% 6ms 8128KB
100 AC 92% 18ms 8352KB
101 AC 92% 25ms 8704KB
102 AC 92% 31ms 7808KB
103 AC 92% 84ms 8064KB
104 AC 92% 33ms 8448KB
105 AC 92% 35ms 7504KB
106 AC 92% 16ms 8672KB
107 AC 92% 57ms 8256KB
108 AC 92% 196ms 7536KB
109 AC 92% 101ms 7856KB
110 AC 92% 144ms 8192KB
111 AC 92% 18ms 8432KB
112 AC 92% 122ms 8432KB
113 AC 92% 52ms 7504KB
114 AC 92% 33ms 8384KB
115 AC 92% 184ms 8432KB
116 AC 92% 42ms 8672KB
117 AC 92% 147ms 7824KB
118 AC 92% 12ms 8048KB
119 AC 92% 122ms 8704KB
120 AC 92% 77ms 8320KB
121 AC 92% 16ms 8304KB
122 AC 92% 155ms 7248KB
123 AC 92% 144ms 8064KB
124 AC 92% 111ms 7824KB

ソースコード

#include <iostream>
#include <vector>
#include <string>
#include <tuple>
#include <algorithm>
using namespace std;

int Rand(int p, int q) {
	int s = 0, t = 1;
	for (int i = 0; i < 3; i++) { s += (rand() % 1024)*t; t *= 1024; }
	return p + s % (q - p + 1);
}

long long dp[44][44], dp2[44][44], dpr[44][44];

pair<vector<string>, long long> solve(long long N) {
	for (int i = 0; i < 44; i++) {
		for (int j = 0; j < 44; j++) {
			if (i == 0 || j == 0) dpr[i][j] = 1;
			else dpr[i][j] = dpr[i - 1][j] + dpr[i][j - 1];
		}
	}
	for (int i = 2; i < 44; i++) {
		long long H = i / 2, W = (i + 1) / 2;

		for (int j = 1; j <= 500; j++) {
			vector<string>F(H, "");
			for (int j = 0; j < H; j++) { for (int k = 0; k < W; k++) F[j] += "."; }

			long long S = Rand(H * W / 2, H * W * 4 / 5 + 1);
			for (int k = 1; k <= S; k++) {
				while (true) {
					int px = Rand(0, H - 1), py = Rand(0, W - 1);
					if (F[px][py] == '.') { F[px][py] = '2'; break; }
				}
			}
			bool OK = false; dp[0][0] = 1; if (F[0][0] == '2') dp[0][0] = 2;
			for (int k = 0; k < H; k++) {
				for (int l = 0; l < W; l++) {
					if (k == 0 && l == 0) continue;
					dp[k][l] = 0;
					if (k >= 1) dp[k][l] += dp[k - 1][l];
					if (l >= 1) dp[k][l] += dp[k][l - 1];
					if (F[k][l] == '2') dp[k][l] *= 2;
					if (dp[k][l] > N) { OK = true; break; }
				}
			}
			if (OK == true || !(N * 5 / 6 <= dp[H - 1][W - 1] && dp[H - 1][W - 1] <= N)) continue;

			dp2[H - 1][W - 1] = 1; if (F[H - 1][W - 1] == '2') dp2[H - 1][W - 1] = 2;
			for (int k = H - 1; k >= 0; k--) {
				for (int l = W - 1; l >= 0; l--) {
					if (k == H - 1 && l == W - 1) continue;
					dp2[k][l] = 0;
					if (k < H - 1) dp2[k][l] += dp2[k + 1][l];
					if (l < W - 1) dp2[k][l] += dp2[k][l + 1];
					if (F[k][l] == '2') dp2[k][l] *= 2;
				}
			}
			long long A = N - dp[H - 1][W - 1];
			vector<tuple<long long, int, int>>vec;
			for (int k = 0; k < H; k++) {
				for (int l = 0; l < W; l++) { if (F[k][l] == '.') vec.push_back(make_tuple(dpr[k][l] * dp2[k][l], k, l)); }
			}
			sort(vec.begin(), vec.end()); reverse(vec.begin(), vec.end());
			for (int k = 0; k < vec.size(); k++) {
				if (A >= get<0>(vec[k])) { F[get<1>(vec[k])][get<2>(vec[k])] = '1'; A -= get<0>(vec[k]); }
			}
			long long SS = 0, V = A;
			while (A >= 2) { if (A % 2 == 1) SS++; A /= 2; SS++; }
			if (SS <= (H + W + 1)) {
				return make_pair(F, V);
			}
		}
	}
	return make_pair(vector<string>{""}, -1);
}

char c[60][60];

void getans(long long N) {
	bool OK=false;if(N%2==0){N/=2;OK=true;}
	for (int i = 0; i < 60; i++) { for (int j = 0; j < 60; j++) c[i][j] = '#'; }
	pair<vector<string>, int>C = solve(N);

	int H = C.first.size(), W = C.first[0].size();
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) { c[i][j + 2] = C.first[i][j]; }
	}
	for (int i = 0; i < H + 2; i++) c[i][0] = '.'; c[0][1] = '.'; c[H][W + 1] = '.';
	for (int i = 0; i < W + 2; i++) c[H + 1][i] = '.';

	vector<int>G; long long V = C.second;
	while (V >= 2) { if (V % 2 == 1) { G.push_back(1); V--; } G.push_back(2); V /= 2; }
	reverse(G.begin(), G.end());

	if (V == 0) { c[1][0] = '#'; }

	int cx = 1, cy = 0;
	for (int i = 0; i < G.size(); i++) { c[cx][cy] = ('0' + G[i]); if (cx < H + 1) cx++; else cy++; }
	if(OK==true) c[0][0]='2';

	cout << H + 2 << " " << W + 2 << endl;
	for (int i = 0; i < H + 2; i++) {
		for (int j = 0; j < W + 2; j++) cout << c[i][j]; cout << endl;
	}
}

int main() {
	long long n; cin >> n;
	if (n == 0) {
		cout << "1 1" << endl;
		cout << "#" << endl;
		return 0;
	}
	getans(n);
	return 0;
}