#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)
{
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 solve()
{
i64 res = 1;
for (int i = 0; i < 62; i++)
{
i64 need = (1ll << i);
i64 now = value[i] / x;
if (now < need)
continue;
now -= need;
if (now >= need - 1)
{
// 아래에서 자유롭게 선택 가능한 경우
cnt[i] = res;
res *= 2;
continue;
}
// 내려가면서, bit 위치 1인 경우 센다.
int fst = -1;
for (int bit = i - 1; bit >= 0; bit--)
{
if ((now & (1ll << bit)) == 0)
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 = lim & ((1ll << bit) - 1);
i64 now = value[bit] / x;
lim = min(lim, now);
for (int j = bit - 1; j >= 0; j--)
{
if ((lim & (1ll << j)) == 0)
continue;
// 100..00 반영
res += 1 + calc(j, lim);
break;
}
return res;
}
i64 x;
vector<i64> value;
vector<i64> cnt;
};
i64 count_tastiness(i64 x, vector<i64> a)
{
Solver s(x, a);
return s.solve();
}
Compilation message
biscuits.cpp: In constructor 'Solver::Solver(i64, const std::vector<long long int>&)':
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++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |