Rng HDU - 6574

Avin is studying how to synthesize data. Given an integer n, he constructs an interval using the following method: he first generates a integer r between 1 and n (both inclusive) uniform-randomly, and then generates another integer l between 1 and r (both inclusive) uniform-randomly. The interval [l, r] is then constructed. Avin has constructed two intervals using the method above. He asks you what the probability that two intervals intersect is. You should print p* q(−1)(MOD 1, 000, 000, 007), while pq denoting the probability.
Input
Just one line contains the number n (1 ≤ n ≤ 1, 000, 000).
Output
Print the answer.
Sample Input
1
2
Sample Output
1
750000006

题意;

给你一个数n,然后从n里面随机选出来一个数 r,然后从1到r中再随机选出一个数,组成一个区间[ l , r ]; 进行两次这样的实验,产生两个区间,求这两个区间有重合部分的概率;

思路;

思路:用古典概型的方法计算俩个区间不想交的概率,再用 1 减去即可。

由于 枚举俩种括号的所有可能出现的情况是不易的 并且我们知道 俩个区间不相交只和左边区间的右端点和右边区间的左端点有关。已知 选区间 满足伯努利实验,是个独立实验。所以我们可以直接设 左边区间 的右端点为 R左、右边区间的 左端点为L右,那么只要 R左 < L右 即可。

所以不相交的概率可以这么求

在这里插入图片描述
在这里插入图片描述

然后用逆元公式处理下就行了

#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
ll quick_pow(ll a,ll p)
{
	ll ans=1;
	a=a%mod;
	while(p)
	{
		if(p&1) ans=ans*a%mod;
		p>>=1;
		a=a*a%mod;
	}
	return ans;
}
int main()
{
	ll n;
	scanf("%lld",&n);
	if(n==1) printf("1\n");
	else printf("%lld\n",(n+1)*quick_pow(2*n,mod-2)%mod);
	
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值