QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#129034 | #5424. NaN in a Heap | PetroTarnavskyi | AC ✓ | 1163ms | 3616kb | C++17 | 2.3kb | 2023-07-21 19:49:12 | 2023-07-21 19:49:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a); i<(b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FILL(a, b) memset(a, b, sizeof(a))
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int mod = 1e9 + 7;
int add(int a, int b){
return (a + b < mod) ? (a + b) : (a + b - mod);
}
int mult(int a, int b){
return 1LL * a * b % mod;
}
int binpow(int a, int n){
int res = 1;
while(n){
if(n & 1)
res = mult(res, a);
a = mult(a, a);
n /= 2;
}
return res;
}
int ober(int a){
return binpow(a, mod - 2);
}
int Left(int sz){
if(sz == 1)
return 0;
int bit = 31 - __builtin_clz(sz);
int last = min(sz - (1 << bit) + 1, (1 << (bit - 1)));
return last + (1 << (bit - 1)) - 1;
}
int ans = 0;
void solve(int sz, int szNan, int cnt, int prod){
if(sz == szNan){
ans = add(ans, mult(sz, mult(cnt, prod)));
return;
}
if(sz < szNan)
return;
prod = mult(prod, sz);
prod = mult(prod, ober(sz - szNan));
int l = Left(sz);
int r = sz - 1 - l;
if(l == r)
solve(l, szNan, mult(2, cnt), prod);
else{
solve(l, szNan, cnt, prod);
solve(r, szNan, cnt, prod);
}
}
set<int> SZS;
void fin(int sz){
if(sz == 0 || SZS.find(sz) != SZS.end())
return;
SZS.insert(sz);
int l = Left(sz);
int r = sz - 1 - l;
fin(l);
fin(r);
}
int dob;
void g(int sz, int szNan, int cnt){
if(sz == szNan){
dob = mult(dob, binpow(ober(sz), cnt));
return;
}
if(sz < szNan)
return;
int l = Left(sz);
int r = sz - 1 - l;
if(l == r)
g(l, szNan, mult(2, cnt));
else{
g(l, szNan, cnt);
g(r, szNan, cnt);
}
}
void solve(){
int n;
cin >> n;
SZS.clear();
fin(n);
dob = 1;
for(int sz : SZS)
g(n, sz, 1);
ans = 0;
for(int szNan : SZS){
solve(n, szNan, 1, dob);
//cout << szNan << " " << ans << endl;
}
//FOR(i, 1, n + 1)
// ans = mult(ans, i);
cout << mult(ans, ober(n)) << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--)
solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 3540kb
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: 0
Accepted
time: 29ms
memory: 3616kb
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 1 666666672 375000003 633333338 97222223 55555556 822222228 123456791 596445110 229888169 878681664 673617560 436681157 699563287 68610901 902106812 308824953 904504598 779800169 693423362 328728506 466956451 68896808 88594095 156207594 533144330 758445723 92289701 206321076 267778621 266415260 48...
result:
ok 1000 numbers
Test #3:
score: 0
Accepted
time: 1163ms
memory: 3548kb
input:
1000 1000000000 999999999 999999998 999999997 999999996 999999995 999999994 999999993 999999992 999999991 999999990 999999989 999999988 999999987 999999986 999999985 999999984 999999983 999999982 999999981 999999980 999999979 999999978 999999977 999999976 999999975 999999974 999999973 999999972 9999...
output:
191769339 839078655 63430702 488796230 588110810 163742647 964465260 961862258 425060141 344042065 747463620 143548999 281463738 797756640 382302841 365802993 511891059 902367958 70796927 495040138 33561329 728278059 879674300 992542203 309248753 251669085 188046077 672990625 635281273 113409431 972...
result:
ok 1000 numbers
Test #4:
score: 0
Accepted
time: 1040ms
memory: 3616kb
input:
1000 524125987 923264237 374288891 535590429 751244358 124321145 232930851 266089174 543529670 773363571 319728747 580543238 582720391 468188689 490702144 598813561 138628383 284660056 733781508 155605777 931759705 245485733 723534730 257812292 794937524 596788519 188451996 981010588 14483682 592676...
output:
726166876 355333772 482635633 758157044 775831523 760255234 748551027 477359472 86466892 226497967 994190156 546377096 39059573 537362710 398171443 66921908 845526053 340839799 621258613 555318917 603009173 528685604 550082786 978381020 650853592 125984432 139054932 319259349 481246666 178000233 799...
result:
ok 1000 numbers