結果

問題 No.1554 array_and_me
ユーザー rtyu
提出日時 2021-07-13 14:29:43
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,020 bytes
コンパイル時間 753 ms
コンパイル使用メモリ 76,884 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-02 13:24:44
合計ジャッジ時間 3,083 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

long long md = 998244353;

struct sn
{
	int p, q;
	bool operator<(const sn &r) const {
		return p * r.q < r.p * q;
	}
	sn(int p, int q) : p(p), q(q) {};
	sn() : p(0), q(1) {};
};

long long fp(long long n, long long k)
{
	long long s = 1;
	while (k) {
		if (k & 1) s = (s * n) % md;
		n = (n * n) % md; k /= 2;
	}
	return s;
}

priority_queue<sn> qs;

int main()
{ 
	ios::sync_with_stdio(false);
	cin.tie(0);
	int tc; cin >> tc;
	for (int ti = 0; ti < tc; ti++) {
		int n, k; cin >> n >> k;
		long long p = 1, q = 1, t = 0;
		while (!qs.empty())
			qs.pop();
		for (int i = 1; i <= n; i++) {
			int a; cin >> a;
			t += a;
			qs.push(sn(a, 1));
		}
		for (int i = 1; i <= k; i++) {
			p = (p * i) % md;
			q = (q * t) % md;
		}
		for (int i = 0; i < k; i++) {
			sn s = qs.top(); qs.pop();
			p = (p * s.p) % md; q = (q * s.q) % md;
			s.q++;
			qs.push(s);
		}
		cout << (p * fp(q, md - 2)) % md << '\n';
	}
	return 0;
}
0