1569C - Jury Meeting - CodeForces Solution


combinatorics math *1500

Please click on ads to support us..

Python Code:

t=int(input())
mod=998244353
for asdad in range(t):
    mx=0
    mcnt=0
    n=int(input())
    liste=[-1]
    for a in input().split():
        liste.append(int(a))
        mx=max(mx,int(a))
    mcnt=liste.count(mx)
    p,b=1,1
    ans=1
    if mcnt==1:
        mcnt=liste.count(mx-1)
        p=mcnt+1
        b=mcnt
    for i in range(1,n+1):
        if(i==p):
            ans=(ans*b)%mod
        else:
            ans=(ans*i)%mod
    print(ans)

C++ Code:

#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <random>
#include <algorithm>
#include <unordered_map>
#include <map>
using namespace std;

#define int long long

int M = 998244353;
int fact[200002] = {};
int rfact[200002] = {};


int bin_pow(int x, int n) {
    if (n == 0) return 1;
    if (n % 2) {
        return x * bin_pow(x, n-1) % M;
    } else {
        int tmp = bin_pow(x, n / 2);
        return tmp * tmp % M;
    }
}

int A(int n, int k) {
    return fact[n] * rfact[n-k] % M;
}

void solve() {
    int n;
    cin >> n;
    vector<int> arr(n);
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }
    sort(arr.begin(), arr.end());
    if (arr[n-1] - arr[n-2] == 0) {
        cout << fact[n] << "\n";
    } else if (arr[n-1] - arr[n-2] == 1) {
        int cnt = 1;
        for (int i = n-3; i >= 0; --i) {
            if (arr[i] == arr[i + 1]) {
                ++cnt;
            } else {
                break;
            }
        }
        int res = 0;
        for (int i = 0; i < n; ++i) {
            res += fact[n-1];
            res %= M;
            if (i >= cnt) {
                res -= A(i, cnt) * fact[n - 1 - cnt] % M;
            }
            res = (res + M) % M;
        }
        cout << res << "\n";
    } else {
        cout << 0 << "\n";
    }

}



signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    fact[0] = 1;
    for (int i = 1; i < 200002; ++i) {
        fact[i] = fact[i-1] * i % M;
    }
    rfact[200001] = bin_pow(fact[200001], M-2);
    for (int i = 200000; i >= 0; --i) {
        rfact[i] = rfact[i + 1] * (i + 1) % M;
    }
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons