結果

問題 No.1554 array_and_me
ユーザー SSRSSSRS
提出日時 2021-06-18 22:11:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 1,970 bytes
コンパイル時間 1,890 ms
コンパイル使用メモリ 176,692 KB
実行使用メモリ 9,312 KB
最終ジャッジ日時 2023-09-04 23:17:52
合計ジャッジ時間 5,501 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 28 ms
4,380 KB
testcase_02 AC 29 ms
4,380 KB
testcase_03 AC 29 ms
4,380 KB
testcase_04 AC 29 ms
4,380 KB
testcase_05 AC 29 ms
4,380 KB
testcase_06 AC 88 ms
9,164 KB
testcase_07 AC 87 ms
9,184 KB
testcase_08 AC 87 ms
9,164 KB
testcase_09 AC 87 ms
9,188 KB
testcase_10 AC 87 ms
9,312 KB
testcase_11 AC 41 ms
7,056 KB
testcase_12 AC 42 ms
8,464 KB
testcase_13 AC 42 ms
6,940 KB
testcase_14 AC 43 ms
6,936 KB
testcase_15 AC 42 ms
6,880 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 7 ms
4,380 KB
testcase_18 AC 8 ms
4,384 KB
testcase_19 AC 8 ms
4,380 KB
testcase_20 AC 8 ms
4,380 KB
testcase_21 AC 52 ms
4,384 KB
testcase_22 AC 53 ms
4,384 KB
testcase_23 AC 52 ms
4,384 KB
testcase_24 AC 52 ms
4,380 KB
testcase_25 AC 52 ms
4,384 KB
testcase_26 AC 62 ms
4,384 KB
testcase_27 AC 61 ms
4,380 KB
testcase_28 AC 62 ms
4,380 KB
testcase_29 AC 61 ms
4,380 KB
testcase_30 AC 62 ms
4,380 KB
testcase_31 AC 61 ms
4,380 KB
testcase_32 AC 62 ms
4,380 KB
testcase_33 AC 62 ms
4,380 KB
testcase_34 AC 62 ms
4,384 KB
testcase_35 AC 62 ms
4,380 KB
testcase_36 AC 62 ms
4,380 KB
testcase_37 AC 62 ms
4,380 KB
testcase_38 AC 61 ms
4,380 KB
testcase_39 AC 62 ms
4,380 KB
testcase_40 AC 62 ms
4,380 KB
testcase_41 AC 31 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
long long modpow(long long a, long long b){
	long long ans = 1;
	while (b > 0){
		if (b % 2 == 1){
			ans *= a;
			ans %= MOD;
		}
		a *= a;
		a %= MOD;
		b /= 2;
	}
	return ans;
}
long long modinv(long long a){
	return modpow(a, MOD - 2);
}
vector<long long> mf = {1};
vector<long long> mfi = {1};
long long modfact(int n){
	if (mf.size() > n){
		return mf[n];
	} else {
		for (int i = mf.size(); i <= n; i++){
			long long next = mf.back() * i % MOD;
			mf.push_back(next);
			mfi.push_back(modinv(next));
		}
		return mf[n];
	}
}
long long modfactinv(int n){
	if (mfi.size() > n){
		return mfi[n];
	} else {
		return modinv(modfact(n));
	}
}
long long gcd(long long a, long long b){
  if (b == 0){
    return a;
  } else {
    return gcd(b, a % b);
  }
}
struct fraction{
  long long num, den;
  fraction(long long num, long long den): num(num), den(den){
    long long g = gcd(num, den);
    num /= g;
    den /= g;
  }
};
bool operator <(fraction a, fraction b){
  return a.num * b.den < b.num * a.den;
}
int main(){
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    for (int j = 0; j < N; j++){
      cin >> A[j];
    }
    int S = 0;
    for (int j = 0; j < N; j++){
      S += A[j];
    }
    vector<int> c(N, 0);
    priority_queue<pair<fraction, int>> pq;
    for (int j = 0; j < N; j++){
      pq.push(make_pair(fraction(A[j], 1), j));
    }
    for (int j = 0; j < K; j++){
      int id = pq.top().second;
      pq.pop();
      c[id]++;
      pq.push(make_pair(fraction(A[id], c[id] + 1), id));
    }
    long long ans = 1;
    for (int j = 0; j < N; j++){
      long long p = A[j] * modinv(S % MOD) % MOD;
      ans *= modpow(p, c[j]);
      ans %= MOD;
    }
    ans *= modfact(K);
    ans %= MOD;
    for (int j = 0; j < N; j++){
      ans *= modfactinv(c[j]);
      ans %= MOD;
    }
    cout << ans << endl;
  }
}
0