結果
問題 | No.1958 Bit Game |
ユーザー |
![]() |
提出日時 | 2022-05-31 14:42:45 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 585 ms / 2,000 ms |
コード長 | 2,958 bytes |
コンパイル時間 | 2,789 ms |
コンパイル使用メモリ | 77,968 KB |
実行使用メモリ | 66,576 KB |
最終ジャッジ日時 | 2024-09-21 01:17:35 |
合計ジャッジ時間 | 18,245 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
import java.io.*;import java.util.*;public class Main {static final int MOD = 998244353;public static void main(String[] args) throws Exception {Scanner sc = new Scanner();int n = sc.nextInt();long x = sc.nextInt();long y = sc.nextInt();long[] countsX = new long[19];for (int i = 0; i < x; i++) {int v = sc.nextInt();for (int j = 0; j <= 18 && v > 0; j++) {countsX[j] += v % 2;v >>= 1;}}long[] countsY = new long[19];for (int i = 0; i < y; i++) {int v = sc.nextInt();for (int j = 0; j <= 18 && v > 0; j++) {countsY[j] += v % 2;v >>= 1;}}long ans = 0;for (int i = 0; i <= 18; i++) {long[][][] matrix = new long[19][2][2];matrix[0][0][0] = (x * y - countsX[i] * countsY[i]) % MOD;matrix[0][0][1] = x * (y - countsY[i]) % MOD;matrix[0][1][0] = countsX[i] * countsY[i] % MOD;matrix[0][1][1] = x * countsY[i] % MOD;for (int j = 1; j <= 18; j++) {for (int a = 0; a < 2; a++) {for (int b = 0; b < 2; b++) {for (int c = 0; c < 2; c++) {matrix[j][a][c] += matrix[j - 1][a][b] * matrix[j - 1][b][c] % MOD;matrix[j][a][c] %= MOD;}}}}long[] current = new long[2];current[0] = (1 << i);int z = n;for (int j = 18; j >= 0; j--) {if (z >= (1 << j)) {z -= (1 << j);long[] next = new long[2];for (int a = 0; a < 2; a++) {for (int b = 0; b < 2; b++) {next[a] += matrix[j][a][b] * current[b] % MOD;next[a] %= MOD;}}current = next;}}ans += current[1];ans %= MOD;}System.out.println(ans);}}class Scanner {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer("");public Scanner() throws Exception {}public int nextInt() throws Exception {return Integer.parseInt(next());}public long nextLong() throws Exception {return Long.parseLong(next());}public double nextDouble() throws Exception {return Double.parseDouble(next());}public String next() throws Exception {if (!st.hasMoreTokens()) {st = new StringTokenizer(br.readLine());}return st.nextToken();}}