hdu 5419(数学期望)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5419

题解:

首先分母是C(m,3),考虑如何求出分子

考虑数学期望的独立性,我们首先可以用线性的时间处理每个点被多少区间覆盖,如果第i个点被si个区间覆盖,则该点对结果的贡献度为wi*C(si,3),注意这里si必须要大于等于3才能够对结果有贡献。


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

typedef long long LL;
const int maxn = 50005;
int n,m,w[maxn];
int bound[maxn];

LL gcd(LL a,LL b)
{
	if(b) return gcd(b,a % b);
    return a;
}

LL C(LL a)
{
	return a * (a - 1) * (a - 2) / 6;
}

int main()
{
	int t,l,r;
	scanf("%d",&t);
	while(t--)
	{
		memset(bound,0,sizeof(bound));
		scanf("%d%d",&n,&m);
		for(int i = 1; i <= n; i++)
			scanf("%d",&w[i]);
		for(int i = 1; i <= m; i++)
		{
			scanf("%d%d",&l,&r);
			bound[l]++; bound[r+1]--;
		}
		int cnt = 0;
		LL ans1 = 0;
		for(int i = 1; i <= n; i++)
		{
			cnt += bound[i];
			if(cnt >= 3)
				ans1 += w[i] * C(cnt);
		}
		LL ans2 = C(m);
		LL d = gcd(ans1,ans2);
		if(d > 0)
		{
			ans1 /= d; ans2 /= d;
			if(ans2 == 1) printf("%lld\n",ans1);
			else printf("%lld/%lld\n",ans1,ans2);
		}
		else printf("0\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值