Submission #573618

#TimeUsernameProblemLanguageResultExecution timeMemory
573618SSRSK-th path (IZhO11_kthpath)C++14
100 / 100
1 ms300 KiB
#include <bits/stdc++.h> using namespace std; int main(){ int N, M; cin >> N >> M; vector<vector<char>> A(N, vector<char>(M)); for (int i = 0; i < N; i++){ for (int j = 0; j < M; j++){ cin >> A[i][j]; } } long long K; cin >> K; K--; vector<vector<long long>> dp(N, vector<long long>(M, 0)); dp[N - 1][M - 1] = 1; for (int i = N - 1; i >= 0; i--){ for (int j = M - 1; j >= 0; j--){ if (i < N - 1){ dp[i][j] += dp[i + 1][j]; } if (j < M - 1){ dp[i][j] += dp[i][j + 1]; } } } string ans; ans += A[0][0]; vector<vector<long long>> dp2(N, vector<long long>(M, 0)); dp2[0][0] = 1; for (int i = 0; i < N + M - 2; i++){ vector<long long> cnt(26, 0); for (int j = max(i + 1 - M, 0); j < min(i + 1, N); j++){ int k = i - j; if (j < N - 1){ cnt[A[j + 1][k] - 'a'] += dp[j + 1][k] * dp2[j][k]; } if (k < M - 1){ cnt[A[j][k + 1] - 'a'] += dp[j][k + 1] * dp2[j][k]; } } int p = 0; for (int j = 0; j < 26; j++){ if (K >= cnt[j]){ K -= cnt[j]; p++; } else { break; } } ans += 'a' + p; for (int j = max(i + 1 - M, 0); j < min(i + 1, N); j++){ int k = i - j; if (j < N - 1){ if (A[j + 1][k] == 'a' + p){ dp2[j + 1][k] += dp2[j][k]; } } if (k < M - 1){ if (A[j][k + 1] == 'a' + p){ dp2[j][k + 1] += dp2[j][k]; } } } } cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...