QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#149748 | #5424. NaN in a Heap | fireince | WA | 34ms | 3628kb | C++14 | 2.7kb | 2023-08-25 13:58:08 | 2023-08-25 13:58:10 |
Judging History
answer
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <tuple>
#include <vector>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, bool> pib;
typedef tuple<int, int, bool> iib;
#define T make_tuple
const int P = 1e9 + 7;
int spes[40];
map<pib, map<int, ll> > f;
int n, D;
int dep(int x) {
int d = 0;
while (x)
x /= 2, d++;
return d;
}
ll fpow(ll a, ll b) {
return b == 0 ? 1 : (b & 1 ? a * fpow(a * a % P, b / 2) % P : fpow(a * a % P, b / 2));
}
void dfs(int x, int spe, int d) {
ll sizl, sizr;
bool s = spe, sl = false, sr = false;
if (f.count({x, s}))
return;
if (x == 1) {
f[{x, s}][0] = 1;
f[{x, s}][1] = 1 * fpow(n, P - 2);
return;
}
if (x == 0) {
f[{x, s}][0] = 1;
return;
}
if (spe) {
if (spes[d + 1] == (spe << 1)) {
sizr = (1 << (D - dep(spe << 1 | 1))) - 1, sizl = x - 1 - sizr;
dfs(sizl, spe << 1, d + 1);
dfs(sizr, 0, d + 1);
sl = true;
} else {
sizl = (1 << (D + 1 - dep(spe << 1))) - 1, sizr = x - 1 - sizl;
dfs(sizl, 0, d + 1);
dfs(sizr, spe << 1 | 1, d + 1);
sr = true;
}
} else {
dfs(x / 2, spe, d + 1);
sizl = sizr = (x - 1) / 2;
}
f[{x, s}][0] = f[{sizl, sl}][0] * f[{sizr, sr}][0] % P * fpow(x, P - 2) % P;
f[{x, s}][x] = f[{sizl, sl}][0] * f[{sizr, sr}][0] % P * fpow(n, P - 2) % P;
for (auto p : f[{sizl, sl}]) {
if (p.first == 0)
continue;
f[{x, s}][p.first] += p.second * f[{sizr, sr}][0] % P * fpow(x - p.first, P - 2) % P;
f[{x, s}][p.first] %= P;
}
for (auto p : f[{sizr, sr}]) {
if (p.first == 0)
continue;
f[{x, s}][p.first] += p.second * f[{sizl, sl}][0] % P * fpow(x - p.first, P - 2) % P;
f[{x, s}][p.first] %= P;
}
// cout << x << "," << s << ": " << endl;
// for (auto p : f[{x, s}]) {
// cout << p.first << " " << p.second << endl;
// }
}
void solve() {
cin >> n;
f.clear();
memset(spes, 0, sizeof(spes));
D = dep(n);
if ((n - 1) & n) {
int t = n / 2, i = D - 1;
while (t) {
spes[i] = t;
i--;
t /= 2;
}
}
bool s = spes[1] == 1;
dfs(n, s, dep(1));
ll ans = 0;
for (auto p : f[{n, s}]) {
if (p.first != 0)
ans = (ans + p.second) % P;
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3628kb
input:
5 1 3 7 10 20221218
output:
1 666666672 55555556 596445110 3197361
result:
ok 5 number(s): "1 666666672 55555556 596445110 3197361"
Test #2:
score: -100
Wrong Answer
time: 34ms
memory: 3460kb
input:
1000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101...
output:
1 0 666666672 416666670 633333338 97222223 55555556 0 123456791 596445110 229888169 878681664 673617560 436681157 699563287 0 902106812 308824953 904504598 779800169 693423362 328728506 466956451 68896808 88594095 156207594 533144330 758445723 92289701 206321076 267778621 0 481273698 84771521 422955...
result:
wrong answer 2nd numbers differ - expected: '1', found: '0'