原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1309.html

题目传送门 - 51Nod1309

题意

长度为N的整数数组A,有Q个查询,每个查询包含一个数M,对A的所有不同排列,执行find函数(需用到查询中的M),你来计算find函数的返回值的和。由于结果很大,输出Mod 1000000007的结果。
 
void find(int permutation_A[], int M){
x = Length(permutation_A);
sum = 0;
for(i = 0; i < x; i++) {
if (permutation_A[i] <= M)
sum = sum + permutation_A[i];
else
break;
}
return sum;
}
 
 
 

题解

  我们首先考虑求答案的期望。

  由于期望具有线性性,所以我们可以对于每一个数字对答案的贡献分开考虑。

  显然,如果在一个排列中,如果当前数字产生贡献,当且仅当该数字不大于 m ,且所有大于 m 的数字都出现在它后面。那么,设大于等于 m 的数字有 k 个,假设当前数字不大于 m ,那么当前数字产生贡献的概率是 $\frac{1}{k+1}$ 。那么它对总期望的贡献就是 它的值 × 概率。我们只需要把所有不大于 m 的数对期望的贡献求和就可以得到总期望了。又由于,所有不大于 m 的数产生贡献的概率相同,所以我们可以前缀和处理一下,快速求得期望。

  所以最终答案就是期望 × 排列总数。

代码

 #include <bits/stdc++.h>
using namespace std;
const int N=50005,mod=1e9+7;
int n,q,a[N],Ha[N],p[N],hs;
int Fac[N];
int Pow(int x,int y){
int ans=1;
for (;y;y>>=1,x=1LL*x*x%mod)
if (y&1)
ans=1LL*ans*x%mod;
return ans;
}
int main(){
scanf("%d%d",&n,&q);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
for (int i=1;i<=n;i++)
Ha[i]=a[i];
hs=1;
for (int i=2;i<=n;i++)
if (Ha[i]!=Ha[i-1])
p[hs]=i-1,Ha[++hs]=Ha[i];
p[hs]=n;
for (int i=1;i<=n;i++)
a[i]=(a[i-1]+a[i])%mod;
Fac[0]=1;
for (int i=1;i<=n;i++)
Fac[i]=1LL*Fac[i-1]*i%mod;
int ans=Fac[n];
for (int i=1;i<=hs;i++)
ans=1LL*ans*Pow(Fac[p[i]-p[i-1]],mod-2)%mod;
while (q--){
int v;
scanf("%d",&v);
int x=upper_bound(Ha+1,Ha+hs+1,v)-Ha-1;
int now=1LL*ans*Pow(n-p[x]+1,mod-2)%mod*a[p[x]]%mod;
printf("%d\n",now);
}
return 0;
}

  

51Nod1309 Value of all Permutations 期望的更多相关文章

  1. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  4. 【BZOJ-3143】游走 高斯消元 + 概率期望

    3143: [Hnoi2013]游走 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2264  Solved: 987[Submit][Status] ...

  5. bzoj1415[NOI2005]聪聪和可可-期望的线性性

    这道题之前我写过一个巨逗比的写法(传送门:http://www.cnblogs.com/liu-runda/p/6220381.html) 当时的原因是这道题可以抽象出和"绿豆蛙的归宿&qu ...

  6. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  7. 【BZOJ3036】绿豆蛙的归宿 概率与期望

    最水的概率期望,推荐算法合集之<浅析竞赛中一类数学期望问题的解决方法> #include <iostream> #include <cstdio> using na ...

  8. POJ2369 Permutations(置换的周期)

    链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  9. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

随机推荐

  1. 随机函数rand()和srand()

    C++中随机函数rand()和srand()的用法 一.rand() 函数名:   rand     功   能:   随机数发生器   用   法:   int rand(void); 所在头文件: ...

  2. Django 中间件 请求前

    中间件: class TestMiddleware(object): """中间件类""" def __init__(self): &quo ...

  3. atop工具检测linux硬件异常

    引言 Linux以其稳定性,越来越多地被用作服务器的操作系统(当然,有人会较真地说一句:Linux只是操作系统内核:).但使用了Linux作为底层的操作系统,是否我们就能保证我们的服务做到7*24地稳 ...

  4. web.xml详细选项配置

    Web.xml常用元素 <web-app> <display-name></display-name>定义了WEB应用的名字 <description> ...

  5. IOS 颜色的宏定义

    #define RGB(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]   self.vie ...

  6. Confluence 6 用户目录图例 - 连接 Jira 和 Jira 连接 LDAP

    上面的图: Confluence 连接到 JIRA 用户管理,JIRA 使用 LDAP 用户目录. https://www.cwiki.us/display/CONFLUENCEWIKI/Diagra ...

  7. python面试1-30题

    1.一行代码实现1--100之和 利用sum()函数求和 2.如何在一个函数内部修改全局变量 利用global 修改全局变量 3.列出5个python标准库 os:提供了不少与操作系统相关联的函数 s ...

  8. 断路器Feign

    Feign是自带断路器,需要在配置文件中开启断路器 改造消费者项目(FeignDemo) 1.在application.yml配置文件中开启断路器 eureka: client: service-ur ...

  9. java多线程快速入门(十八)

    Lock锁是JDK1.5之后推出的并发包里面的关键字(注意捕获异常,释放锁) Lock与synchronized的区别 Lock锁可以人为的释放锁(相当于汽车中的手动挡) synchronized当线 ...

  10. 《剑指offer》 数值的整数次方

    本题来自<剑指offer> 数值的整数次方 题目: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 思路: 代码从三个方面处 ...