#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 big) {
bool fuck = 0;
int dp[n+1][k+1];
memset(dp,0,sizeof dp);
dp[0][0] = 1;
for (int i=1;i<=n && !fuck;i++) {
for (int j=0;j<=k && !fuck;j++) {
if (!j) {
dp[i][j] = 1;
continue;
}
if (dp[i-1][j-1]+dp[i-1][j] > big) {
fuck = 1;
break;
}
dp[i][j] = dp[i-1][j-1]+dp[i-1][j];
}
}
if (!fuck && dp[n][k] <= big) return dp[n][k];
return -1;
}
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]) {
int wys = nck(n+m-x-y-1,n-x,k);
if (wys==-1) {
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,k);
if (wys == -1) {
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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |