Rng(法則+逆元+確率)
1501 ワード
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
Sample Output
この問題の意味は1からnの区間の中で2つのシーケンスを探し出して、2つのシーケンスが交差しない確率を求めて、私たちは彼が交差する確率を求めることができます.
まず1からnまでの区間で最初のシーケンスの右端を選択し、その選択確率は1/n、iとマークし、次に左端を選択し、選択確率は1/iとする.
最後の確率式はn+1/2*n(私は太く求めません..)その逆元用フェルマ小定理はa^(p-1)=1(mod p)すなわちa^(p-2)=a^-1(mod p)である
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
2
Sample Output
1
750000006
この問題の意味は1からnの区間の中で2つのシーケンスを探し出して、2つのシーケンスが交差しない確率を求めて、私たちは彼が交差する確率を求めることができます.
まず1からnまでの区間で最初のシーケンスの右端を選択し、その選択確率は1/n、iとマークし、次に左端を選択し、選択確率は1/iとする.
最後の確率式はn+1/2*n(私は太く求めません..)その逆元用フェルマ小定理はa^(p-1)=1(mod p)すなわちa^(p-2)=a^-1(mod p)である
#include
using namespace std;
long long mod=1000000007;
long long qpow(long long a,long long b){
if(b<0) return 0;
long long res=1;
a%=mod;
while(b){
if(b&1){
res*=a;
res%=mod;
}
b>>=1;
a*=a;
a%=mod;
}
return res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
long long n;
while(cin>>n){
long long ans=((n+1)*qpow(2*n,mod-2))%mod;
cout<