Submission #676562

# Submission time Handle Problem Language Result Execution time Memory
676562 2022-12-31T09:23:11 Z QwertyPi K-th path (IZhO11_kthpath) C++14
0 / 100
1 ms 432 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][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

kthpath.cpp: In function 'int32_t main()':
kthpath.cpp:27:21: warning: iteration 30 invokes undefined behavior [-Waggressive-loop-optimizations]
   27 |   C[i][0] = C[i][i] = 1;
      |             ~~~~~~~~^~~
kthpath.cpp:26:19: note: within this loop
   26 |  for(int i = 1; i <= 60; i++){
      |                 ~~^~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 432 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -