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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
// O(kx)
// O(qklogx)
ll count_tastiness(ll x, vector<ll> a) {
int k = 60;
while ((int)a.size() < k + 1) a.push_back(0);
for (int i = 0; i < k; i++) {
ll z = max(0LL, a[i] - x) / 2;
a[i + 1] += z;
a[i] -= 2 * z;
}
if (x <= 10000) {
int C = x + 2;
vector<ll> dp(C, 0);
dp[0] = 1;
for (int i = 0; i < k; i++) {
vector<ll> ndp(C, 0);
for (int carry = 0; carry < C; carry++) {
int cur = carry + a[i];
ndp[cur / 2] += dp[carry];
if (cur >= x) ndp[(cur - x) / 2] += dp[carry];
}
dp.swap(ndp);
}
ll res = 0;
for (const ll &z : dp) res += z;
return res;
}
ll sum = 0;
for (const ll &z : a) sum += z;
if (sum <= 100000) {
ll res = 0;
for (ll y = 0; y <= 100000; y++) {
auto a_copy = a;
bool can = true;
for (int i = 0; i < k; i++) {
ll need = x * ((y >> i) & 1);
if (a[i] < need) {
can = false;
break;
}
a[i] -= need;
a[i + 1] += a[i] / 2;
}
if (can) res++;
a = a_copy;
}
return res;
}
vector<pair<ll, ll>> dp;
dp.emplace_back(0, 1);
for (int i = 0; i < k; i++) {
vector<pair<ll, ll>> ndp_no_add, ndp_add;
for (const auto &[carry, ways] : dp) {
int cur = a[i] + carry;
ndp_no_add.emplace_back(cur / 2, ways);
if (cur >= x) ndp_add.emplace_back((cur - x) / 2, ways);
}
vector<pair<ll, ll>> tmp;
merge(ndp_no_add.begin(), ndp_no_add.end(), ndp_add.begin(), ndp_add.end(), back_inserter(tmp));
vector<pair<ll, ll>> ndp;
for (const auto &[carry, ways] : tmp) {
if (ndp.empty() || carry != ndp.back().first) ndp.emplace_back(carry, ways); else ndp.back().second += ways;
}
dp.swap(ndp);
}
ll res = 0;
for (const auto &[carry, ways] : dp) res += ways;
return res;
}
#ifdef LOCAL
int main() {
freopen("input.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int k;
ll x;
cin >> k >> x;
vector<ll> a(k);
for (ll &z : a) cin >> z;
cout << count_tastiness(x, a);
return 0;
}
#endif
# | 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... |