結果

問題 No.1554 array_and_me
ユーザー t33ft33f
提出日時 2021-06-19 09:33:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 2,561 bytes
コンパイル時間 843 ms
コンパイル使用メモリ 83,148 KB
実行使用メモリ 7,008 KB
最終ジャッジ日時 2023-09-05 00:42:07
合計ジャッジ時間 3,796 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,552 KB
testcase_01 AC 19 ms
4,556 KB
testcase_02 AC 19 ms
4,744 KB
testcase_03 AC 19 ms
4,552 KB
testcase_04 AC 19 ms
4,628 KB
testcase_05 AC 19 ms
4,600 KB
testcase_06 AC 46 ms
6,820 KB
testcase_07 AC 45 ms
6,616 KB
testcase_08 AC 46 ms
6,604 KB
testcase_09 AC 46 ms
6,756 KB
testcase_10 AC 45 ms
6,780 KB
testcase_11 AC 28 ms
7,008 KB
testcase_12 AC 27 ms
6,828 KB
testcase_13 AC 27 ms
6,612 KB
testcase_14 AC 27 ms
6,832 KB
testcase_15 AC 27 ms
6,652 KB
testcase_16 AC 7 ms
4,768 KB
testcase_17 AC 7 ms
4,752 KB
testcase_18 AC 7 ms
4,600 KB
testcase_19 AC 7 ms
4,596 KB
testcase_20 AC 7 ms
4,552 KB
testcase_21 AC 34 ms
4,696 KB
testcase_22 AC 34 ms
4,768 KB
testcase_23 AC 33 ms
4,564 KB
testcase_24 AC 33 ms
4,556 KB
testcase_25 AC 34 ms
4,752 KB
testcase_26 AC 38 ms
5,028 KB
testcase_27 AC 38 ms
4,916 KB
testcase_28 AC 39 ms
4,916 KB
testcase_29 AC 38 ms
5,024 KB
testcase_30 AC 38 ms
5,068 KB
testcase_31 AC 38 ms
5,092 KB
testcase_32 AC 38 ms
5,316 KB
testcase_33 AC 39 ms
5,000 KB
testcase_34 AC 38 ms
5,028 KB
testcase_35 AC 38 ms
5,120 KB
testcase_36 AC 36 ms
4,980 KB
testcase_37 AC 36 ms
4,964 KB
testcase_38 AC 36 ms
4,964 KB
testcase_39 AC 37 ms
4,916 KB
testcase_40 AC 36 ms
5,004 KB
testcase_41 AC 21 ms
4,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <queue>
#include <vector>
#include <numeric>
#include <iostream>
using namespace std;
constexpr int M = 998244353;

long long modexp(int x, long long e, int m) {
    long long ans = 1, p = x % m;
    while (e > 0) {
        if (e % 2 != 0) ans = (ans * p) % m;
        p = (p * p) % m;
        e >>= 1;
    }
    return ans;
}

class ModComb {
    long long *fact, *facti;
    const int mod;
public:
    explicit ModComb(int n, int m) : mod(m) {
        fact = new long long[n+1];
        facti = new long long[n+1];
        fact[0] = 1; facti[0] = 1;
        for (int i = 1; i <= n; i++) fact[i] = (fact[i-1] * i) % m;
        // calc 1/n!
        long long &inv = facti[n], pw = fact[n];
        inv = 1;
        for (int e = mod-2; e > 0; e /= 2) {
            if (e&1) inv = inv * pw % mod;
            pw = pw * pw % mod;
        }
        for (int i = n-1; i > 0; i--) facti[i] = (facti[i+1] * (i+1)) % m;
    }

    ~ModComb() {
        if (fact) delete[] fact;
        if (facti) delete[] facti;
    }

    long long getFact(int n) const {
        return fact[n];
    }

    long long getFactInv(int n) const {
        return facti[n];
    }

    long long getComb(int n, int k) const {
        if (n < 0 || k < 0 || k > n) return 0;
        return fact[n] * facti[k] % mod * facti[n-k] % mod;
    }

    long long getPerm(int n, int k) const {
        if (n < 0 || k < 0 || k > n) return 0;
        return fact[n] * facti[n-k] % mod;
    }
};

int main() {
    int t; cin >> t;
    ModComb mc(100100, M);
    while (t--) {
        int n, k; cin >> n >> k;
        vector<int> a(n);
        for (int i = 0; i < n; i++) cin >> a[i];
        struct entry {
            int i, m, a;
            entry(int i, int m, int a) : i(i), m(m), a(a) {}
            bool operator<(const entry& rhs) const {
                return a * rhs.m < rhs.a * m;
            }
        };
        priority_queue<entry> pq;
        for (int i = 0; i < n; i++) pq.emplace(i, 1, a[i]);
        vector<int> f(n);
        int sum = 0;
        while (sum < k) {
            auto [i, m, a] = pq.top();
            pq.pop();
            sum++;
            f[i]++;
            assert(f[i] == m);
            pq.emplace(i, m+1, a);
        }

        long long p = modexp(accumulate(a.begin(), a.end(), 0), M-k-1, M);
        int s = k;
        for (int i = 0; i < n; i++) {
            p = p * mc.getComb(s, f[i]) % M;
            p = p * modexp(a[i], f[i], M) % M;
            s -= f[i];
        }
        assert(s == 0);
        cout << p << endl;
    }
}
0