869C - The Intriguing Obsession - CodeForces Solution


combinatorics dp math *1800

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define pii pair<ll int,ll int>
#define umap unordered_map
#define popcount(x) __builtin_popcountll(x)
#define all(v) v.begin() , v.end()
#define PI 3.141592653589793238
#define E 2.7182818284590452353602874713527
#define M 998244353
const long long INF = 1e18;
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
ll int fact[5001], invfact[5001];

ll power(ll a, ll b, const ll mod){ 
    ll x = 1, y = a; 
    while (b){ 
        if (b & 1){ 
            x *= y;
            x %= mod; 
        } 
        y *= y;
        y %= mod; 
        b >>= 1; 
    } 
    return x; 
}

ll modular_inverse(ll n, const ll mod){ 
    return power(n, mod-2, mod); 
} 
 
ll nCr(ll n, ll k, ll mod){ 
    return (fact[n]*((invfact[k]*invfact[n-k])%mod))%mod;
} 


int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
fact[0]=1;
for (int i = 1; i <=5000; i++)
{
    fact[i]=fact[i-1]*i;
    fact[i]%=M;
}

invfact[5000] = modular_inverse(fact[5000],M);
for(int i=4999;i>=0;i--) 
{
    invfact[i] = invfact[i+1]*(i+1)%M;
}
ll int a,b,c;
cin>>a>>b>>c;
if(a>b)
swap(a,b);
if(a>c)
swap(a,c);
if(b>c)
swap(b,c);
ll int ans=1,ans2=0,ans3=1;
for (int i = 0; i <=a; i++)
{
ans*=nCr(a,i,M);
ans%=M;
ans*=nCr(b,i,M);
ans%=M;
ans*=fact[i]%M;
ans%=M;
ans2+=ans;
ans2%=M;
ans=1;
}
ans3*=ans2;
ans3%=M;
ans2=0;
for (int i = 0; i <=a; i++)
{
ans*=nCr(a,i,M);
ans%=M;
ans*=nCr(c,i,M);
ans%=M;
ans*=fact[i]%M;
ans%=M;
ans2+=ans;
ans2%=M;
ans=1;
}
ans3*=ans2;
ans3%=M;
// error(ans3);
ans2=0;

// error(ans2);
for (int i = 0; i <=b; i++)
{
ans*=nCr(b,i,M);
ans%=M;
ans*=nCr(c,i,M);
ans%=M;
ans*=fact[i]%M;
ans%=M;
ans2+=ans;
ans2%=M;
ans=1;
}

ans3*=ans2;
ans3%=M;
cout<<ans3;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST