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.

先排好序, 考虑$a[i]$的贡献, 显然除了$a[i]$以外所有$\ge a[i]$的数要全部排在$a[i]$前面才能有贡献, 即$\binom{n}{n-i+1}(n-i)!(i-1)!=\frac{n!}{n-i+1}$

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, a[N], f[N]; int main() {
scanf("%d", &n);
ll fac = 1, ans = 0;
REP(i,1,n) scanf("%d",a+i), fac=fac*i%P;
sort(a+1,a+1+n);
REP(i,1,n) {
if (a[i]==a[n]) break;
if (a[i]==a[i-1]) ans+=f[i]=f[i-1];
else ans+=f[i]=fac*inv(n-i+1)%P*a[i]%P;
}
printf("%lld\n",ans%P);
}

Max History CodeForces - 938E (组合计数)的更多相关文章

  1. Intercity Travelling CodeForces - 1009E (组合计数)

    大意: 有一段$n$千米的路, 每一次走$1$千米, 每走完一次可以休息一次, 每连续走$x$次, 消耗$a[1]+...+a[x]$的能量. 休息随机, 求消耗能量的期望$\times 2^{n-1 ...

  2. Yet Another Problem On a Subsequence CodeForces - 1000D (组合计数)

    大意:定义一个长为$k>1$且首项为$k-1$的区间为好区间. 定义一个能划分为若干个好区间的序列为好序列. 给定序列$a$, 求有多少个子序列为好序列. 刚开始一直没想出来怎么避免重复计数, ...

  3. Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)

    大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is ...

  4. Different Subsets For All Tuples CodeForces - 660E (组合计数)

    大意: 定义$f(a)$表示序列$a$本质不同子序列个数. 给定$n,m$, 求所有长$n$元素范围$[1,m]$的序列的$f$值之和. 显然长度相同的子序列贡献是相同的. 不考虑空串, 假设长$x$ ...

  5. Singer House CodeForces - 830D (组合计数,dp)

    大意: 一个$k$层完全二叉树, 每个节点向它祖先连边, 就得到一个$k$房子, 求$k$房子的所有简单路径数. $DP$好题. 首先设$dp_{i,j}$表示$i$房子, 分出$j$条简单路径的方案 ...

  6. GukiZ and Binary Operations CodeForces - 551D (组合计数)

    大意: 给定$n,k,l,m$, 求有多少个长度为$n$, 元素全部严格小于$2^l$, 且满足 的序列. 刚开始想着暴力枚举当前or和上一个数二进制中$1$的分布, 但这样状态数是$O(64^3)$ ...

  7. 【BZOJ5491】[HNOI2019]多边形(模拟,组合计数)

    [HNOI2019]多边形(模拟,组合计数) 题面 洛谷 题解 突然特别想骂人,本来我考场现切了的,结果WA了几个点,刚刚拿代码一看有个地方忘记取模了. 首先发现终止态一定是所有点都向\(n\)连边( ...

  8. 【BZOJ5323】[JXOI2018]游戏(组合计数,线性筛)

    [BZOJ5323][JXOI2018]游戏(组合计数,线性筛) 题面 BZOJ 洛谷 题解 显然要考虑的位置只有那些在\([l,r]\)中不存在任意一个约数的数. 假设这样的数有\(x\)个,那么剩 ...

  9. 【BZOJ5305】[HAOI2018]苹果树(组合计数)

    [BZOJ5305][HAOI2018]苹果树(组合计数) 题面 BZOJ 洛谷 题解 考虑对于每条边计算贡献.每条边的贡献是\(size*(n-size)\). 对于某个点\(u\),如果它有一棵大 ...

随机推荐

  1. PHP-配置MySQL

    安装mysql 修改PHP配置文件 修改php安装路径下 php.ini extension=php_mysqli.dll 在代码路径下添加php文件,在里面编辑 <?php phpinfo() ...

  2. 「前端」尚妆 UI 组件库工程实践(weex vue)

    本文来自尚妆前端团队南洋 发表于尚妆github博客,欢迎订阅! 前言 尚妆大前端团队使用 weex 进行三端统一开发有一段时间了,截止本文发表「达人店」APP大部分页面都已经用 weex 进行了重构 ...

  3. ORA-00600: internal error code, arguments: [kqludp2], [0x08D226918], [0], [], [], [], [], [], [], [], [], []

    问题描述: 1)report builder + xml publisher 做的报表,报表提交后报黄色警告,输出文件是XML格式,日志提示如下: +--------- 1) POST-PROCESS ...

  4. CentOS 安装 Mongodb详解 --- 有Linux基础

    安装包:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.1.tgz 安装过程 安装pstree小工具,以及其使用 关闭 ...

  5. nacos 使用笔记

    启动命令: 单机模式启动 start.sh -m standalone

  6. mandatory argument 'crshome' is missing

    1. 错误信息 在oracle  10.2.0.4 to 11.1.0.6 的各个版本中,尽管变量ORA_CRS_HOME设置正确,也会遇到如下错误: # ./diagcollection.pl -c ...

  7. dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib

    dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib Table of Contents 1. 启动时报错 ...

  8. flutter shared_preferences 异步变同步

    前言 在开发原生iOS或Native应用时,一般有判断上一次安装的版本和当前版本是否是同一个版本,如果不是同一个版本,则要显示引导页,这个过程是把版本号持久化存储起来,在每一次启动时进行判断,这个过程 ...

  9. Java异常超详细总结

    1.1,什么是异常:   异常就是Java程序在运行过程中出现的错误. 骚话: 世界上最真情的相依就是你在try我在catch,无论你发什么脾气,我都静静接受,默默处理(这个可以不记) 1.2,异常继 ...

  10. 五十四:WTForms表单验证之自定义表单验证器

    如果想要对表单中的某个字段进行自定义验证,则需要对这个字段进行单独的验证1.定义一个方法,命名规则为:validate_字段名(self, filed)2.在方法中,使用filed.data获取字段的 ...