結果

問題 No.362 門松ナンバー
ユーザー 🐬hec🐬hec
提出日時 2016-03-24 23:42:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 245 ms / 3,000 ms
コード長 2,053 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 161,352 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-09 23:35:40
合計ジャッジ時間 5,660 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
6,812 KB
testcase_01 AC 45 ms
6,940 KB
testcase_02 AC 45 ms
6,940 KB
testcase_03 AC 25 ms
6,944 KB
testcase_04 AC 14 ms
6,940 KB
testcase_05 AC 75 ms
6,944 KB
testcase_06 AC 145 ms
6,944 KB
testcase_07 AC 83 ms
6,944 KB
testcase_08 AC 116 ms
6,944 KB
testcase_09 AC 187 ms
6,944 KB
testcase_10 AC 245 ms
6,944 KB
testcase_11 AC 233 ms
6,944 KB
testcase_12 AC 238 ms
6,944 KB
testcase_13 AC 243 ms
6,944 KB
testcase_14 AC 238 ms
6,940 KB
testcase_15 AC 241 ms
6,940 KB
testcase_16 AC 234 ms
6,944 KB
testcase_17 AC 210 ms
6,940 KB
testcase_18 AC 241 ms
6,944 KB
testcase_19 AC 108 ms
6,940 KB
testcase_20 AC 245 ms
6,940 KB
testcase_21 AC 28 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using namespace std;
using ll=long long;

ll dp[20][11][11][11][2][2]; // is_small is_leading  

bool check(int a,int b,int c){
	if(a==b) return false;
	if(b==c) return false;
	if(c==a) return false;

	if(min({a,b,c})==b) return true;
	if(max({a,b,c})==b) return true;

	return false;
}


ll cnt(ll arg){
	string m=to_string(arg);
	int n=int(m.size());
	clr(dp,0LL);
	dp[0][10][10][10][0][0]=1LL;

	rep(i,n)rep(prv,11)rep(cur,11)rep(nxt,11)rep(nz,2)rep(small,2){
		if(dp[i][prv][cur][nxt][nz][small]==0) continue;
		rep(digit,10){
			int d=m[i]-'0';
			int nprv=cur,ncur=nxt,nnxt=digit;
			int nnz=nz,nsmall=small;
			if(digit!=0) nnz=1;
			if(digit<d) nsmall=1;
			if(nsmall==0 && digit>d) continue;
			if(nprv!=10 && check(nprv,ncur,nnxt)==false) continue;
			if(nnz==0) nnxt=10;
			dp[i+1][nprv][ncur][nnxt][nnz][nsmall]+=dp[i][prv][cur][nxt][nz][small];
		}
	}

	ll ret=0LL;
	rep(prv,10)rep(cur,10)rep(nxt,10)rep(small,2) ret+=dp[n][prv][cur][nxt][1][small];
	return ret;
}


int main(void){
	ll t;
	cin >> t;
	rep(testcase,t){
		ll k;
		cin >> k;

		ll ng=101LL,ok=37294859064823LL;
		while(abs(ok-ng)>1){
			const ll mid=(ng+ok)/2LL;
			if(k<= cnt(mid)) 
				ok=mid;
			else
				ng=mid;
		}	
		cout << ok << endl;
	}
	return 0;
}
0