Advertisement
erek1e

IOI '20 - Biscuits (9pts)

Mar 5th, 2023
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include "biscuits.h"
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. const int MX = 60;
  7.  
  8. long long count_tastiness(long long x, vector<long long> a) {
  9.     int k = a.size();
  10.     long long maxSum = 0;
  11.     for (int i = 0; i < k; ++i) {
  12.         maxSum += (1LL << i) * a[i];
  13.     }
  14.     k = 1;
  15.     long long p = 1;
  16.     while (p < maxSum) ++k, p <<= 1;
  17.     while (a.size() < k) a.push_back(0);
  18.  
  19.     auto possible = [&k, &a, &maxSum, &x](const bitset<MX> &y) {
  20.         long long need = 0; // as a multiple of current power of two
  21.         for (int i = k-1; i >= 0; --i) {
  22.             if (y[i]) need += x;
  23.             long long use = min(need, a[i]);
  24.             need -= use;
  25.             need <<= 1;
  26.             if (need > maxSum) return false;
  27.         }
  28.         return !need;
  29.     };
  30.  
  31.     bitset<MX> y = 0;
  32.     long long ans = 1;
  33.     bool foundNext = true;
  34.     while (foundNext) {
  35.         foundNext = false;
  36.         bitset<MX> z = y;
  37.         // find next highest y-value that works
  38.         for (int i = 0; i < k; ++i) {
  39.             if (y[i]) {z[i] = 0; continue;}
  40.             // change a 0 into a 1, and set all smaller bits to 0
  41.             z[i] = 1;
  42.             if (possible(z)) {
  43.                 ++ans, y = z;
  44.                 foundNext = true;
  45.                 break;
  46.             }
  47.             z[i] = 0;
  48.         }
  49.     }
  50.     return ans;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement