# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
760154 | drdilyor | Party (INOI20_party) | C++17 | 1 ms | 468 KiB |
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;
using ll = long long;
#ifdef ONPC
#define debug(args...) cout << "["<< #args << "]= "; debug_out(args);
#else
#define debug(args...) 42;
#endif
void debug_out() {
cout << endl;
}
template<typename H, typename... T>
void debug_out(H h, T... t) {
cout << h;
if (sizeof...(t)) cout << ", ";
debug_out(t...);
}
constexpr int mod = 1e9 + 7;
ll modpow(ll a, ll b) {
a %= mod;
ll res = 1;
while (b) {
if (b&1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
ll inv(ll x) {
return modpow(x, mod-2);
}
ll pw2(ll x) {
return x < 0 ? 0 :(1LL << x);
}
ll sub(ll a, ll b) {
a -= b;
return a < 0 ? a + mod : a;
}
ll solve(ll n) {
ll k = __lg(n+1);
if ((1ll<<k)-1 != n) return -1;
debug(n, k);
auto comb = [&](int chosen)->ll{ return sub(modpow(2, n - chosen), 1); };
ll res = 0;
for (int i = 0; i < k; i++) {
for (int d = 1; d <= k*2; d++) {
int cnt = 0, perim=0;
int lastj = 0;
for (int j = i; j >= 0 && i - j < d; j--) {
lastj = j;
ll size = i == j ? ((1<< (k - j))-1) : (1 << (k - j-1));
int depth = d - (i - j);
ll need = i == j ? pw2(depth) - 1 : (pw2(depth)-1) - (pw2(depth-1)-1);
perim += need >= size ? 0 : i == j ? need+1 : need;
if (perim) assert(need + perim <= size);
cnt += min(size, need);
}
if (lastj) perim++;
if (n == cnt) break;
assert(cnt < n);
ll level = pw2(i);
ll p = comb(cnt);
if (cnt + perim < n)
p -= comb(cnt + perim);
debug(i, d, cnt, level, perim, p);
debug(d*level % mod * p);
res += d * level % mod * p;
res %= mod;
}
}
debug(res, comb(0));
return res * inv(comb(0)) % mod;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int q; cin >> q;
while (q--) {
ll n; cin >> n;
cout << solve(n) << '\n';
}
}
Compilation message (stderr)
# | 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... |