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
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时,p=(a-b)/a
那么当a固定时,枚举b得到
再枚举a得到
将a和b替换成ij,再随便化简一下
最后再用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;
}