結果
問題 | No.1958 Bit Game |
ユーザー |
![]() |
提出日時 | 2022-05-28 00:12:41 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 73 ms / 2,000 ms |
コード長 | 2,830 bytes |
コンパイル時間 | 489 ms |
コンパイル使用メモリ | 46,408 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-20 17:39:29 |
合計ジャッジ時間 | 3,803 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
/* -*- coding: utf-8 -*-** 1958.cc: No.1958 Bit Game - yukicoder*/#include<cstdio>#include<algorithm>using namespace std;/* constant */const int MAX_N = 200000;const int BN = 18;const int MAX_X = 1 << BN;const int MAX_Y = 1 << BN;const int MOD = 998244353;/* typedef */template<const int MOD>struct MI {int v;MI(): v() {}MI(int _v): v(_v % MOD) {}MI(long long _v): v(_v % MOD) {}MI operator+(const MI m) const { return MI(v + m.v); }MI operator-(const MI m) const { return MI(v + MOD - m.v); }MI operator*(const MI m) const { return MI((long long)v * m.v); }MI &operator+=(const MI m) { return (*this = *this + m); }MI &operator-=(const MI m) { return (*this = *this - m); }MI &operator*=(const MI m) { return (*this = *this * m); }MI pow(int n) const { // a^n % MODMI pm = 1, a = *this;while (n > 0) {if (n & 1) pm *= a;a *= a;n >>= 1;}return pm;}MI inv() const { return pow(MOD - 2); }MI operator/(const MI m) const { return *this * m.inv(); }MI &operator/=(const MI m) { return (*this = *this / m); }};typedef MI<MOD> mi;template <typename T>struct Vec {T v[2];Vec() {}Vec(T a, T b) { v[0] = a, v[1] = b; }};template <typename T>struct Mat {T v[2][2];Mat(): v() {}Mat(T a, T b, T c, T d) {v[0][0] = a, v[0][1] = b, v[1][0] = c, v[1][1] = d;}void init() { v[0][0] = v[0][1] = v[1][0] = v[1][1] = 0; }void unit() { v[0][0] = v[1][1] = 1, v[0][1] = v[1][0] = 0; }Mat operator*(const Mat &m) const {return Mat(v[0][0] * m.v[0][0] + v[0][1] * m.v[1][0],v[0][0] * m.v[0][1] + v[0][1] * m.v[1][1],v[1][0] * m.v[0][0] + v[1][1] * m.v[1][0],v[1][0] * m.v[0][1] + v[1][1] * m.v[1][1]);}Vec<T> operator*(const Vec<T> &w) const {return Vec<T>(v[0][0] * w.v[0] + v[0][1] * w.v[1],v[0][0] * w.v[0] + v[0][1] * w.v[1]);}Mat pow(int e) const {Mat mp; mp.unit();Mat ma = *this;while (e > 0) {if (e & 1) mp = mp * ma;ma = ma * ma;e >>= 1;}return mp;}};typedef Vec<mi> vec;typedef Mat<mi> mat;/* global variables */int as[MAX_X], bs[MAX_Y];/* subroutines */int bitcnt(int m, int v[], int k) {int c = 0;for (int i = 0; i < m; i++) c += (v[i] >> k) & 1;return c;}/* main */int main() {int n, x, y;scanf("%d%d%d", &n, &x, &y);for (int i = 0; i < x; i++) scanf("%d", as + i);for (int i = 0; i < y; i++) scanf("%d", bs + i);mi sum = 0;for (int i = 0, bi = 1; i < BN; i++, bi <<= 1) {int ac1 = bitcnt(x, as, i), ac0 = x - ac1;int bc1 = bitcnt(y, bs, i), bc0 = y - bc1;mat ma(ac0, 0, ac1, x);mat mb(y, bc0, 0, bc1);mi v = (mb * ma).pow(n).v[1][0];sum += v * bi;}printf("%d\n", sum.v);return 0;}