#include <bits/stdc++.h>
using namespace std;
#define sp << " " <<
#define int long long
#define vi vector<int>
#define F(xxx,yyy) for (int xxx=1;xxx<=yyy;xxx++)
#define pii pair<int,int>
const int N = 1e5+1,inf = 1e18;
int nck(int n,int k) {
int p = 1;
for (int i=n;i>=n-k+1;i--) p*=i;
for (int i=1;i<=k;i++) p/=i;
return p;
}
void solve() {
int n,m;
cin >> n >> m;
char grid[n+1][m+1];
F(i,n) {
string s;
cin >> s;
F(j,m) grid[i][j] = s[j-1];
}
int k;
cin >> k;
int x = 1;
int y = 1;
string cur = "a";
while (x<n || y<m) {
if (x == n) {
cur+=grid[x][++y];
continue;
}
if (y == m) {
cur+=grid[++x][y];
continue;
}
if (grid[x][y+1] < grid[x+1][y]) {
//sağa gitmek istiyorum
int wys = nck(n+m-x-y-1,n-x);
if (wys >= k) {
y++;
cur+=grid[x][y];
continue;
}
else {
x++;
cur+=grid[x][y];
k-=wys;
continue;
}
}
else {
int wys = nck(n+m-x-y-1,m-x);
if (wys >= k) {
x++;
cur+=grid[x][y];
continue;
}
else {
y++;
cur+=grid[x][y];
k-=wys;
continue;
}
}
}
cout << cur << endl;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
int t = 1;
//cin >> t;
F(i,t) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |