#include<bits/stdc++.h>
using namespace std;
char a[35][35];
string answer;
long double f[65];
long double n, m, k;
long double fact (int a) {
if (a <= 1) return 1;
if (f[a]) return f[a];
return f[a] = a * fact(a-1);
}
long double ways (int i, int j) {
return ( fact(n-i+m-j) / fact(m-j) ) / fact(n-i);
}
void ans (int i = 1, int j = 1) {
if (i == n) {
for (int x = j; x <= m; x++) answer += a[i][x];
return;
}
if (j == m) {
for (int x = i; x <= n; x++) answer += a[x][j];
return;
}
// cerr << ways(i+1, j) << ' ' << ways(i, j+1) << ' ' << k << endl;
answer += a[i][j];
if (a[i+1][j] < a[i][j+1]) {
if (ways(i+1, j) >= k) return ans(i+1, j);
k-=ways(i+1, j);
return ans(i, j+1);
}
else {
if (ways(i, j+1) >= k) return ans(i, j+1);
k-=ways(i, j+1);
return ans (i+1, j);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
cin >> k;
ans(); cout << answer;
return 0;
}
/* POSSIBLE BUGS
overflow
*/
/* TESTCASE
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |