B - Removing Blocks

Time limit : 2sec / Memory limit : 1024MB

Score : 600 points

Problem Statement

There are \(N\) blocks arranged in a row, numbered 1 to \(N\) from left to right. Each block has a weight, and the weight of Block \(i\) is \(A_i\). Snuke will perform the following operation on these blocks \(N\) times:

Choose one block that is still not removed, and remove it. The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself).

Here, two blocks \(x\) and \(y\) ( \(x≤y\) ) are \(connected\) when, for all \(z\) ( \(x≤z≤y\) ), Block \(z\) is still not removed.

There are \(N\)! possible orders in which Snuke removes the blocks. For all of those \(N\)! orders, find the total cost of the \(N\) operations, and calculate the sum of those \(N\)! total costs. As the answer can be extremely large, compute the sum modulo \(10^9+7\).

Constraints

  • \(1≤N≤10^5\)

  • \(1≤A_i≤10^9\)

  • All values in input are integers.


题意:给你\(n\)个位置,每个位置有权值\(a\),随机删去位置上的数,得到的权值是这个位置相连的联通块内的权值和(联通的定义是位置相邻且数没有删去),每次删完,求所有删数方案的权值和。

发现权值和就是一次删数(指删完整个序列)的权值期望乘上删数方案\(n!\),于是我们需要求删数的权值期望。

考虑每个位置的贡献,当位置\(i\)被删去时,\(\tt{ta}\)的连通性一共有\(n\)种可能。

设删去时的连通块为\((i,j)\),则在这种情况下删\(\tt{ta}\)的可能性是\(P_{i,j}=\frac{1}{|i-j|+1}\)

则\(i\)的整个权值贡献为\(a_i\sum_{j=1}^nP_{i,j}\)

则答案为

\[fac_n\sum_{i=1}^na_i\sum_{j=1}^nP_{i,j}
\]

发现\(p\)可以预处理前缀和,枚举一下\(i\)就可以了


Code:

#include <cstdio>
#define ll long long
const int N=1e5+10;
const ll mod=1e9+7;
int n;
ll a[N],fac=1,inv[N],ans;
ll quickpow(ll d,ll k)
{
ll f=1;
while(k)
{
if(k&1) f=f*d%mod;
d=d*d%mod;
k>>=1;
}
return f;
}
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
int main()
{
scanf("%d",&n);
rep(i,1,n) fac=fac*i%mod,inv[i]=quickpow(i,mod-2),scanf("%lld",a+i);
rep(i,1,n) (inv[i]+=inv[i-1])%=mod;
rep(i,1,n) (ans+=a[i]*(inv[i]+inv[n-i+1]-1))%=mod;
printf("%lld\n",ans*fac%mod);
return 0;
}

2018.10.24

AtCoder Grand Contest 028 B - Removing Blocks 解题报告的更多相关文章

  1. [AtCoder]Grand Contest 028

    A Two Abbreviations 题意:给定两个串,长度为\(N\)的\(A\)和长度为\(M\)的\(B\),一个串\(S\)被称为好的,当且仅当:这个串的长度\(L\)能被\(N,M\)整除 ...

  2. AtCoder Grand Contest 028 A:Two Abbreviations

    题目传送门:https://agc028.contest.atcoder.jp/tasks/agc028_a 题目翻译 给你两个串\(s\)与\(t\),长度分别为\(n,m\).问你存不存在一个串长 ...

  3. AtCoder Grand Contest 019 F-yes or no

    AtCoder Grand Contest 019 F-yes or no 解题思路: 考虑一个贪心策略,假设当前还有 \(x\) 道 \(\text{yes}\) 和 \(y\) 道 \(\text ...

  4. Atcoder Grand Contest 036 D - Negative Cycle

    Atcoder Grand Contest 036 D - Negative Cycle 解题思路 在某些情况下,给一张图加或删一些边要使图合法的题目要考虑到最短路的差分约束系统.这一题看似和最短路没 ...

  5. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  6. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  7. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  8. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

  9. AtCoder Grand Contest 009

    AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...

随机推荐

  1. Mysql导出表结构和数据

    导出数据库 -- 导出dbname表结构 mysqldump -uroot -p123456 -d dbname > dbname.sql -- 导出dbname表数据 mysqldump -u ...

  2. Laravel -- Blade模板

    {{--流程控制--}} @if($name == '1') this is 1 @elseif($name == '2') this.is 2 @else who am i? @endif @for ...

  3. js onsubmit和return false的关系

    一直以来,我都是以为onsubmit=“return false”就不会进行提交,但经过项目之后才知道return false只是避免了之后的跳转,但onsubmit已经是正在进行了,故onsubmi ...

  4. zabbix配置报警媒介-用户-动作-邮件脚本触发mailx邮件报警

    2018-09-16更新,新版本zabbix不需要使用脚本发送邮件,在zabbix web界面直接配置就可以 配置邮件参数,测试发送邮件 确认安装相关服务,centos7默认安装 [root@VM_1 ...

  5. windows环境下,用python绘图库matplotlib绘图时中文乱码问题

    1.下载中文字体(看自己爱好就行)下面这个举例: SimHei - Free Font Download​www.fontpalace.co 2.下载之后,打开即可安装,将字体安装进windows系统 ...

  6. QOS-Qos标记和QOS-Policy策略

    QOS-Qos标记和qos  policy策略 2018年7月7日 20:29 主要标记方法 : IP ToS字段标记 IP Precedence(IP优先级) DSCP 二层 802.1p  CoS ...

  7. spring配置jackson不返回null值

    #json不返回null spring.jackson.default-property-inclusion=non_null

  8. Android开发——Android手机屏幕适配方案总结

    )密度无关像素,单位为dp,是Android特有的单位 Android开发时通常使用dp而不是px单位设置图片大小,因为它可以保证在不同屏幕像素密度的设备上显示相同的效果. /** * dp与px的转 ...

  9. BZ 600题祭

    不知不觉就600题了呢. 明天就要省选了.不要让这个数字定格在这里吧!

  10. 算法-----数组------ 数组中的第K个最大元素

    在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 ...