#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define heap priority_queue
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define mem(s, x) memset(s, x, sizeof (s))
template <class X, class Y>
inline bool maximize(X &x, Y y) {
return (x < y ? x = y, true : false);
}
template <class X, class Y>
inline bool minimize(X &x, Y y) {
return (x > y ? x = y, true : false);
}
template <class X>
inline void compress(X &a) {
sort(all(a));
a.resize(unique(all(a)) - a.begin());
}
template<class X>
inline void printArr(vector<X> a, char spec = ' ', char line = '\n') {
for (X val : a) cout << val << spec;
cout << line;
}
int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcountll(x); }
// const ll oo = (ll) 1e17;
// const int INF = (int) 1e9 + 7, MOD = (int) 1e9 + 7;
// int add(int &x, int y) {
// x += y;
// if (x >= MOD) x -= MOD;
// if (x < 0) x += MOD;
// return x;
// }
// int mult(int x, int y) {
// return 1LL * x * y % MOD;
// }
const ull oo = (ll) 9e18 + 10;
ull add(ull &x, ull y) {
x += y;
if (x >= oo) x = oo;
return x;
}
ull mult(ull x, ull y) {
return x >= oo / y ? oo : x * y;
}
const int N = 2e3 + 15;
int T;
int sub;
int n, m, q;
char a[N][N];
ull dp[N][N], dp_go[N][N];
vector<pair<int, int>> diag[2 * N];
void solve() {
// cin >> sub;
cin >> n >> m;
rep(i, n) rep(j, m) {
cin >> a[i][j];
}
dp[n][m] = 1;
ford(i, n, 1) ford(j, m, 1) {
add(dp[i][j], dp[i + 1][j]);
add(dp[i][j], dp[i][j + 1]);
}
rep(i, n) rep(j, m) {
diag[i + j].pb(mp(i, j));
}
// rep(_, q) {
ull k;
cin >> k;
memset(dp_go, 0, sizeof dp_go);
string res;
res += a[1][1];
dp_go[2][1] = dp_go[1][2] = 1;
vector<ull> value(26);
forn(len, 2, n + m - 1) {
fill(all(value), 0);
for (pair<int, int> i : diag[len + 1]) {
int x = i.first, y = i.second;
add(value[a[x][y] - 'a'], mult(dp_go[x][y], dp[x][y]));
}
forn(i, 0, 25) {
if (value[i] < k) {
k -= value[i];
}
else {
res += char(i + 'a');
break;
}
}
for (pair<int, int> i : diag[len + 1]) {
int x = i.first, y = i.second;
if (a[x][y] != res.back()) continue;
add(dp_go[x + 1][y], dp_go[x][y]);
add(dp_go[x][y + 1], dp_go[x][y]);
}
}
cout << res << '\n';
// }
return;
}
int main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define TASK "task"
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}
Compilation message (stderr)
kthpath.cpp: In function 'int main()':
kthpath.cpp:147:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
kthpath.cpp:148:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |