QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#284761 | #5424. NaN in a Heap | C1942huangjiaxu | AC ✓ | 23ms | 3912kb | C++14 | 2.2kb | 2023-12-16 14:56:49 | 2023-12-16 14:56:50 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int P=1e9+7;
struct node{
int sz,dp[61];
}O;
node f[31];
int T,n,cnt,sz[61],ls[61],rs[61],id[61],inv[61][61],tot,rt,nw;
inline int md(int x){
return x>=P?x-P:x;
}
int ksm(int x,int y){
int res=1;
for(;y;y>>=1,x=1ll*x*x%P)if(y&1)res=1ll*res*x%P;
return res;
}
inline node operator +(node a,node b){
node c=O;
c.sz=nw;
c.dp[0]=1ll*a.dp[0]*b.dp[0]%P;
int i=1,j=1;
c.dp[c.sz]=1ll*a.dp[0]*b.dp[0]%P;
for(int i=1;i<=cnt;++i)if(sz[i]<sz[c.sz])c.dp[i]=(1ll*a.dp[i]*b.dp[0]+1ll*b.dp[i]*a.dp[0])%P;
for(int i=0;i<=cnt;++i)if(c.dp[i]&&i!=c.sz)c.dp[i]=1ll*c.dp[i]*inv[c.sz][i]%P;
return c;
}
struct val{
int w,i,j;
};
inline void Inv(bool fg=false){
vector<val>tmp;
for(int i=0;i<=cnt;++i)for(int j=0;j<=cnt;++j)if((fg||i>30||j>30)&&sz[i]>sz[j])tmp.emplace_back(val{sz[i]-sz[j],i,j});
if(tmp.empty())return;
for(int i=1;i<tmp.size();++i)tmp[i].w=1ll*tmp[i].w*tmp[i-1].w%P;
tmp.back().w=ksm(tmp.back().w,P-2);
for(int i=tmp.size()-1;~i;--i){
int u=tmp[i].w;
if(i)tmp[i].w=1ll*u*tmp[i-1].w%P;
inv[tmp[i].i][tmp[i].j]=tmp[i].w;
if(i)tmp[i-1].w=1ll*u*(sz[tmp[i].i]-sz[tmp[i].j])%P;
}
}
void init(){
for(int i=0;i<=30;++i)sz[i]=(1<<i)-1;
cnt=30,Inv(true);
f[0].sz=1,f[0].dp[0]=f[0].dp[1]=1;
for(int i=1;i<30;++i)nw=i+1,f[i]=f[i-1]+f[i-1];
}
void getinv(int n,int &x){
x=++tot;
if(!n)return;
int t=1,o=0;
while(2*t+1<=n)t=2*t+1,++o;
if(t==n)return;
else id[x]=++cnt,sz[cnt]=n;
if((n-t-1)<(t>>1))t>>=1;
getinv(t,ls[x]),getinv(n-t-1,rs[x]);
}
node calc(int n,int x){
if(!n){
node a=O;
a.sz=0,a.dp[0]=1;
return a;
}
int t=1,o=0;
while(2*t+1<=n)t=2*t+1,++o;
if(t==n)return f[o];
if((n-t-1)<(t>>1))t>>=1;
node L=calc(t,ls[x]),R=calc(n-t-1,rs[x]);
nw=id[x];
return L+R;
}
void solve(){
scanf("%d",&n);
for(int i=0;i<=tot;++i)ls[i]=rs[i]=id[i]=0;
for(int i=31;i<=cnt;++i)sz[i]=0;
cnt=30,rt=tot=0;
getinv(n,rt),Inv();
node res=calc(n,rt);
int ans=0;
for(int i=1;i<=cnt;++i)if(res.dp[i])ans=md(ans+res.dp[i]);
printf("%d\n",1ll*ans*ksm(n,P-2)%P);
}
int main(){
scanf("%d",&T);
init();
while(T--)solve();cerr<<1000*clock()/CLOCKS_PER_SEC<<endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3856kb
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: 5ms
memory: 3776kb
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: 23ms
memory: 3912kb
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: 23ms
memory: 3828kb
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