NEERC2015(计数)

题意:有2^m个人,随机安排他们进行2^m淘汰赛,每个人有一个编号和一个值a,且编号大的人一定胜过编号小的人,每次比赛的收益是双方的a值的乘积,求总收益的期望

令n=2^m,然后把1-n的a翻转过来,变成编号小的人一定胜过编号大的人(这么假设是因为窝一开始读错题意了qwq

本质还是计数问题,总共有n!种情况,计数就行了。。考虑一对人的贡献,考虑他们在高度为k的子树的根中相遇,那么

\sum_{i=1}^{n}\sum_{j=i+1}^{n}\sum_{k=1}^{m}2^{k-1}A_{j-1}^{2^{k-1}-1}2^{k-1}A_{i-2^{k-1}-1}^{2^{k-1}-1}a[i]a[j]*2*2^{n-k}

由于j\geqslant 2^{k-1}\&\&i\geqslant 2^k,上式可以化为

\sum_{k=1}^{m}\sum_{j=2^{k-1}}^{n}\sum_{i=max\{j+1,2^k\}}^{n}2^{k-1}A_{j-1}^{2^{k-1}-1}2^{k-1}A_{i-2^{k-1}-1}^{2^{k-1}-1}a[i]a[j]*2*2^{n-k}\\= \sum_{k=1}^{m}2^{n+k-1}\sum_{j=2^{k-1}}^{n}A_{j-1}^{2^{k-1}-1}a[j]\sum_{i=max\{j+1,2^k\}}^{n}A_{i-2^{k-1}-1}^{2^{k-1}-1}a[i]

枚举k的时候维护A_{i-2^{k-1}-1}^{2^{k-1}-1}a[i]的前缀和就行了。。然后复杂度是O(nm)=O(nlogn)

 

 

 

/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */ 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll  long long
#define eps 1e-12
#define succ(x) (1<

 

 

 

E. Entertainment

time limit per test

3.0 s

memory limit per test

512 MB

input

standard input

output

standard output

The Berland King is hosting a fencing tournament once again. A total of 2k best fighters from all over the country came to the capital of Berland to compete for the glory and — of course — for formidable cash prizes.

Every fighter is given a number from 1 to 2k according to his mastery: the strongest fighter is labeled 1, the second strongest fighter is labeled 2 and so on to the weakest fighter, who is labeled 2k. A standard play-off scheme is implemented: the players are randomly put in the leaves of the tournament bracket which is a full binary tree with 2k leaves. All initial dispositions are equiprobable. Then the matches are played between all pairs of players sharing a parent in the tournament bracket, with winners advancing to the next stage and losers being eliminated from the tournament. This process continues until there is only one player remaining. It's easy to see that the participants and the results of all games are determined by the initial disposition.

Actually, there is no intrigue: the fighter with the lower number always beats the fighter with the higher number. Still, the matches are entertaining, and some fighters are more fun to watch than the others. More precisely, i-th fighter has an amusement level of ai; note that some fighters may not be among the strongest, but tend to play amusing matches nonetheless, meaning that ai doesn't depend from i in any way. If the players with numbers i and j play a match, it has spectacularity equal to ai·aj. The entertainment of the whole tournament is defined as the sum of spectacularity of all matches to play.

The number of tickets sold for the matches (and thus, the amount of money made) heavily relies on the spectacularity of the matches. The seeding is not announced yet, and the King requested you to calculate the expectation of tournament's entertainment. Note that the value depends only on the random seeding as all the matches' outcomes are predetermined.

Input

The first line contains the only integer k (1 ≤ k ≤ 18) — the number of tournament rounds.

The next line contains 2k space-separated numbers a1, ..., a2k (0 ≤ ai ≤ 109) — amusement levels of the fighters.

Output

Let the expectation of total spectacularity of all matches be equal to the irreducible fraction P / Q. Print the value of P·Q - 1 in the prime field of integers modulo 1 000 000 007 (109 + 7). It is guaranteed that this modulo does not divide Q, thus the number to be printed is well-defined.

Examples

Input

Copy

1
2 3

Output

Copy

6

Input

Copy

2
1 2 3 4

Output

Copy

14

Input

Copy

2
4 3 2 1

Output

Copy

333333358

Note

In the third sample the expectation is .

你可能感兴趣的:(组合数学)