結果

問題 No.362 門松ナンバー
ユーザー koyumeishikoyumeishi
提出日時 2016-03-16 02:11:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 281 ms / 3,000 ms
コード長 1,978 bytes
コンパイル時間 801 ms
コンパイル使用メモリ 77,308 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-09 19:57:21
合計ジャッジ時間 6,003 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
6,816 KB
testcase_01 AC 99 ms
6,940 KB
testcase_02 AC 99 ms
6,940 KB
testcase_03 AC 51 ms
6,940 KB
testcase_04 AC 26 ms
6,940 KB
testcase_05 AC 150 ms
6,940 KB
testcase_06 AC 263 ms
6,940 KB
testcase_07 AC 130 ms
6,940 KB
testcase_08 AC 161 ms
6,940 KB
testcase_09 AC 244 ms
6,940 KB
testcase_10 AC 278 ms
6,940 KB
testcase_11 AC 276 ms
6,940 KB
testcase_12 AC 279 ms
6,944 KB
testcase_13 AC 278 ms
6,944 KB
testcase_14 AC 279 ms
6,940 KB
testcase_15 AC 277 ms
6,944 KB
testcase_16 AC 277 ms
6,944 KB
testcase_17 AC 270 ms
6,940 KB
testcase_18 AC 279 ms
6,940 KB
testcase_19 AC 245 ms
6,944 KB
testcase_20 AC 281 ms
6,944 KB
testcase_21 AC 29 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//using __int128 , 64bit gcc

#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...);
}

using Int = __int128;

string ltos(Int x){
	string ret;
	if(x==0) return "0";
	while(x){
		ret += '0' + x%10;
		x /= 10;
	}
	reverse(ret.begin(), ret.end());
	return ret;
}

Int cnt_kadomatsu_digit(string s){
	//dp[ x[i-2] ][ x[i-1] ][ is_small ]
	vector<vector<vector<Int>>> dp;
	resize(dp, 11,11, 2);

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

	for(int i=0; i<s.size(); i++){
		vector<vector<vector<Int>>> 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) continue;

				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_);
	}

	Int 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_(){
	string input;
	cin >> input;
	Int n = 0;
	for(Int i=input.size()-1, k=1; i>=0; i--, k*=10){
		n += (input[i]-'0') * k;
	}

	n += cnt_kadomatsu_digit(ltos(99));

	Int lb = 1;
	Int ub = (Int(1))<<120;//Int(1LL<<58)*Int(1LL<<58);
	while(ub-lb>1){
		Int mid = (lb+ub)/2;
		bool ok = cnt_kadomatsu_digit(ltos(mid)) >= n;
		(ok? ub : lb) = mid;
	}

	cout << ltos(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