結果

問題 No.362 門松ナンバー
ユーザー parukiparuki
提出日時 2016-06-09 20:41:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 344 ms / 3,000 ms
コード長 2,276 bytes
コンパイル時間 1,911 ms
コンパイル使用メモリ 167,892 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-01 13:28:13
合計ジャッジ時間 6,686 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
4,380 KB
testcase_01 AC 46 ms
4,380 KB
testcase_02 AC 46 ms
4,380 KB
testcase_03 AC 24 ms
4,380 KB
testcase_04 AC 14 ms
4,384 KB
testcase_05 AC 77 ms
4,380 KB
testcase_06 AC 143 ms
4,376 KB
testcase_07 AC 87 ms
4,376 KB
testcase_08 AC 133 ms
4,380 KB
testcase_09 AC 226 ms
4,376 KB
testcase_10 AC 336 ms
4,384 KB
testcase_11 AC 317 ms
4,380 KB
testcase_12 AC 317 ms
4,376 KB
testcase_13 AC 332 ms
4,376 KB
testcase_14 AC 315 ms
4,380 KB
testcase_15 AC 325 ms
4,376 KB
testcase_16 AC 293 ms
4,376 KB
testcase_17 AC 251 ms
4,380 KB
testcase_18 AC 344 ms
4,380 KB
testcase_19 AC 112 ms
4,380 KB
testcase_20 AC 343 ms
4,380 KB
testcase_21 AC 37 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

int T;
ll K;
// [i桁目まで見た][numより小さいことが確定][前の前の数][前の数] = 場合の数
ll dp[15][2][11][10];

bool checkKado(int a, int b, int c){
    if(a == b || b == c || c == a)return false;
    if(a == 10)return true;
    return (a<b&&b>c)||(a>b&&b<c);
}

ll count(ll num){
    string digits = to_string(num);
    if(sz(digits) < 3)return 0;
    each(c, digits)c -= '0';
    MEM(dp, 0);
    for(int i = 1; i < digits[0]; ++i){
        dp[1][1][10][i] = 1;
    }
    dp[1][0][10][digits[0]] = 1;
    FOR(i, 1, digits.size()){
        rep(lessThanNum, 2){
            rep(prepre, 11){
                rep(pre, 10){
                    ll val = dp[i][lessThanNum][prepre][pre];
                    rep(cur, 10){
                        if(!lessThanNum && cur > digits[i]){
                            continue;
                        }
                        if(!checkKado(prepre, pre, cur)){
                            continue;
                        }
                        dp[i + 1][lessThanNum || cur < digits[i]][pre][cur] += val;
                    }
                }
            }
        }
    }
    
    ll result = 0;
    rep(lessThanNum, 2)rep(pre, 10)rep(prepre, 10){
        result += dp[sz(digits)][lessThanNum][prepre][pre];
    }
    if(sz(digits) > 3){
        result += count(stoll(string(sz(digits) - 1, '9')));
    }
    return result;
}

void solve(){
    ll ng = 99, ok = 37294859064823ll, mid;
    while(ok - ng > 1){
        mid = (ok + ng) / 2;
        ll cnt = count(mid);
        (cnt >= K ? ok : ng) = mid;
    }
    cout << ok << endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    while(cin >> T){
        while(T--){
            cin >> K;
            solve();
        }
    }
}
0