【HDU - 6574】Rng(概率,古典概型)

题干:

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)q(−1)(MOD 1, 000, 000, 007), while pqpq 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的区间,定义产生一个区间[l,r]的方式:第一次在[1,n]内等概率选一个点当做r,然后在[1,r]内等概率选一个点当l。

如此产生两个区间,问这两个区间相交(应该是指的有交点)的概率是多大?

解题报告:

对右端点分成三种情况,因为是古典改性直接用乘法原理计算对应事件发生的概率;累加即可。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
const ll mod = 1e9 + 7;
int n;
ll qpow(ll a,ll b){
	ll res=1;
	while(b) {
		if(b&1) res=res*a%mod;
		a=a*a%mod;b>>=1;
	}
	return res;
}
int main()
{
	while(~scanf("%d",&n)){
		printf("%lld\n",(n+1)*qpow(2*n,mod-2)%mod);
	}
	return 0;
}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值