原文链接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. Ex 2_25 n位十进制整数转换为二进制形式..._第四次作业

    (a)   当n=1时,(10)d=(1010)b 当n=2时,(100)d=(10)d x (10)d=(1010)b x (1010)b 当n=4时,(10000)d=(100)d x (100) ...

  2. [转]自定义alert弹框,title不显示域名

    //(仅去掉网址) (function(){ window.alert = function(name){ var iframe = document.createElement("IFRA ...

  3. linux学习之uniq

    uniq最经常用的是统计次数,通常先排序,然后uniq  -c cat a.txt |sort -nr |uniq -c

  4. 瑞联科技:Pwp3框架 调用存储过程返还数据集合 到前端界面展示

    一:代码结构: 1:Js 代码结构 2:Java 代码结构 二:前端界面展示效果 为了数据安全性:界面数据做了处理 三:全端代码展示 1:main.vop <html> <head& ...

  5. 银联支付java版

    注:本文来源于:<  银联支付java版    > 银联支付java版 2016年09月18日 15:55:20 阅读数:2431 首先去银联官网注册测试支付账户   下载对应的demo[ ...

  6. 初识dubbo

    1. 为什么需要 Dubbo(摘自http://dubbo.apache.org/zh-cn/docs/user/quick-start.html) 随着互联网的发展,网站应用的规模不断扩大,常规的垂 ...

  7. 基于BootStrap的Collapse折叠(包含回显展开折叠的对应状态)

    情况描述:为了改善页面上的input框太多,采用∧∨折叠展开,这个小东西来控制,第一次做,记录一下ヾ(◍°∇°◍)ノ゙下边是Code 代码: //html代码 <div id="col ...

  8. 统计nginx日志里访问次数最多的前十个IP

    awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr -k1 | head -n 10

  9. Yslow web性能测试插件

    YSlow可以对网站的页面进行分析,并告诉你为了提高网站性能,如何基于某些规则而进行优化. YSlow可以分析任何网站,并为每一个规则产生一个整体报告,如果页面可以进行优化,则YSlow会列出具体的修 ...

  10. 论文阅读笔记十五:Pyramid Scene Parsing Network(CVPR2016)

    论文源址:https://arxiv.org/pdf/1612.01105.pdf tensorflow代码:https://github.com/hellochick/PSPNet-tensorfl ...