# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
676563 | QwertyPi | K번째 경로 (IZhO11_kthpath) | C++14 | 1 ms | 340 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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][61];
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |