結果

問題 No.3290 Encrypt Failed, but Decrypt Succeeded
ユーザー t98slider
提出日時 2025-10-03 22:01:30
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,084 bytes
コンパイル時間 1,893 ms
コンパイル使用メモリ 198,148 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-10-03 22:01:34
合計ジャッジ時間 3,077 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<typename T>
T ADD(T a, T b){
    T res;
    return __builtin_add_overflow(a, b, &res)? std::numeric_limits<T>::max() : res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    ll k;
    string s;
    cin >> n >> k >> s;
    vector<ll> dp(n + 1);
    dp[n] = 1;
    for(int i = n; i >= 1; i--){
        int v = s[i - 1] - '0';
        if(v >= 1) dp[i - 1] = ADD(dp[i - 1], dp[i]);
        if(i >= 2){
            v += 10 * (s[i - 2] - '0');
            if(s[i - 2] == '0') continue;
            if(v <= 26){
                dp[i - 2] = ADD(dp[i - 2], dp[i]);
            }
        }
    }
    k--;
    string ans;
    for(int i = 0; i < n; i++){
        if(i + 1 < n){
            int v = 10 * (s[i] - '0') + s[i + 1] - '0';
            if(k >= dp[i + 1] && v <= 26){
                k -= dp[i + 1];
                ans += 'a' + v - 1;
                i++;
                continue;
            }
        }
        ans += 'a' + (s[i] - '0') - 1;
    }
    cout << ans << '\n';
}
0