zoukankan      html  css  js  c++  java
  • Rng(求逆元)

    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.
     

    求逆元的几种方法:https://blog.csdn.net/xiaoming_p/article/details/79644386

    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
     
    规律:(n+1)*n/2/(n*n)
    代码:
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<vector>
    #include<map>
    #include<cmath>
    const int maxn=1e5+5;
    const long long  mod=1e9+7; 
    typedef long long ll;
    using namespace std;
    
    ll ksm(ll x,ll y)
    {
      ll ans=1;
      while(y)
      {
          if(y&1)
          ans=ans*x%mod;
          y>>=1;
          x=x*x%mod;
      }
      return ans;
    } 
    int main()
    {
       ll n;
       while(cin>>n)
       {
           ll p=(n+1)*n/2;
           ll q=n*n;
           printf("%lld
    ",(p*ksm(q,mod-2))%mod);
       } 
       return 0;
    }
     
     
  • 相关阅读:
    Chp18: Hard
    内存泄漏 和 内存溢出
    Chp4: Trees and Graphs
    trie树--详解
    Tries
    Comparable & Comparator
    memory leak
    8个月从CS菜鸟到拿到Google Offer的经历+内推
    Word Ladder II
    Java垃圾回收机制
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11221390.html
Copyright © 2011-2022 走看看