1925D - Good Trip - CodeForces Solution


combinatorics math probabilities

Please click on ads to support us..

Python Code:

import sys
sys.setrecursionlimit(10000000)

m = 1000000007
 
def gcd(a, b):
 
    if (a == 0):
        return b
    return gcd(b % a, a)
 
def modexp(x, n):
 
    if (n == 0) :
        return 1
     
    elif (n % 2 == 0) :
        return modexp((x * x) % m, n // 2)
     
    else :
        return (x * modexp((x * x) % m, 
                           (n - 1) / 2) % m)
 
 
def getFractionModulo(a, b):
 
    c = gcd(a, b)
 
    a = a // c
    b = b // c
 
        d = modexp(b, m - 2)
 
        ans = ((a % m) * (d % m)) % m
 
    return ans

t = int(input())
for _ in range(t):
    n, _m, k = map(int, input().split())
    sm = 0
    for __ in range(_m):
        _a, _b, x = map(int, input().split())
        sm += x
    if not _m: 
        print(0)
        continue

    p = 2 * sm * k * (n**2 - n) + 2 * _m * (k**2 - k)
    q = n ** 4 + n ** 2 - 2 * (n ** 3)
    print(getFractionModulo(p, q))

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <random>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef tree<pair<int, int>, null_type, greater<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define AboTaha_on_da_code ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define X first
#define Y second

const int dx[8]={0, 0, 1, -1, 1, -1, -1, 1};
const int dy[8]={1, -1, 0, 0, 1, -1, 1, -1};
const int mod = 1e9+7; // 1e9+7, 998244353
const double EPS = 1e-8;
// BEFORE coding are you sure you understood the statement correctly?
// PLEASE do not forget to read the sample explanation carefully.
// WATCH out for overflows & RTs in general.
// TEST your idea or code on the corner cases.
// ANALYZE each idea you have thoroughly.

int mpow(int a, int b, int m)
{
    int res = 1;
    while(b) {
        if (b&1) res = 1LL*res*a%m;
        a = 1LL*a*a%m, b/=2;
    }
    return res;
}

const int N = 2e5+10;
int fact[N], invfact[N];
void pre()
{
    fact[0] = invfact[0] = 1;
    for (int i = 1; i < N; i++) {
        fact[i] = 1LL*fact[i-1]*i%mod;
        invfact[i] = mpow(fact[i], mod-2, mod);
    }
}

int nCr(int n, int r)
{
    if (r > n) return 0;
    return 1LL*fact[n]*invfact[r]%mod*invfact[n-r]%mod;
}

inline int inv(int x) {
    return mpow(x, mod-2, mod);
}

void burn(int tc) {
    int n, m, k; cin >> n >> m >> k;
    ll sm = 0;
    for (int i = 0; i < m; i++) {
        int x; cin >> x >> x >> x;
        sm+=x;
        if (sm >= mod) sm-=mod;
    }
    int ans = 0;
    for (int x = 0; x < k; x++) {
        int cur = 1LL*(x+1)*(2*sm+x)/2%mod*inv(m)%mod;
        cur = 1LL*cur*mpow(1LL*m*inv(nCr(n, 2))%mod, x+1, mod)%mod;
        cur = 1LL*cur*mpow((mod+1-1LL*m*inv(nCr(n, 2))%mod)%mod, k-x-1, mod)%mod;
        cur = 1LL*cur*nCr(k, x+1)%mod;
        ans+=cur;
        if (ans >= mod) ans-=mod;
    }
    cout << ans;
}

int main() {

    AboTaha_on_da_code

//    freopen("khoshaf.in", "r", stdin);
//    freopen("Aout.txt", "w", stdout);

    int _t = 1; cin >> _t;

    pre();

    for (int i = 1; i <= _t; i++) {
//        cout << "Case " << i << ": ";
        burn(i);
        cout << '\n';
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers