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;
}