結果

問題 No.362 門松ナンバー
コンテスト
ユーザー ei1333333
提出日時 2016-04-17 23:25:01
言語 C++11
(gcc 15.3.0 + boost 1.92.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 57 ms / 3,000 ms
+ 763µs
コード長 1,304 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 920 ms
コンパイル使用メモリ 179,220 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2026-09-10 03:30:40
合計ジャッジ時間 3,327 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int64 dp[2][3][11][10][500];  
string s;  
 
int64 rec(int idx, bool free, int pree, int pre, int pre_up)
{
  if(idx == s.size()) return(1);  
  if(~dp[free][pre_up][pree][pre][idx]) return dp[free][pre_up][pree][pre][idx];
  int64 ret = 0, end = free ? 9 : s[idx] - '0';  
  for(int i = 0; i <= end; i++) {
#define next(l) rec(idx + 1, free|(i != end), pre, i, l)  
    if(pre_up == 1 && pre <= i) continue;  
    if(pre_up == 2 && pre >= i) continue;
    if(pre_up == 0){
      if(pre && pre == i) continue;
      if(pre == 0) ret += next(0);  
      else if(pre < i) ret += next(1);  
      else if(pre > i) ret += next(2);  
    } else if(pre_up == 1) {
      if(pree == i) continue;
      ret += next(2);
    } else {
      if(pree == i) continue;
      ret += next(1);
    }
  }  
  return dp[free][pre_up][pree][pre][idx] = ret;
}  

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

  while(T--) {
    int64 K;
    cin >> K;
    int64 low = 0, high = 37294859064823;
    while(high - low > 0) {
      int64 mid = (high + low) >> 1LL;
      stringstream sss; sss << mid;
      s = sss.str();
      memset(dp, -1, sizeof(dp));
      if(rec(0, 0, 0, 0, 0) - 91  < K) low = mid + 1;
      else high = mid;
    }
    cout << low << endl;
  }
}
0