hduoj - 6574 Rng【概率】

Rng

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 16    Accepted Submission(s): 10


 

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* 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

 

 

Source

2019CCPC-江西省赛(重现赛)- 感谢南昌大学

 

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6577 6576 6575 6574 6573 

 

分析:设第一个区间的右端点为a,第二个为b,那么可以得到两个区间不相交的概率为:

(1)b>a时,p=(b-a)/b

(2)b

那么当a固定时,枚举b得到\frac{\sum _{b=1}^a(1-\frac{b}{a})+\sum _{b=a+1}^n(1-\frac{a}{b})}{n}

再枚举a得到\frac{\sum _{a=1}^n(1-\frac{\sum _{b=1}^a(\frac{b}{a})+\sum _{b=a+1}^n(\frac{a}{b})}{n})}{n}

将a和b替换成ij,再随便化简一下

1-\frac{\sum _{i=1}^n( \frac{1}{i}*\sum _{j=1}^ij + \sum _{j=i+1}^n(\frac{i}{j}))}{n^2}

1-\frac{\sum _{i=1}^n \frac{1+i}{2}+\sum _{i=1}^ni*\sum _{j=i+1}^n\frac{1}{j}}{n^2}

1-\frac{\sum _{i=1}^n \frac{1+i}{2}+\sum _{j=1}^n\frac{1}{j}\sum _{i=1}^{j-1}i}{n^2}

1-\frac{\sum _{i=1}^n \frac{1+i}{2}+\sum _{i=1}^n \frac{i-1}{2}}{n^2}

1-\frac{n+1}{2*n}

最后再用1减一下就可以了。

#include "bits/stdc++.h"

using namespace std;
const int mod = 1e9 + 7;

long long qk(long long a, long long n) {
    long long res = 1;
    while (n) {
        if (n & 1)res = res * a % mod;
        n >>= 1;
        a = a * a % mod;
    }
    return res;
}

int main() {
    long long n;
    cin >> n;
    cout << (n + 1) * qk(2 * n, mod - 2) % mod << endl;
}
#include "bits/stdc++.h"

using namespace std;
const int mod = 1e9 + 7;

long long qk(long long a, long long n) {
    long long res = 1;
    while (n) {
        if (n & 1)res = res * a % mod;
        n >>= 1;
        a = a * a % mod;
    }
    return res;
}

long long las[1004];

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        las[i] = qk(i, mod - 2);
    }
    for (int i = n - 1; i >= 1; --i) {
        las[i] = (las[i] + las[i + 1]) % mod;
    }
    long long ans = 0;
    las[n + 1] = 0;
    for (int i = 1; i <= n; ++i) {
        ans = (ans + (1 + i) * qk(2, mod - 2) % mod) % mod;
        ans = (ans + i * (las[i + 1]) % mod) % mod;
    }
    cout << ans * qk(n, mod - 2) % mod * qk(n, mod - 2) % mod << endl;
}

 

你可能感兴趣的:(hduoj - 6574 Rng【概率】)