This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "biscuits.h"
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#define all(x) (x).begin(), (x).end()
#define xx first
#define yy second
using namespace std;
template<typename T, typename Pr = less<T>>
using pq = priority_queue<T, vector<T>, Pr>;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
struct Solver
{
Solver(i64 xv, const vector<i64>& a)
: x(xv), value(62), cnt(62), maxBit(0)
{
for (int i = 0; i < a.size(); i++)
value[i] = (1ll << i) * a[i];
for (int i = 1; i < 62; i++)
value[i] += value[i - 1];
i64 maxv = value[61];
while ((1ll << maxBit) <= maxv / x)
maxBit++;
}
i64 solve()
{
i64 res = 1;
for (int i = 0; i < maxBit; i++)
{
i64 need = x * (1ll << i);
i64 now = value[i];
if (now < need)
continue;
now -= need;
// 내려가면서, bit 위치 1인 경우 센다.
int fst = -1;
for (int bit = i - 1; bit >= 0; bit--)
{
if (now < x * (1ll << bit) || value[bit] < x * (1ll << bit))
continue;
fst = bit;
break;
}
if (fst != -1)
cnt[i] = calc(fst, now);
else
cnt[i] = 1;
res += cnt[i];
}
return res;
}
i64 calc(int bit, i64 lim)
{
i64 res = 1; // 000..0
for (int j = 0; j < bit; j++)
res += cnt[j];
lim -= x * (1ll << bit);
i64 now = value[bit] - (x * (1ll << bit));
lim = min(lim, now);
bool isNxt = false;
for (int j = bit - 1; j >= 0; j--)
{
if (lim < x * (1ll << j) || value[j] < x * (1ll << j))
continue;
res += calc(j, lim);
isNxt = true;
break;
}
if (!isNxt)
res++;
return res;
}
i64 x;
int maxBit;
vector<i64> value;
vector<i64> cnt;
};
i64 count_tastiness(i64 x, vector<i64> a)
{
Solver s(x, a);
return s.solve();
}
Compilation message (stderr)
biscuits.cpp: In constructor 'Solver::Solver(i64, const std::vector<long long int>&)':
biscuits.cpp:111:14: warning: 'Solver::cnt' will be initialized after [-Wreorder]
111 | vector<i64> cnt;
| ^~~
biscuits.cpp:109:6: warning: 'int Solver::maxBit' [-Wreorder]
109 | int maxBit;
| ^~~~~~
biscuits.cpp:32:2: warning: when initialized here [-Wreorder]
32 | Solver(i64 xv, const vector<i64>& a)
| ^~~~~~
biscuits.cpp:35:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i = 0; i < a.size(); i++)
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |