結果

問題 No.362 門松ナンバー
ユーザー ei1333333
提出日時 2016-04-17 23:25:01
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 81 ms / 3,000 ms
コード長 1,304 bytes
コンパイル時間 1,316 ms
コンパイル使用メモリ 162,256 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-04 10:58:54
合計ジャッジ時間 3,192 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#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