【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 2Sample 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; }?
總結
以上是生活随笔為你收集整理的【HDU - 6574】Rng(概率,古典概型)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【POJ - 2942】Knights
- 下一篇: ssonsvr.exe - ssonsv