多校第六场——1001 Road To The 3rd Building

Problem Description
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

题意:给你n个数,现在任意选择一个区间求其平均值,你需要求出这个平均值的期望。

思路:在这里插入图片描述
ps:快速幂板子记得把a模一下,不然会wa

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
int num[N];
ll quick_pow(ll a, ll b)
{
	ll ans = 1;
	a %= mod;
	while (b)
	{
		if (b & 1)
			ans = ans * a%mod;
		a = a * a%mod;
		b >>= 1;
	}
	return ans;
}
ll sum_1[N], sum_2[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int T;
	cin >> T;
	while (T--)
	{
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			cin >> num[i];
			sum_1[i] = (sum_1[i - 1] + num[i]) % mod;
		}
		if (n & 1)
		{
			sum_2[1] = sum_1[n];
			for (int i = 2; i <= n / 2; i++)
			{
				sum_2[i] = (sum_2[i - 1] + sum_1[n - i + 1] - sum_1[i - 1] + mod) % mod;
			}
			sum_2[n / 2 + 1] = (sum_2[n / 2] + num[n / 2 + 1]) % mod;
			sum_2[n] = sum_1[n];
			for (int i = n - 1; i >= n / 2 + 2; i--)
			{
				sum_2[i] = (sum_2[i + 1] + sum_1[i] - sum_1[n - i] + mod) % mod;
			}
		}
		else
		{
			sum_2[1] = sum_1[n];
			for (int i = 2; i <= n / 2; i++)
			{
				sum_2[i] = (sum_2[i - 1] + sum_1[n - i + 1] - sum_1[i - 1] + mod) % mod;
			}
			sum_2[n] = sum_1[n];
			for (int i = n - 1; i >= n / 2 + 1; i--)
			{
				sum_2[i] = (sum_2[i + 1] + sum_1[i] - sum_1[n - i] + mod) % mod;
			}
		}
		ll ans = 0;
		for (int i = 1; i <= n; i++)
		{
			ans = (ans + sum_2[i] * quick_pow(i, mod - 2) % mod) % mod;
		}
		ans = ((ans * 2 % mod) * quick_pow(1LL * n*(n + 1), mod - 2) % mod);
		cout << ans << endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值