# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
488982 | SirCovidThe19th | K-th path (IZhO11_kthpath) | C++17 | 1 ms | 332 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
int n, m; ll k, ways[35][35], choose[65][65]; pll range[35][35];
char A[35][35]; vector<char> ans;
int main(){
cin >> n >> m;
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> A[i][j];
cin >> k;
for (int i = 0; i < 65 and (choose[i][0] = 1); i++)
for (int j = 1; j <= i; j++)
choose[i][j] = choose[i - 1][j] + choose[i - 1][j - 1];
ans.push_back(A[1][1]); ways[1][1] = 1; range[1][1] = {1, choose[n + m - 2][n - 1]};
for (int len = 2; len <= n + m - 1; len++){
vector<pii> test; ll rangeSt = 0, tot = 0;
for (int i = min(len, n), j = max(len - n + 1, 1); i and j <= m; i--, j++){
pll R = {};
if (ways[i - 1][j]) R = range[i - 1][j];
if (ways[i][j - 1]) R = range[i][j - 1];
if (k >= R.f and k <= R.s){ //a potential spot
test.push_back({i, j}); rangeSt = R.f;
ways[i][j] += ways[i - 1][j] + ways[i][j - 1];
}
}
for (char c = 'a'; c <= 'z'; c++){
for (pii i : test) if (A[i.f][i.s] == c)
tot += ways[i.f][i.s] * choose[(n - i.f) + (m - i.s)][(m - i.s)];
for (pii i : test) if (A[i.f][i.s] == c){
pll R = {rangeSt, rangeSt + tot - 1};
if (k >= R.f and k <= R.s){
if (ans.size() < len) ans.push_back(c);
range[i.f][i.s] = R;
}
else ways[i.f][i.s] = 0; //bad spot
}
rangeSt += tot; tot = 0;
}
}
for (char c : ans) cout<<c;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |