Submission #622806

#TimeUsernameProblemLanguageResultExecution timeMemory
622806ereklePacking Biscuits (IOI20_biscuits)C++17
0 / 100
1093 ms380 KiB
#include "biscuits.h"
#include <bits/stdc++.h>

using namespace std;

const int MX = 60;

bool multOverflow(long long a, long long b) {
	if (!a || !b) return false;
	long long c = a*b;
	if (a == c/b) return false;
	return true;
}

long long count_tastiness(long long x, vector<long long> a) {
	int k = a.size();
	k = 60;
	while (a.size() < k) a.push_back(0);
	long long maxSum = 0;
	for (int i = 0; i < k; ++i) {
		maxSum += (1LL << i) * a[i];
	}

	auto possible = [&k, &a, &maxSum, &x](const bitset<MX> &y) {
		long long extra = maxSum;
		for (int i = 0; i < k && extra >= 0; ++i) {
			if (y[i]) {
				if (multOverflow(x, (1 << i))) return false;
				extra -= x*(1 << i);
			}
		}
		if (extra < 0) return false;
		long long need = 0; // as a multiple of current power of two
		for (int i = k-1; i >= 0; --i) {
			if (y[i]) need += x;
			long long use = min(need, a[i]);
			need -= use;
			need <<= 1;
		}
		return !need;
	};

	bitset<MX> y = 0;
	long long ans = 1;
	bool foundNext = true;
	while (foundNext) {
		foundNext = false;
		// find next highest y-value that works
		for (int i = 0; i < k; ++i) {
			if (y[i]) continue;
			// change a 0 into a 1, and set all smaller bits to 0
			bitset<MX> z = y;
			z[i] = 1;
			for (int j = i-1; j >= 0; --j) z[j] = 0;
			if (possible(z)) {
				++ans, y = z;
				foundNext = true;
				break;
			}
		}
	}
	return ans;
}

Compilation message (stderr)

biscuits.cpp: In function 'long long int count_tastiness(long long int, std::vector<long long int>)':
biscuits.cpp:18:18: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   18 |  while (a.size() < k) a.push_back(0);
      |         ~~~~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...