結果

問題 No.1189 Sum is XOR
ユーザー EbishuEbishu
提出日時 2020-08-02 00:54:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,525 bytes
コンパイル時間 894 ms
コンパイル使用メモリ 95,332 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-23 00:27:46
合計ジャッジ時間 2,840 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
4,380 KB
testcase_01 AC 54 ms
4,376 KB
testcase_02 AC 49 ms
4,380 KB
testcase_03 AC 19 ms
4,376 KB
testcase_04 AC 17 ms
4,380 KB
testcase_05 AC 27 ms
4,380 KB
testcase_06 AC 39 ms
4,376 KB
testcase_07 AC 23 ms
4,380 KB
testcase_08 AC 10 ms
4,376 KB
testcase_09 AC 11 ms
4,380 KB
testcase_10 AC 8 ms
4,376 KB
testcase_11 AC 26 ms
4,380 KB
testcase_12 AC 49 ms
4,376 KB
testcase_13 AC 57 ms
4,376 KB
testcase_14 AC 45 ms
4,376 KB
testcase_15 AC 32 ms
4,376 KB
testcase_16 AC 28 ms
4,376 KB
testcase_17 AC 29 ms
4,380 KB
testcase_18 AC 28 ms
4,376 KB
testcase_19 AC 51 ms
4,380 KB
testcase_20 AC 52 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;
using lint = int64_t;

constexpr lint mod = 998244353;
		
inline bool f(int x, int y) { return x + y == (x xor y); }

lint inv(lint x) {
	lint res = 1, expo = mod - 2;
	while (expo > 0) {
		if (expo & 1) {
			res *= x; res %= mod;
		}
		x *= x; x %= mod;
		expo >>= 1;
	}
	return res;
}

lint solve(int n, int k, const vector<int>& a) {
	lint res = 0;
	if (11 <= k) return 0;
	else {

		unordered_map<int, lint>cnt;
		for (int i = 0; i < n; i++) {
			cnt[a[i]]++;
		}

		if (k == 2) {
			for (auto [i, cnt1] : cnt) {
				for (auto [j, cnt2] : cnt) {
					if (f(i, j)) res += cnt1 * cnt2;
				}
			}
			return res / 2 % mod;
		}
		else {

			vector<unordered_map<int, lint>>mp_vec(k - 2);

			for (auto [i, cnt1] : cnt) {
				for (auto [j, cnt2] : cnt) {
					if (f(i, j)) mp_vec[0][i xor j] += cnt1 * cnt2;
				}
			}

			for (int i = 0; i < k - 2; i++) {
				for (auto [key, value] : mp_vec[i]) {
					for (auto [j, cnt1] : cnt) {
						if (f(key, j)) {
							if (i < k - 3) {
								mp_vec[i + 1][key xor j] += cnt1 * value % mod;
								mp_vec[i + 1][key xor j] %= mod;
							}
							else {
								res += cnt1 * value % mod;
								res %= mod;
							}
						}
					}
				}
			}

			lint fact = 1;
			for (lint i = 2; i <= k; i++) {
				fact *= i; fact %= mod;
			}

			return res * inv(fact) % mod;
		}
	}
}

int main() {
	int n, k;
	cin >> n >> k;
	vector<int>a(n);
	for (int& e : a) cin >> e;
	cout << solve(n, k, a) << endl;
}
0