Submission #676563

# Submission time Handle Problem Language Result Execution time Memory
676563 2022-12-31T09:23:28 Z QwertyPi K-th path (IZhO11_kthpath) C++14
100 / 100
1 ms 340 KB
#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
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 300 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 308 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct