結果

問題 No.1554 array_and_me
ユーザー 👑 ygussanyygussany
提出日時 2021-06-18 21:49:35
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 2,386 bytes
コンパイル時間 916 ms
コンパイル使用メモリ 30,960 KB
実行使用メモリ 9,744 KB
最終ジャッジ日時 2023-09-04 22:46:01
合計ジャッジ時間 5,026 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,564 KB
testcase_01 AC 32 ms
7,512 KB
testcase_02 AC 33 ms
7,400 KB
testcase_03 AC 34 ms
7,472 KB
testcase_04 AC 33 ms
7,592 KB
testcase_05 AC 33 ms
7,524 KB
testcase_06 AC 86 ms
9,596 KB
testcase_07 AC 94 ms
9,744 KB
testcase_08 AC 92 ms
9,552 KB
testcase_09 AC 91 ms
9,720 KB
testcase_10 AC 101 ms
9,636 KB
testcase_11 AC 64 ms
9,600 KB
testcase_12 AC 67 ms
9,628 KB
testcase_13 AC 75 ms
9,652 KB
testcase_14 AC 73 ms
9,632 KB
testcase_15 AC 84 ms
9,548 KB
testcase_16 AC 3 ms
7,504 KB
testcase_17 AC 3 ms
7,568 KB
testcase_18 AC 3 ms
7,496 KB
testcase_19 AC 4 ms
7,576 KB
testcase_20 AC 4 ms
7,588 KB
testcase_21 AC 71 ms
7,520 KB
testcase_22 AC 69 ms
7,460 KB
testcase_23 AC 69 ms
7,504 KB
testcase_24 AC 69 ms
7,576 KB
testcase_25 AC 71 ms
7,556 KB
testcase_26 AC 84 ms
7,576 KB
testcase_27 AC 80 ms
7,716 KB
testcase_28 AC 80 ms
7,628 KB
testcase_29 AC 82 ms
7,508 KB
testcase_30 AC 84 ms
7,512 KB
testcase_31 AC 85 ms
7,568 KB
testcase_32 AC 84 ms
7,516 KB
testcase_33 AC 84 ms
7,628 KB
testcase_34 AC 80 ms
7,604 KB
testcase_35 AC 83 ms
7,616 KB
testcase_36 AC 80 ms
7,636 KB
testcase_37 AC 73 ms
7,668 KB
testcase_38 AC 78 ms
7,572 KB
testcase_39 AC 80 ms
7,576 KB
testcase_40 AC 75 ms
7,636 KB
testcase_41 AC 35 ms
7,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 998244353;
long long fact[100001], fact_inv[100001];

typedef struct {
	long double key;
	int id;
} data;

typedef struct {
	data obj[100001];
	int size;
} min_heap;
 
void push(data x, min_heap* h)
{
	int i = ++(h->size), j = i >> 1;
	data tmp;
	h->obj[i] = x;
	while (j > 0) {
		if (h->obj[i].key < h->obj[j].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j >>= 1;
		} else break;
	}
}

data pop(min_heap* h)
{
	int i = 1, j = 2;
	data output = h->obj[1], tmp;
	h->obj[1] = h->obj[(h->size)--];
	while (j <= h->size) {
		if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1;
		if (h->obj[j].key < h->obj[i].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j = i << 1;
		} else break;
	}
	return output;
}

long long div_mod(long long x, long long y, long long z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

long long pow_mod(int n, int k)
{
	long long N, ans = 1;
	for (N = n; k > 0; k >>= 1, N = N * N % Mod) if (k & 1) ans = ans * N % Mod;
	return ans;
}

long long combination(int n, int k)
{
	return fact[n] * fact_inv[k] % Mod * fact_inv[n-k] % Mod;
}

void solve()
{
	int i, j, k = 0, N, K, sum = 0;
	long long ans = 1;
	data d;
	static int A[100001], num[100001];
	static long long prob[100001];
	static long double likely[100001];
	static min_heap h;
	h.size = 0;
	scanf("%d %d", &N, &K);
	for (i = 0; i < N; i++) {
		scanf("%d", &(A[i]));
		sum += A[i];
	}
	for (i = 0; i < N; i++) {
		prob[i] = div_mod(A[i], sum, Mod);
		likely[i] = (long double)A[i] * K / sum;
		num[i] = (long long)A[i] * K / sum;
		k += num[i];
		d.key = (num[i] + 1 - likely[i]) / likely[i];
		d.id = i;
		push(d, &h);
	}
	for (i = 1; i <= K - k; i++) {
		d = pop(&h);
		j = d.id;
		num[j]++;
		d.key = (num[j] + 1 - likely[j]) / likely[j];
		push(d, &h);
	}
	for (i = 0; i < N; i++) {
		ans = ans * pow_mod(prob[i], num[i]) % Mod * combination(K, num[i]) % Mod;
		K -= num[i];
	}
	printf("%lld\n", ans);
}

int main()
{
	int i;
	for (i = 1, fact[0] = 1; i <= 100000; i++) fact[i] = fact[i-1] * i % Mod;
	for (i = 99999, fact_inv[100000] = div_mod(1, fact[100000], Mod); i >= 0; i--) fact_inv[i] = fact_inv[i+1] * (i + 1) % Mod;

	int t, T;
	scanf("%d", &T);
	for (i = 1; i <= T; i++) solve();
	fflush(stdout);
	return 0;
}
0