fjwc2019 D6T1 堆(组合数+打表)
但是每个点都遍历一遍,有些点的子树完全相同却重复算了
忽然记起完全二叉树的性质之一:每个非叶节点的子树中至少有一个是满二叉树
那么我们预处理满二叉树的那一块,剩下的dfs就可以辣
求阶乘.......分块打表
设打表分成$k$段,则复杂度$O(logn+n/k)$
#include<cstdio> const int mod=1e9+7,W=1e7; const int tab[100]={1,924724006,582347126,500419162,881147799,693776109,435873621,279027658,727951124,398578768,678364145,204828554,345795998,116118093,359401113,236930793,856493327,207383191,617606889,933753281,26701748,329394893,360779992,416008308,187501984,165706817,328891607,16385287,117411011,404196042,765064133,239669664,761588352,566114869,673499119,840260100,352356536,53839501,178657924,373444237,227300165,207172723,444208499,367531373,297449176,605324209,729265513,567907756,125889461,250743107,666666670,598576559,632705086,295855233,185718228,414607857,737215408,863388390,182290465,707552496,881713600,417895708,490627919,364521407,775935292,972492338,473340273,920880265,530581,696910290,64037482,649527920,756691728,283805222,711255329,825205499,263679166,341083474,914727729,919247968,465317279,960145703,274813468,393588827,65909169,521964827,794328994,484551338,521297378,54488990,591837535,255746228,25827429,177799409,92011129,469664591,35708489,197025781,288851931,254032854}; int n,f[31],ans; inline int Pow(int x,int y){ int re=1; for(;y;y>>=1,x=1ll*x*x%mod) if(y&1) re=1ll*re*x%mod; return re; } int dfs(int x){ int i=0; while((1<<i)-1<x) ++i; if((1<<i)-1==x) return f[i]; if(x+(1<<(i-2))<=(1<<i)-1) return 1ll*Pow(x,mod-2)*f[i-2]%mod*dfs(x-(1<<(i-2)))%mod; else return 1ll*Pow(x,mod-2)*f[i-1]%mod*dfs(x-(1<<(i-1)))%mod; } int main(){ freopen("heap.in","r",stdin); freopen("heap.out","w",stdout); scanf("%d",&n); f[0]=1; for(int i=1;i<=30;++i) f[i]=1ll*f[i-1]*f[i-1]%mod*Pow((1<<i)-1,mod-2)%mod; ans=tab[(n-1)/W]; for(int i=(n-1)/W*W+2;i<=n;++i) ans=1ll*ans*i%mod; ans=1ll*ans*dfs(n)%mod; printf("%d",ans); return 0; }