【题目】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 225 Accepted Submission(s): 142
Problem Description
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* (MOD 1, 000, 000, 007), while 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
Source
2019CCPC-江西省赛(重现赛)- 感谢南昌大学
【题意】给你一个n,第一次可随机从1到n选一个点r,接着从1到r随机选择一个点l,那么就选出了一个区间,l,r,以同样的方法选第二个区间,问两个区间相交的概率。
【做法】
反过来思考比较简单,我求不相交的概率,然后1减去它就行了
自己参考着手推一遍就会了
分析:设第一个区间的右端点为a,第二个为b,那么可以得到两个区间不相交的概率为:
(1)b>a时,p=(b-a)/b
(2)b<a时,p=(a-b)/a
那么当a固定时,枚举b得到
再枚举a得到
将a和b替换成ij,再随便化简一下
再用1减一下就是答案了,处理一个后缀和就可以求解了。
这里它中间化简省略了很多,因为公式比较难打,建议自己手写一遍。。
【代码】
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
const ll mod=1e9+7;
ll n;
ll b[N];
ll cal(ll n)
{
return n*(n+1)/2;
}
ll powmod(ll a,ll b)
{
ll res=1;
for(;b;b>>=1)
{
if(b&1) res=res*a%mod;
a=a*a%mod;
}
return res;
}
ll inv(ll x)
{
return powmod(x,mod-2);
}
int main()
{
cin>>n;
ll ans=0;
for(ll i=n;i>=1;i--)
{
b[i]=(b[i+1]+inv(i))%mod;
}
for(ll i=1;i<=n;++i)
{
ans=(ans+(i+1)*inv(2)%mod+i*b[i+1]%mod)%mod;
}
printf("%lld\n",ans*inv(n*n%mod)%mod);
}