結果

問題 No.294 SuperFizzBuzz
ユーザー koyumeishikoyumeishi
提出日時 2015-10-23 23:12:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,644 bytes
コンパイル時間 772 ms
コンパイル使用メモリ 80,228 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-30 18:05:31
合計ジャッジ時間 1,682 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

template<class T>
ostream& operator << (ostream& os, vector<T> vec){
	for(int i=0; i<vec.size(); i++){
		os << vec[i] << " ";
	}
	return os;
}

long long sfb_cnt(string s){

	vector<vector<long long>> dp(2, vector<long long>(15, 0));
	dp[0][0] = 1;
	for(int i=0; i<s.size(); i++){
		vector<vector<long long>> dp_(2, vector<long long>(15, 0));
		for(int smaller=0; smaller<2; smaller++)
		for(int mod_15=0; mod_15<15; mod_15++)
		if(dp[smaller][mod_15]) for(int d:{3,5}){
			if(smaller == 0 && d>(s[i]-'0')) continue;
			int next_smaller = smaller || d<(s[i]-'0');
			int next_mod = (mod_15*10+d)%15;

			dp_[next_smaller][next_mod] += dp[smaller][mod_15];			
		}

		if(i>0) for(int d:{3,5}){
			dp_[1][d] += 1;
		}
		swap(dp, dp_);

	}

	return dp[0][0] + dp[1][0];
}

int main(){
	long long n;
	cin >> n;

	int len = 3;
	while(1){
		string s(len, '5');
		if(sfb_cnt(s) >= n){
			break;
		}
		len++;
	}

	//cerr << len << " " << sfb_cnt(string(len, '5')) << endl;

	auto check = [&](long long bits){
		string s(len, '3');
		for(int i=0; i<len; i++){
			if((bits>>i)&1) s[len-1-i] = '5';
		}
		long long cnt = sfb_cnt(s);
		//cerr << s << " " << cnt << endl;
		return cnt >= n;
	};

	long long lb = 0;
	long long ub = 1LL<<len;
	while(ub-lb>1){
		long long mid = (lb+ub)/2;
		bool ok = check(mid);
		(ok? ub : lb) = mid;
	}

	string s(len, '3');
	for(int i=0; i<len; i++){
		if((ub>>i)&1) s[len-1-i] = '5';
	}

	cout << s << endl;
	return 0;
}

0