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. 百度知道芝麻将,申请资格&权限介绍&奖惩制度(简剖)

    芝麻将,即百度知道管理员.不及掌握,了解下也是好的. 知道圈子,把注册用户,成为芝麻.一般用户,即小芝麻.芝麻将,就是咱们说的知道管理员. 申请. 芝麻将,申请条件不是很难,难的是后续维护,申请前考虑 ...

  2. 20.2 解析与序列化【JavaScript高级程序设计第三版】

    JSON 之所以流行,拥有与JavaScript 类似的语法并不是全部原因.更重要的一个原因是,可以把JSON 数据结构解析为有用的JavaScript 对象.与XML 数据结构要解析成DOM 文档而 ...

  3. Linux 新建定时任务

    Linux 新建定时任务: 1.查看指定用户列表: crontab -u apache -l 2.切换至对应用户,这里是apache su apache 3.新增定时任务: crontab -e 写入 ...

  4. python+matplotlib 绘制等高线

    python+matplotlib 绘制等高线 步骤有七: 有一个m*n维的矩阵(data),其元素的值代表高度 构造两个向量:x(1*n)和y(1*m).这两个向量用来构造网格坐标矩阵(网格坐标矩阵 ...

  5. 自定义控件,继承自 ListView

    public class MyListView extends ListView { /** * 如果在xml中创建并设置了style,就会调用三个参数的. * * @param context * ...

  6. 《linux设备驱动开发详解》笔记——15 linux i2c驱动

    结合实际代码和书中描述,可能跟书上有一定出入.本文后续芯片相关代码参考ZYNQ. 15.1 总体结构 如下图,i2c驱动分为如下几个重要模块 核心层core,完成i2c总线.设备.驱动模型,对用户提供 ...

  7. WPF中InkCanvas(墨水面板)用法

    原文:WPF中InkCanvas(墨水面板)用法   WPF中InkCanvas(墨水面板)用法                                                    ...

  8. 多表头的DataGridView

           上次在程序中要用到多表头的DataGridView,在网上搜索了一个,感觉还不错,现在简单的介绍一下它的用法.首先得把这个dll拷贝到相应的目录下,dll名称是myMultiColHea ...

  9. ExtJs工具篇(3)——Aptana Studio3乱码的问题

            在Aptana Studio里面使用,发现输入的中文是乱码,在浏览器中浏览也是乱码,想着肯定是编码的问题,但是一直没有找到在那个地方设置.以为汉化后就可以了,没想到汉化后竟然还是乱码, ...

  10. 如何理解Java中参数传递只能传值?

    以前学习C#的时候,是完全在工作岗位上学习,一些底层较为深入的道理都不是很清楚.如今学习了Java,对于Java参数传递只能传值,不能传引用(指针)感到很困惑,在C#中不是常常说把某个引用传递到函数中 ...