【51nod】2027 期望问题

%%%zsy

看不懂题解的垃圾选手在zsy大佬的讲解下终于知道了这道题咋做……

先把所有\(a\)从大到小排序

设\(f_{i}\)为前\(i\)个数组成的排列的值,然后显然第\(i + 1\)个插进来的时候,有\(i + 1\)个位置,而且它比谁都要大

以下默认\(f_{1}\)到\(f_{k - 1}\)都是0(因为他们一定是最后弹出的,可以直接算),我们只针对排在k及以后的数讨论贡献

当第\(i\)个数加进来的时候,它有\(i\)种插入位置,前\(k - 1\)个插入位置被算的次数都是i

剩余的次数是一个首项为i,公差为-1的等差数列

然后考虑这个数对于其它的数的贡献

如果第\(i\)个数的次数是\(i\),它对前\(i - 1\)个数的排列的每个数的贡献没有任何影响

否则的话,对于前\(i - 1\)个数的排列中,若某个排列中一个数贡献为\(c\),那么它贡献+1的方案数是\(c - (k - 1)\)

我们把这两部分拆开,\(cx - (k - 1)x\),显然前半部分就是\(f_{i - 1}\),后半部分是一个常数\(k - 1\)乘上所有排在\([k,i - 1]\)的值的和再乘上排列方案数\((i - 1)!\)

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 1000005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N,K;
int a[MAXN],ans;
int fac[MAXN],invfac[MAXN];
int f[MAXN],s[MAXN];
u32 sd;
inline u32 Rand() {
sd ^= sd << 13;
sd ^= sd >> 17;
sd ^= sd << 5;
return sd;
}
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
void update(int &x,int y) {
x = inc(x,y);
}
int C(int n,int m) {
if(n < m) return 0;
return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
int getsum(int s,int l) {
int t = s - l + 1;
return mul(mul(inc(s,t),l),(MOD + 1) / 2);
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
void Solve() {
read(N);read(K);read(sd);
for(int i = 1 ; i <= N ; ++i) {
a[i] = Rand() % N + 1;
}
sort(a + 1,a + N + 1);
s[0] = 0;
for(int i = 1 ; i <= N ; ++i) {
s[i] = a[i];
update(s[i],s[i - 1]);
}
fac[0] = 1;
for(int i = 1 ; i <= N ; ++i) fac[i] = mul(fac[i - 1],i);
invfac[N] = fpow(fac[N],MOD - 2);
for(int i = N - 1 ; i >= 0 ; --i) invfac[i] = mul(invfac[i + 1],i + 1);
for(int i = 1 ; i <= K - 1 ; ++i) {
update(ans,mul(mul(i,a[i]),fac[N]));
}
for(int i = K ; i <= N ; ++i) {
int t = 0;
update(t,mul(K - 1,mul(i,a[i])));
update(t,mul(getsum(i,i - (K - 1)),a[i]));
update(f[i],mul(t,fac[i - 1]));
update(f[i],mul(f[i - 1],i + 1));
int h = inc(s[i - 1],MOD - s[K - 1]);
h = mul(h,K - 1);h = mul(h,fac[i - 1]);
update(f[i],MOD - h);
}
update(ans,f[N]);
ans = mul(ans,invfac[N]);
out(ans);enter;
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}

【51nod】2027 期望问题的更多相关文章

  1. 51Nod 1450 闯关游戏 —— 期望DP

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1450 期望DP: INF 表示这种情况不行,转移时把不行的概率也转 ...

  2. 51NOD 1705 七星剑 [DP 期望的线性性质]

    传送门 题意: 七颗星,第$i$课星用第$j$个宝石有$p[i][j]$的概率成功,失败将为$g[i][j]$颗星: 第$j$个宝石化费$c[j]$ 求最小期望化费 $MD$本来自己思路挺对的看了半天 ...

  3. 51nod 1630(定积分 + 期望)

    51nod1630 每个人进入竞技场后,会等概率随机匹配一个人,匹配到的人与当前胜利和失败场数无关. 胜利达到x场,或失败达到y场后,退出竞技场,根据退出时的胜利场数获得奖励,不能中途放弃. 水平高的 ...

  4. 51nod 1943 联通期望 题解【枚举】【二进制】【概率期望】【DP】

    集合统计类期望题目. 题目描述 在一片大海上有 \(n\) 个岛屿,规划建设 \(m\) 座桥,第i座桥的成本为 \(z_i\),但由于海怪的存在,第 \(i\) 座桥有 \(p_i\) 的概率不能建 ...

  5. 51nod 1450 闯关游戏——期望dp

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1450 想了半天,不知道不能走的状态(即最后不足m个的状态)怎么办. ...

  6. 51Nod 1381 硬币游戏 | 概率(数学期望)

    Input 第一行给出一个整数T,表示有T组数据(1<=T<=10000). 第2行到T+1,每行给出一个整数R.(0< R <= 10,000,000,000) Output ...

  7. 51nod 1098 最小方差 排序+前缀和+期望方差公式

    题目: 题目要我们,在m个数中,选取n个数,求出这n个数的方差,求方差的最小值. 1.我们知道,方差是描述稳定程度的,所以肯定是着n个数越密集,方差越小. 所以我们给这m个数排个序,从连续的n个数中找 ...

  8. 51nod 1836:战忽局的手段(期望)

    题目链接 公式比较好推 精度好难搞啊@_@ 下面记笔记@_@ ****在CodeBlocks中,输出double型变量要使用%f (参见http://bbs.csdn.net/topics/39193 ...

  9. 51nod百度之星2016练习赛

    今天看了看51nod发现有这样一个练习赛,就做了做.因为实力太弱想不出E题,各位神犇勿D. (5.26UPD:E题想粗来了) A 区间交 不难发现若干线段[li,ri]的交就是[max(li),min ...

随机推荐

  1. jsp显示当前系统时间

    第一种方式: <% java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat( "yyy ...

  2. FLUENT多孔介质数值模拟设置【转载】

    转载自:http://zhengjun0228.blog.163.com/blog/static/71377014200971895419613/ 多孔介质条件 多孔介质模型可以应用于很多问题,如通过 ...

  3. js的dom测试及实例代码

    js的dom测试及实例代码 一.总结 一句话总结: 1.需要记得 创建 标签和创建文本节点都是document的活:document.createTextNode("Rockets的姚明&q ...

  4. sed中使用变量及变量中存在特殊字符‘/’处理

    sed中使用变量,普通的处理方式无法解析变量 如当前file文件中存在字符串pedis,现将其替换为redis [root@localhost work]# cat file pedis 如下两种替换 ...

  5. git如何配置邮箱和用户名?

    答: 1. 配置邮箱 git config --global user.email "jello_smith@163.com" 2. 配置用户名 git config --glob ...

  6. LC 898. Bitwise ORs of Subarrays

    We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., ...

  7. PC通过netsh获取wifi密码

    1.查看当前系统所有保存wifi的ssid netsh wlan show profiles 2.根据指定ssid查看wifi密码,密码就是关键内容 netsh wlan show profile n ...

  8. LocalDB数据库修改排序规则,修复汉字变问号

    VS中新增的轻量级数据库LocalDB,有个这个,开发人员就不必再安装庞大的SQL server了,可以方便地测试运行小型项目:既然是轻量级数据库,它抛弃了庞大的身躯,功能上当然也会受到局限,其中之一 ...

  9. Spring分页实现PageImpl<T>类

     Spring框架中PageImpl<T>类的源码如下: /* * Copyright 2008-2013 the original author or authors. * * Lice ...

  10. gateway启动报错:org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found

    将pom.xml中关于spring-boot-start-web模块的jar依赖去掉. 错误分析: 根据上面描述(Description)中信息了解到GatewayAutoConfiguration这 ...