結果
問題 | No.2167 Fibonacci Knapsack |
ユーザー |
|
提出日時 | 2022-12-20 19:22:56 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 11,935 bytes |
コンパイル時間 | 3,608 ms |
コンパイル使用メモリ | 247,892 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-11-18 02:03:00 |
合計ジャッジ時間 | 4,631 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | WA * 21 |
ソースコード
#ifndef HIDDEN_IN_VS // 折りたたみ用// 警告の抑制#define _CRT_SECURE_NO_WARNINGS// ライブラリの読み込み#include <bits/stdc++.h>using namespace std;// 型名の短縮using ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;using Graph = vvi;// 定数の定義const double PI = acos(-1);const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)const vi DY = { 0, 1, 0, -1 };int INF = 1001001001; ll INFL = 4004004004004004004LL;double EPS = 1e-12;// 入出力高速化struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;// 汎用マクロの定義#define all(a) (a).begin(), (a).end()#define sz(x) ((int)(x).size())#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了// 汎用関数の定義template <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら trueを返す)template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら trueを返す)// 演算子オーバーロードtemplate <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }// 手元環境(Visual Studio)#ifdef _MSC_VER#include "local.hpp"// 提出用(gcc)#elseinline int popcount(int n) { return __builtin_popcount(n); }inline int popcount(ll n) { return __builtin_popcountll(n); }inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }#define gcd __gcd#define dump(...)#define dumpel(v)#define dump_list(v)#define dump_list2D(v)#define input_from_file(f)#define output_to_file(f)#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }#endif#endif // 折りたたみ用//--------------AtCoder 専用--------------#include <atcoder/all>using namespace atcoder;//using mint = modint1000000007;using mint = modint998244353;//using mint = modint; // mint::set_mod(m);istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;//----------------------------------------//【部分和問題(数え上げ)】O(n v)/** 非負整数列 a[0..n) の部分和として i∈[0..v] を作る方法が何通りあるかを cnt[i] に格納し cnt を返す.**(和を状態にもつ状態 DP)*/template <class T>vector<T> count_partial_sum(const vi& a, int v) {// 参考 : https://qiita.com/suisen_cp/items/794f24d31852b97d58a6int n = sz(a);// dp[i][j] : a[0..i) の中で和がちょうど j という状態をとる場合の数vector<vector<T>> dp(n + 1, vector<T>(v + 1));dp[0][0] = 1; // 空和が 0 であることに対応// 貰う DPrep(i, n) {repi(j, 0, v) {// i 番目の数を選ばない場合dp[i + 1][j] = dp[i][j];// i 番目の数が j より大きいと選べない.if (j < a[i]) continue;// i 番目の数を選ぶ場合を加算する.dp[i + 1][j] += dp[i][j - a[i]];}}return dp[n];}//【フィボナッチ数】O(n)/** フィボナッチ数のリスト fib[0..n) を返す(fib[0]=0, fib[1]=1 とする.)*/template <class T>vector<T> fibonacci(int n) {// verify : https://atcoder.jp/contests/tenka1-2012-qualA/tasks/tenka1_2012_qualA_1vector<T> fib(n);fib[0] = 0;fib[1] = 1;repi(i, 2, n - 1) fib[i] = fib[i - 1] + fib[i - 2];return fib;}void zikken() {int n = 25;auto a = fibonacci<int>(n);a.erase(a.begin());a.erase(a.begin());dump(a.back());auto res = count_partial_sum<ll>(a, a.back());// dump_list(res);repe(x, a) cerr << res[x] << " ";cerr << endl;exit(0);}/*{1, 1, 1, 2, 1, 2, 2, 1, 3, 2, 2, 3, 1, 3, 3, 2, 4, 2, 3, 3, 1, 4, 3, 3, 5, 2, 4}http://oeis.org/A000119定数倍は小さそうだけど O(n)?だとしたら重さ = 価値に設定されると枝刈りが効かずバックトラッキングでは TLE する.(←誤り)1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12n がフィボナッチ数なら分割の仕方は O(log n) で抑えられる.*///【フィボナッチ進法表示】/** Fibonacci_representation(ll n) : O(log n)* n 以下の整数のフィボナッチ進法表示を求められるよう初期化する.** ll fibonacci(int i) : O(1)* i 番目のフィボナッチ数 fib[i] を得る(fib[0] = 0, fib[1] = 1 とする.)** vi get_digits(ll n) : O(log n)* n のフィボナッチ進法表示を返す.(下位から順)* 桁の数は {0, 1} であり,1 は連続せず,下 2 桁は常に "00" である.*/class Fibonacci_representation {int m;vl fib;public:// n 以下の整数のフィボナッチ進法表示を求められるよう初期化する.Fibonacci_representation(ll n) {// verify : https://atcoder.jp/contests/arc122/tasks/arc122_cfib = vl{ 0, 1 }; m = 2;while (fib[m - 1] <= n) {fib.push_back(fib[m - 1] + fib[m - 2]);m++;}}// i 番目のフィボナッチ数 fib[i] を得る(fib[0] = 0, fib[1] = 1 とする.)ll fibonacci(int i) {Assert(0 <= i && i < m);return fib[i];}// n のフィボナッチ進法表示を返す(下位から順)vi get_digits(ll n) {// verify : https://atcoder.jp/contests/arc122/tasks/arc122_cif (n == 0) return vi{ 0 };int i = 2;while (fib[i] <= n) i++;vi ds(i);i--;while (i >= 2) {if (fib[i] <= n) {ds[i] = 1;n -= fib[i];}else ds[i] = 0;i--;}ds[1] = ds[0] = 0;return ds;}};void zikken2() {Fibonacci_representation FR((ll)1e18);dump(FR.get_digits(6728), 23);dump(FR.get_digits(6764), 1);dump(FR.get_digits(6765), 10);dump("----");dump(FR.get_digits(6739), 30);ll x = FR.fibonacci(6) + FR.fibonacci(8);ll y = FR.fibonacci(4) + FR.fibonacci(6) + FR.fibonacci(8) + FR.fibonacci(10) + FR.fibonacci(12);dump(FR.get_digits(x), x, 5);dump(FR.get_digits(y), y, 6);exit(0);}/*0 0 0 0 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 23 = 3 * 6 + 50 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 10----0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 30 = 5 * 60 0 0 0 0 0 1 0 1 29 50 0 0 0 1 0 1 0 1 0 1 0 1 231 6*/ll naive(int n, ll W, vl w) {auto v = fibonacci<int>(n + 2);v.erase(v.begin(), v.begin() + 2);// dp[i][j] : 品物 [i..n) の中で価値ちょうど j を実現できる最小重さvector<map<ll, ll>> dp(n + 1);dp[n][0] = 0;repir(i, n - 1, 0) {repe(tmp, dp[i + 1]) {ll j, w_min;tie(j, w_min) = tmp;// 品物 i を選ばない場合if (dp[i].count(j)) chmin(dp[i][j], w_min);else dp[i][j] = w_min;// 品物 i を選ぶ場合if (dp[i].count(j + v[i])) chmin(dp[i][j + v[i]], w_min + w[i]);else dp[i][j + v[i]] = w_min + w[i];}}dumpel(dp);ll res = 0;repe(tmp, dp[0]) {ll j, w_min;tie(j, w_min) = tmp;if (w_min <= W) chmax(res, j);}return res;}/*15 71 2 3 4 50: (0,0) (1,1) (2,2) (3,3) (4,4) (5,4) (6,5) (7,6) (8,5) (9,6) (10,7) (11,8) (12,9) (13,9) (14,10) (15,11) (16,12) (17,13) (18,14) (19,15)1: (0,0) (2,2) (3,3) (5,4) (7,6) (8,5) (10,7) (11,8) (13,9) (15,11) (16,12) (18,14)2: (0,0) (3,3) (5,4) (8,5) (11,8) (13,9) (16,12)3: (0,0) (5,4) (8,5) (13,9)4: (0,0) (8,5)5: (0,0)10*/ll solve(int n, ll W, vl w) {auto v = fibonacci<int>(n + 4);v.erase(v.begin(), v.begin() + 2);// dp[i][j] : 品物 [i..n) の中で価値ちょうど j を実現できる最小重さvector<map<ll, ll>> dp(n + 1);dp[n][0] = 0;ll v_lb = 0;repir(i, n - 1, 0) {repe(tmp, dp[i + 1]) {ll j, w_min;tie(j, w_min) = tmp;// 品物 i を選ばない場合if (j + v[i + 1] - 1 >= v_lb) {if (dp[i].count(j)) chmin(dp[i][j], w_min);else dp[i][j] = w_min;}// 品物 i を選ぶ場合if (j + v[i] + v[i + 1] - 1 >= v_lb && w_min + w[i] <= W) {if (dp[i].count(j + v[i])) chmin(dp[i][j + v[i]], w_min + w[i]);else dp[i][j + v[i]] = w_min + w[i];chmax(v_lb, j + v[i]);}}}dumpel(dp);ll res = 0;repe(tmp, dp[0]) {ll j, w_min;tie(j, w_min) = tmp;if (w_min <= W) chmax(res, j);}return res;}/*15 71 2 3 4 50: (9,6) (10,7)1: (7,6) (8,5) (10,7)2: (5,4) (8,5)3: (5,4) (8,5)4: (0,0) (8,5)5: (0,0)10118 67391 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 41810: (6738,6738) (6739,6739)1: (6738,6738) (6739,6739)2: (6736,6736) (6739,6739)3: (6736,6736) (6739,6739)4: (6723,6723) (6731,6731) (6739,6739)5: (6723,6723) (6731,6731)6: (6697,6697) (6710,6710) (6731,6731)7: (6676,6676) (6710,6710)8: (6676,6676) (6710,6710)9: (6532,6532) (6621,6621) (6710,6710)10: (6532,6532) (6621,6621)11: (6155,6155) (6388,6388) (6621,6621)12: (6155,6155) (6388,6388)13: (5168,5168) (5778,5778) (6388,6388)14: (5168,5168) (5778,5778)15: (2584,2584) (4181,4181) (5778,5778)16: (2584,2584) (4181,4181)17: (0,0) (4181,4181)18: (0,0)6739効かないと思ってた枝刈りがめちゃめちゃ効いてる.簡単に実装できるんだからとりあえずやってみるべきだった.*/int main() {// input_from_file("input.txt");// output_to_file("output.txt");// zikken2();//【解説 AC】// 効かないと思って考えてなかった枝刈りの方針でいけるみたい.int t;cin >> t;rep(hoge, t) {int n; ll W;cin >> n >> W;vl w(n);cin >> w;// dump(naive(n, W, w));cout << solve(n, W, w) << endl;}}