結果
| 問題 |
No.362 門松ナンバー
|
| コンテスト | |
| ユーザー |
koyumeishi
|
| 提出日時 | 2016-03-16 02:11:59 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 324 ms / 3,000 ms |
| コード長 | 1,978 bytes |
| コンパイル時間 | 907 ms |
| コンパイル使用メモリ | 77,312 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-10-01 20:11:27 |
| 合計ジャッジ時間 | 6,953 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
//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;
}
koyumeishi