結果

問題 No.362 門松ナンバー
ユーザー 0w10w1
提出日時 2017-01-07 21:34:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 3,000 ms
コード長 2,710 bytes
コンパイル時間 1,805 ms
コンパイル使用メモリ 181,420 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-22 16:51:16
合計ジャッジ時間 4,179 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
4,380 KB
testcase_01 AC 24 ms
4,376 KB
testcase_02 AC 23 ms
4,376 KB
testcase_03 AC 13 ms
4,376 KB
testcase_04 AC 7 ms
4,380 KB
testcase_05 AC 37 ms
4,380 KB
testcase_06 AC 66 ms
4,376 KB
testcase_07 AC 37 ms
4,380 KB
testcase_08 AC 51 ms
4,376 KB
testcase_09 AC 81 ms
4,376 KB
testcase_10 AC 103 ms
4,376 KB
testcase_11 AC 100 ms
4,376 KB
testcase_12 AC 100 ms
4,376 KB
testcase_13 AC 101 ms
4,376 KB
testcase_14 AC 101 ms
4,376 KB
testcase_15 AC 102 ms
4,376 KB
testcase_16 AC 98 ms
4,380 KB
testcase_17 AC 85 ms
4,376 KB
testcase_18 AC 107 ms
4,376 KB
testcase_19 AC 55 ms
4,376 KB
testcase_20 AC 109 ms
4,376 KB
testcase_21 AC 13 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef vector< vvl > vvvl;
typedef vector< vvvl > vvvvl;
typedef vector< vvvvl > vvvvvl;
typedef pair< int, int > pii;
typedef vector< pii > vp;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< string > vs;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
  if( x > v ){
    x = v;
    return 1;
  }
  return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
  if( x < v ){
    x = v;
    return 1;
  }
  return 0;
}

const int INF = 0x3f3f3f3f;

int T;

void init(){
  cin >> T;
}

void preprocess(){

}

int kado( int x, int y, int z ){
  if( x == y or y == z or z == x ){
    return 0;
  }
  return min( { x, y, z } ) == y or max( { x, y, z } ) == y;
}

ll dfs( ll v ){
  vi s;
  for( ; v; v /= 10 ){
     s.emplace_back( v % 10 );
  }
  reverse( s.begin(), s.end() );
  vvvvvl dp( s.size() + 1, vvvvl( 2, vvvl( 2, vvl( 10, vl( 10 ) ) ) ) );
  dp[ 0 ][ 0 ][ 0 ][ 0 ][ 0 ] = 1;
  for( int i = 0; i < s.size(); ++i ){
    for( int j = 0; j < 2; ++j ){
      dp[ i + 1 ][ j | 0 < s[ i ] ][ 0 ][ 0 ][ 0 ] += dp[ i ][ j ][ 0 ][ 0 ][ 0 ];
      if( i + 3 <= s.size() ){
        for( int k = 1; k < ( j ? 10 : s[ i ] + 1 ); ++k ){
          for( int l = 0; l < ( ( j or k < s[ i ] ) ? 10 : s[ i + 1 ] + 1 ); ++l ){
            for( int m = 0; m < ( ( j or k < s[ i ] or l < s[ i + 1 ] ) ? 10 : s[ i + 2 ] + 1 ); ++m ){
              if( not kado( k, l, m ) ) continue;
              dp[ i + 3 ][ j | k < s[ i ] or l < s[ i + 1 ] or m < s[ i + 2 ] ][ 1 ][ l ][ m ] += dp[ i ][ j ][ 0 ][ 0 ][ 0 ];
            }
          }
        }
      }
      for( int k = 0; k < 10; ++k ){
        for( int l = 0; l < 10; ++l ){
          for( int d = 0; d < ( j ? 10 : s[ i ] + 1 ); ++d ){
            if( not kado( k, l ,d ) ) continue;
            dp[ i + 1 ][ j | d < s[ i ] ][ 1 ][ l ][ d ] += dp[ i ][ j ][ 1 ][ k ][ l ];
          }
        }
      }
    }
  }
  ll sum = 0;
  for( int i = 0; i < 2; ++i ){
    for( int j = 0; j < 10; ++j ){
      for( int k = 0; k < 10; ++k ){
        sum += dp[ s.size() ][ i ][ 1 ][ j ][ k ];
      }
    }
  }      
  return sum;
} 

void solve(){
  for( int kase = 0; kase < T; ++kase ){
    ll K; cin >> K;
    ll ans = -1;
    for( ll lb = 102LL, rb = 37294859064823LL; lb <= rb; ){
      ll mid = ( lb + rb ) / 2;
      if( dfs( mid ) >= K ){
        ans = mid;
        rb = mid - 1;
      } else{
        lb = mid + 1;
      }
    }
    cout << ans << endl;
  }
}

signed main(){
  ios::sync_with_stdio( 0 );
  init();
  preprocess();
  solve();
  return 0;
}
0