結果

問題 No.362 門松ナンバー
ユーザー mamekinmamekin
提出日時 2016-04-18 00:01:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 3,000 ms
コード長 2,182 bytes
コンパイル時間 1,288 ms
コンパイル使用メモリ 113,748 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 07:19:38
合計ジャッジ時間 2,408 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,376 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

bool checkKadomatsu(const vector<int>& a)
{
    if(a[0] == a[2])
        return false;
    return (a[0] < a[1] && a[2] < a[1]) || (a[0] > a[1] && a[2] > a[1]);
}

long long solve(long long k)
{
    vector<vector<vector<long long> > > dp(1, vector<vector<long long> >(10, vector<long long>(10, 1)));
    for(int i=1; ; ++i){
        dp.push_back(vector<vector<long long> >(10, vector<long long>(10, 0)));
        for(int b=0; b<=9; ++b){
            for(int a=0; a<=9; ++a){
                for(int c=0; c<=9; ++c){
                    if(checkKadomatsu({c, b, a})){
                        dp[i][c][b] += dp[i-1][b][a];
                    }
                }
            }
        }

        int a, b;
        for(b=1; b<=9; ++b){
            for(a=0; a<=9; ++a){
                if(k > dp[i][b][a])
                    k -= dp[i][b][a];
                else
                    break;
            }
            if(a < 10)
                break;
        }

        if(b < 10){
            long long ans = b * 10 + a;
            for(int j=i-1; j>=0; --j){
                for(int c=0; ; ++c){
                    if(checkKadomatsu({b, a, c})){
                        if(k > dp[j][a][c]){
                            k -= dp[j][a][c];
                        }
                        else{
                            ans *= 10;
                            ans += c;
                            b = a;
                            a = c;
                            break;
                        }
                    }
                }
            }
            return ans;
        }
    }
}

int main()
{
    int t;
    cin >> t;

    while(--t >= 0){
        long long k;
        cin >> k;
        cout << solve(k) << endl;
    }

    return 0;
}
0