結果
問題 | No.294 SuperFizzBuzz |
ユーザー |
![]() |
提出日時 | 2015-10-23 23:12:57 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,644 bytes |
コンパイル時間 | 803 ms |
コンパイル使用メモリ | 80,624 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-23 11:45:41 |
合計ジャッジ時間 | 1,325 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#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;}