发现有一种奇怪的方法不能快速预处理?

复习一下常见的凑组合数的套路

You are given an array a of length n. We define fa the following way:

  • Initially fa = 0, M = 1;
  • for every 2 ≤ i ≤ n if aM < ai then we set fa = fa + aM and then set M = i.

Calculate the sum of fa over all n! permutations of the array a modulo 109 + 7.

Note: two elements are considered different if their indices differ, so for every array a there are exactly n! permutations.

Input

The first line contains integer n (1 ≤ n ≤  1 000 000) — the size of array a.

Second line contains n integers a1, a2, ..., an (1 ≤  ai ≤  109).

Output

Print the only integer, the sum of fa over all n! permutations of the array a modulo 109 + 7.


题目大意

题目分析

主要是记一下一种凑组合数的常见套路,网上其他人都是一种另外的考虑方式。

对于数$a_q$枚举它每一个出现位置的贡献,记严格小于它的数有$m$个,则有$a_q\sum\limits_{p=1}^n{m \choose {p-1}}(p-1)! (n-p)!$即$a_q m!\sum\limits_{p=1}^n{{(n-p)!}\over {(m-p+1)!}}$。于是就会发现右边这个东西分子分母都有自变量,看上去很难处理,但形式上又是有些相似的感觉。

式子可以接着这么化:$a_qm!(n-m+1)!\sum\limits_{p=1}^n{{n-p}\choose{n-m-1}}$,也就是把右式做成一个组合数。

注意到新的右式是经典问题组合数的列前缀和,于是化成:$a_qm!(n-m+1)!{{n}\choose{n-m}}$

最后化简得到:$a_qn!\over{n-m}$

 #include<bits/stdc++.h>
#define MO 1000000007
const int maxn = ; int n,ans,cnt,a[maxn],fac[maxn],inv[maxn]; int read()
{
char ch = getchar();
int num = , fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = -;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
return num*fl;
}
int main()
{
n = read();
for (int i=; i<=n; i++) a[i] = read();
std::sort(a+, a+n+);
fac[] = fac[] = inv[] = inv[] = ;
for (int i=; i<=n+; i++)
inv[i] = MO-1ll*(MO/i)*inv[MO%i]%MO,
fac[i] = 1ll*fac[i-]*i%MO;
for (int i=,j; i<=n; i=j+)
{
for (j=i; a[j+]==a[i]; j++);
if (j==n) break;
cnt = inv[n-i+];
ans = (1ll*ans+1ll*a[i]*(j-i+)%MO*cnt%MO)%MO;
}
printf("%d\n",1ll*ans*fac[n]%MO);
return ;
}

END

【计数】cf938E. Max History的更多相关文章

  1. [CF938E]Max History题解

    题面 >CF传送门< >洛谷传送门< 解法 显而易见,对于一个数\(a_i\),若果它出现在\(f\)序列中,必定\(a_i\)之前的元素要小于\(a_i\),我们设\(cnt ...

  2. Max History CodeForces - 938E (组合计数)

    You are given an array a of length n. We define fa the following way: Initially fa = 0, M = 1; for e ...

  3. 2018.12.12 codeforces 938E. Max History(组合数学)

    传送门 唉最开始居然把题给看错了. 其实是组合数学傻逼题呢. 题意简述:给出一个数列,定义一个与数列有关的fff函数,fff函数定义如下: 首先f=0,M=1f=0,M=1f=0,M=1,一直重复如下 ...

  4. Codeforces 938E Max History:排列 + 逆元【考虑单个元素的贡献】

    题目链接:http://codeforces.com/problemset/problem/938/E 题意: 定义f(a): 初始时f(a) = 0, M = 1. 枚举i = 2 to n,如果a ...

  5. CodeForces 938E Max History 题解

    参考自:https://blog.csdn.net/dreaming__ldx/article/details/84976834 https://blog.csdn.net/acterminate/a ...

  6. 如何解决Redis中的key过期问题

    最近我们在Redis集群中发现了一个有趣的问题.在花费大量时间进行调试和测试后,通过更改key过期,我们可以将某些集群中的Redis内存使用量减少25%. Twitter内部运行着多个缓存服务.其中一 ...

  7. [SQL入门级] 接上篇,继续查询

    距离上一篇时间隔得蛮久了,这篇继续查询,简单总结一下聚合函数.分组的知识. 一.聚合函数(组函数/多行函数) 何谓多行函数,顾名思义就是函数作用于多行数据得出一个输出结果,什么意思呢?看图: 那么常用 ...

  8. SQL基本语句以及示例

    基本语句: /*dorp colunm*/ 语法:ALTER TABLE 表名   DROP COLUMN 要删除的字段 验证财务转换的正确性,查询以下两个表是否有数据 /*表连接inner jion ...

  9. python——使用readline库实现tab自动补全

    Input History readline tracks the input history automatically. There are two different sets of funct ...

随机推荐

  1. 操作iframe的一些方法

    //父页面操作iframe里的内容 oInput.onclick=function(){ var oBox = oIframe.contentWindow.document.getElementByI ...

  2. .netcore中无法使用System.Drawing --解决方案

    问题重现: 无法正常使用  解决方法: 安装System.Drawing.Common的NuGet就能正常使用了 操作之后: 这个是.netcoe中的解决办法,.net framework解决方案中添 ...

  3. 在myecplise中更新代码发布后没效果

    requestmapping指定路径不能访问: 重新发布,重新访问,清除缓存,重新访问,重启浏览器,清除缓存,重新访问,清除Tomcat相关目录,重新发布,重新访问……折腾了很久,可还是没有解决问题. ...

  4. linux系统redis配置环境变量

    1.测试:在任何位置登录redis redis-cli 指定服务器ip(不指定时,默认本机) redis-cli -h 127.0.0.1 指定端口(不指定时,默认6379) redis-cli -h ...

  5. Java字节流与字符流的区别详解

    字节流与字符流 先来看一下流的概念: 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入 ...

  6. 父类和子类以及super关键字

    super和this关键字的特点类似:super代表的是父类对象的引用. 当子父类的成员出现同名时,可以通过super来进行区分. 子类的构造方法中,通过super关键字调用父类的构造方法. publ ...

  7. Entity Framework --Entity SQL注意事项

    Entity SQL 是 ADO.NET 实体框架 提供的 SQL 类语言,用于支持 实体数据模型 (EDM).Entity SQL 可用于对象查询和使用 EntityClient 提供程序执行的查询 ...

  8. DELL R730安装ESXI虚拟化

    dell安装esxi需要dell官方提供的镜像文件地址:http://www.dell.com/support/article/us/en/04/SLN290857/dell%E5%AE%9A%E5% ...

  9. sk-learning(2)

    sk-learning 学习(2) sklearing 训练评估 针对kdd99数据集使用逻辑回归分类训练 然后进行评估 发觉分数有点高的离谱 取出10%数据494021条,并从中选择四分之一作为测试 ...

  10. [VC]C++ operator 两种用法

    C++中的operator,有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).下面分别进行介绍:   1.operato ...