Because of the thriller adventure game The 3rd Building, there are fewer and fewer students who would like to go to the 3rd Building. So few students are working in the studio in the 3rd Building. Students are even more reluctant to go to the 3rd Building for experiments, which are also annoying.
Kanade takes responsibility to improve this status. She thinks it a good idea to decorate the ginkgo trees along the road to the 3rd Building, making them cute. There are n ginkgo trees that are planted along the road, numbered with 1…n. Each tree has a cute value. The cute value of tree i is si.
Kanade defines a plan as an ordered pair (i,j), here 1≤i≤j≤n. It means a student will appear at the position of the tree i magically, walk along the road, and finally stop walking at the position of the tree j. The cute level of a plan is the average of the cute value of the trees visited. Formally, the cute level of plan (i,j) is 1j−i+1∑jk=isk.
Kanade wants to know the mathematical expectation of the cute level if a student will take a plan among all these plans in a uniformly random way. But she is busy with learning Computer Networking, would you help her?
Input
The first line of the input contains an integer T — the number of testcases. You should process these testcases independently.
The first line of each testcase contains an integer n — the number of ginkgo trees.
The second line of each testcase contains n integers si — the cute value of each ginkgo tree, space-separated.
1≤T≤20,1≤n≤2×105,1≤si≤109
It is guaranteed that ∑n≤106.
Output
For each testcase, output the answer in the fraction form modulo 109+7 in one line. That is, if the answer is PQ, you should output P⋅Q−1mod(109+7), where Q−1 denotes the multiplicative inverse of Q modulo 109+7.
Sample Input
3
3
1 3 2
6
1 1 4 5 1 4
9
7325 516 56940 120670 16272 15007 337527 333184 742294
Sample Output
83333336
188888893
303405448
当然还是算每个点对期望的贡献
考虑到每个区间被取到的概率相同,设为p,那么s[i]的贡献就是
∑
f
o
r
所
有
通
过
I
的
区
间
x
,
长
度
为
l
(
x
)
p
∗
(
s
[
i
]
/
l
(
x
)
)
\sum_{for 所有通过I的区间x,长度为l(x)} p*(s[i]/l(x))
∑for所有通过I的区间x,长度为l(x)p∗(s[i]/l(x))
这玩意直接算当然会死,那就把它一通乱拆:p和s[i]都出去,剩下的设他为sumi[i],,,,
还是不好算啊???
下面的思路可以让这场能考两次容斥原理:
其实剩下的东西一句话就是通过i的区间的长度的倒数和!!
我才不会告诉你们第一次脑补弄成了直接求和
那这还是好麻烦啊!
那就首先想想正难则反呗!
含
i
的
区
间
的
长
度
倒
数
和
=
全
部
的
区
间
的
长
度
倒
数
和
−
不
含
i
的
区
间
的
长
度
倒
数
和
含i的区间的长度倒数和=全部的区间的长度倒数和-不含i的区间的长度倒数和
含i的区间的长度倒数和=全部的区间的长度倒数和−不含i的区间的长度倒数和!!!!!!!
翻译一下就是
设长度为i的序列的区间长度倒数和=w[i],
sumi[i]=w[n]-w[i-1]-w[n-i]
i贡献的期望是E[i]
E
[
i
]
=
s
[
i
]
∗
s
u
m
i
[
i
]
∗
p
E[i]=s[i]*sumi[i]*p
E[i]=s[i]∗sumi[i]∗p
p要小心,不是C(n,2)i≤j!!!
很简单,换成左闭右开区间,那就是[1,n+1)选两个不相等的i和j,就是C(n+1,2)
真的好算到能一行解决!!!
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll ha = 1e9+7;
const int N = 2e5+10;
ll Sinv[N],inv[N],w[N],s[N];
void init(){
inv[1]=1;for(int i=2;i<=2e5+1;i++) inv[i]=(ha-ha/i)*inv[ha%i]%ha;
Sinv[0]=0;for(int i=1;i<=2e5;i++) Sinv[i]=(Sinv[i-1]+inv[i])%ha;
for(int i=1;i<=2e5;i++)w[i]=((ll)(i+1)*Sinv[i]%ha-i+ha)%ha;
}
int main(){
int T;
scanf("%d",&T);
init();
while(T--){
ll ans=0;
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&s[i]);
for(int i=1;i<=n;i++)ans+=((w[n]-w[i-1]-w[n-i]+ha*5)%ha*s[i])%ha,ans%=ha;
cout<<((ans*2*inv[n])%ha*inv[n+1]%ha)%ha<<endl;
}
}