#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll comb[63][63];
int N, M;
ll K;
char G[33][33];
string ans;
ll dp[2][33][33];
ll calc() {
for(int x = (int)ans.size() - 1; x >= 0; x--) {
int c = x & 1;
int p = c ^ 1;
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
dp[c][i][j] = 0;
if(G[i][j] != ans[x]) continue;
if(x == (int)ans.size() - 1) {
dp[c][i][j] = comb[N + M - 2 - i - j][M - 1 - j];
continue;
}
if(i < N - 1) dp[c][i][j] += dp[p][i + 1][j];
if(j < M - 1) dp[c][i][j] += dp[p][i][j + 1];
}
}
}
return dp[0][0][0];
}
int main() {
comb[0][0] = 1;
for(int i = 1; i < 63; i++) {
comb[i][0] = 1;
for(int j = 1; j <= i; j++) {
comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
}
}
std::ios::sync_with_stdio(false);
cin >> N >> M;
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
cin >> G[i][j];
}
}
cin >> K;
for(int i = 0; i < N + M; i++) {
for(int j = 0; j < 26; j++) {
ans.push_back('a' + j);
ll t = calc();
if(K <= t) break;
K -= t;
ans.pop_back();
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
17 ms |
384 KB |
Output is correct |
11 |
Correct |
8 ms |
384 KB |
Output is correct |
12 |
Correct |
17 ms |
384 KB |
Output is correct |
13 |
Correct |
30 ms |
384 KB |
Output is correct |
14 |
Correct |
36 ms |
432 KB |
Output is correct |
15 |
Correct |
65 ms |
384 KB |
Output is correct |
16 |
Correct |
65 ms |
420 KB |
Output is correct |
17 |
Correct |
56 ms |
504 KB |
Output is correct |
18 |
Correct |
54 ms |
384 KB |
Output is correct |
19 |
Correct |
59 ms |
384 KB |
Output is correct |
20 |
Correct |
46 ms |
384 KB |
Output is correct |