# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
405362 | my99n | K-th path (IZhO11_kthpath) | C++14 | 1 ms | 204 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;
char a[35][35];
string answer;
long double f[65];
long double n, m, k;
long double fact (int a) {
if (a <= 1) return 1;
if (f[a]) return f[a];
return f[a] = a * fact(a-1);
}
long double ways (int i, int j) {
return ( fact(n-i+m-j) / fact(m-j) ) / fact(n-i);
}
void ans (int i = 1, int j = 1) {
if (i == n) {
for (int x = j; x <= m; x++) answer += a[i][x];
return;
}
if (j == m) {
for (int x = i; x <= n; x++) answer += a[x][j];
return;
}
// cerr << ways(i+1, j) << ' ' << ways(i, j+1) << ' ' << k << endl;
answer += a[i][j];
if (a[i+1][j] < a[i][j+1]) {
if (ways(i+1, j) >= k) return ans(i+1, j);
k-=ways(i+1, j);
return ans(i, j+1);
}
else {
if (ways(i, j+1) >= k) return ans(i, j+1);
k-=ways(i, j+1);
return ans (i+1, j);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
cin >> k;
ans(); cout << answer;
return 0;
}
/* POSSIBLE BUGS
overflow
*/
/* TESTCASE
*/
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |