結果

問題 No.362 門松ナンバー
ユーザー koyumeishikoyumeishi
提出日時 2016-03-16 02:08:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 76 ms / 3,000 ms
コード長 1,720 bytes
コンパイル時間 838 ms
コンパイル使用メモリ 76,532 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-09 19:57:00
合計ジャッジ時間 2,755 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
6,816 KB
testcase_01 AC 18 ms
6,940 KB
testcase_02 AC 16 ms
6,940 KB
testcase_03 AC 10 ms
6,940 KB
testcase_04 AC 5 ms
6,940 KB
testcase_05 AC 29 ms
6,940 KB
testcase_06 AC 49 ms
6,940 KB
testcase_07 AC 28 ms
6,940 KB
testcase_08 AC 36 ms
6,940 KB
testcase_09 AC 58 ms
6,940 KB
testcase_10 AC 72 ms
6,944 KB
testcase_11 AC 74 ms
6,940 KB
testcase_12 AC 70 ms
6,944 KB
testcase_13 AC 70 ms
6,944 KB
testcase_14 AC 71 ms
6,940 KB
testcase_15 AC 70 ms
6,940 KB
testcase_16 AC 70 ms
6,944 KB
testcase_17 AC 64 ms
6,940 KB
testcase_18 AC 75 ms
6,940 KB
testcase_19 AC 41 ms
6,940 KB
testcase_20 AC 76 ms
6,944 KB
testcase_21 AC 9 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <set>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;

//resize(vector, v1,v2,...,vn); v[v1][v2]...[vn]
template<typename V, typename H> void resize(vector<V>& vec, const H head){vec.resize(head);}
template<typename V, typename H, typename ... T> void resize(vector<V>& vec, const H& head, const T ... tail){
	vec.resize(head);
	for(auto& v: vec) resize(v, tail...);
}

string ltos(long long x){
	stringstream ss;
	ss << x;
	return ss.str();
}

long long cnt_kadomatsu_digit(string s){
	vector<vector<vector<long long>>> dp;
	resize(dp, 11,11, 2);

	dp[10][10][0] = 1;

	for(int i=0; i<s.size(); i++){
		vector<vector<vector<long long>>> dp_;
		resize(dp_, 11,11, 2);

		int k = s[i] - '0';
		for(int a=0; a<=10; a++)
		for(int b=0; b<=10; b++)
		for(int s=0; s< 2 ; s++){
			if(!dp[a][b][s]) continue;

			for(int c=0; c<10; c++){
				if(s==0 && c>k) break;
				int next_s = s || c<k;
				bool ok = (a<b && b>c && a!=c) || (a>b && b<c && a!=c) || (a==10 || b==10);
				if(ok){
					dp_[b][(b==10&&c==0) ? 10 : c][next_s] += dp[a][b][s];
				}
			}
		}

		swap(dp, dp_);
	}

	long long ret = 0;
	for(int a=0; a<10; a++)
	for(int b=0; b<10; b++)
	for(int s=0; s<2 ; s++){
		ret += dp[a][b][s];
	}

	return ret;
}

int main_(){
	long long n = 0;
	cin >> n;

	n += cnt_kadomatsu_digit("99");

	long long lb = 1;
	long long ub = 500000000000000LL;
	while(ub-lb>1){
		long long mid = (lb+ub)/2;
		bool ok = cnt_kadomatsu_digit(ltos(mid)) >= n;
		(ok? ub : lb) = mid;
	}

	cout << ub << endl;

	return 0;
}

#include <ctime>
int main(){
	//auto s = clock();

	int t;
	cin >> t;
	while(t--){
		main_();
	}

	//cerr << clock()-s << endl;
	return 0;
}
0