# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
676562 | QwertyPi | K-th path (IZhO11_kthpath) | C++14 | 1 ms | 432 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>
#define x first
#define y second
#define int long long
#define inf (1LL << 60)
using namespace std;
char c[31][31];
int C[61][31];
int _C(int l, int r){
return C[l + r][l];
}
int sat_add(int x, int y){
return min(inf, x + y);
}
int sat_mul(int x, int y){
return min((__int128_t) inf, (__int128_t) x * y);
}
int32_t main(){
int n, m; cin >> n >> m;
C[0][0] = 1;
for(int i = 1; i <= 60; i++){
C[i][0] = C[i][i] = 1;
for(int j = 1; j < i; j++){
C[i][j] = min(inf, C[i - 1][j - 1] + C[i - 1][j]);
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> c[i][j];
}
}
int k; cin >> k;
string s; s.push_back(c[0][0]);
map<pair<int, int>, int> pos, npos;
pos[{0, 0}] = 1;
for(int i = 1; i <= n + m - 1; i++){
map<char, int> M;
for(auto i : pos){
if(i.x.x + 1 < n) M[c[i.x.x + 1][i.x.y]] = sat_add(M[c[i.x.x + 1][i.x.y]], sat_mul(_C(n - 1 - (i.x.x + 1), m - 1 - i.x.y), i.y));
if(i.x.y + 1 < m) M[c[i.x.x][i.x.y + 1]] = sat_add(M[c[i.x.x][i.x.y + 1]], sat_mul(_C(n - 1 - i.x.x, m - 1 - (i.x.y + 1)), i.y));
}
for(auto i : M){
if(k <= i.second){
s.push_back(i.first);
break;
}
k -= i.second;
}
for(auto i : pos){
if(i.x.x + 1 < n && c[i.x.x + 1][i.x.y] == s.back()) npos[{i.x.x + 1, i.x.y}] = sat_add(npos[{i.x.x + 1, i.x.y}], pos[{i.x.x, i.x.y}]);
if(i.x.y + 1 < m && c[i.x.x][i.x.y + 1] == s.back()) npos[{i.x.x, i.x.y + 1}] = sat_add(npos[{i.x.x, i.x.y + 1}], pos[{i.x.x, i.x.y}]);
}
swap(pos, npos);
npos.clear();
}
cout << s << endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |