# Jiangxi CCPC Provincial Competition-Rng (Probability+Inverse Element)

Keywords: Verilog

Jiangxi CCPC Provincial Competition-Rng (Probability+Inverse Element)

Title:

  • Given an n, choose a R1 between [1,n], choose an L1 between [1,R1], get an interval [L1,R1], get an interval [L2,R2], and ask the probability of intersection of two intervals to model 1e9+7.

Train of thought 1:

  • Discuss in different situations, the use of conditional probability

    AcCode
#include <bits/stdc++.h>
using namespace std;
#define fre freopen("C:\\Users\\22765\\Desktop\\in.txt","r",stdin);
#define ms(a) memset((a),0,sizeof(a))
#define re(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define sf(x) scanf("%d",&(x))
#define rg register
#define il inline
typedef long long LL;
const int inf=(0x7f7f7f7f);
const int maxn=1e6+5;
const int mod=1e9+7;

LL inv[maxn];
LL sum_inv[maxn];

//Linear Recursive Inverse Element
void init(){
    inv[1]=1;
    re(i,2,maxn){
        inv[i]=((mod-mod/i)*inv[mod%i])%mod;
    }   
}

//Inverse prefix and processing
void getsum() {
    re(i,1,maxn){
        sum_inv[i]=sum_inv[i-1]+inv[i]%mod;
    }   
}

int main(){
    LL n;
    init();getsum();
//  for(int i=1;i<=20;i++)cout<<inv[i]<<" "<<sum_inv[i]<<endl;
    while(cin>>n){
        LL ans=(n+3)*inv[n]%mod*inv[4]%mod;
        re(i,1,n+1){
            ans=ans+i*(sum_inv[n]-sum_inv[i]+mod)%mod*inv[n]%mod*inv[n]%mod;
            ans%=mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}

Train of thought 2:

  • It is more difficult to find intersection directly. It can be done by finding disjoint first and subtracting it by 1. The inverse theorem of positive and difficult cases is much simpler than the previous method.

    The denominator is all possible positions of two endpoints without restrictions.

AcCode:

#include <bits/stdc++.h>
using namespace std;
#define fre freopen("C:\\Users\\22765\\Desktop\\in.txt","r",stdin);
#define ms(a) memset((a),0,sizeof(a))
#define re(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define sf(x) scanf("%d",&(x))
#define rg register
#define il inline
typedef long long LL;
const int inf=(0x7f7f7f7f);
const int maxn=3000;
LL q=1000000007;
LL qpow(LL a,LL b){
    LL ans=1;
    a%=q;
    while(b){
        if(b&1)ans=ans*a%q;
        b>>=1;
        a=a*a%q;
    } 
    return ans;
}
LL inv(LL a){
    if(a==1)return 1;
    return qpow(a,q-2);
}

int main(){
    LL n;
    while(cin>>n){
        cout<<(n+1)*inv(2*n)%q<<endl; 
    }
    return 0;
}

Posted by kashmirekat on Mon, 14 Oct 2019 12:35:53 -0700