JIJ 编程竞赛 2026(第 476 场 AtCoder Beginner Contest)
F
切比雪夫咖啡馆
Chebyshev Cafe
AtCoder
⌀ 1728
原题
← EG →

切比雪夫咖啡馆AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

There is a city represented by a grid with $N$ rows and $N$ columns. The cell at the $i$-th row from the top and $j$-th column from the left is denoted as cell $(i,j)$. Each cell has one cafe.

有一个城市由 $N$ 行 $N$ 列的网格表示。从上往下第 $i$ 行、从左往右第 $j$ 列的格子称为格子 $(i,j)$。每个格子中都有一家咖啡馆。AI (CI翻译)

You are planning a meeting. Among the participants, the number of people living in cell $(i, j)$ is $(A_i \times B_j) \bmod M$. The travel cost for one participant living in cell $(s_r,\ s_c)$ to go to the cafe at cell $(t_r,\ t_c)$ is $\max(|s_r - t_r|,\ |s_c - t_c|)$.

参加者中,居住在格子 $(i, j)$ 的人数为 $(A_i \times B_j) \bmod M$。一名居住在格子 $(s_r,\ s_c)$ 的参加者前往格子 $(t_r,\ t_c)$ 的咖啡馆所需的移动费用为 $\max(|s_r - t_r|,\ |s_c - t_c|)$。AI (CI翻译)

Let $f(i, j)$ be the total travel cost incurred when gathering all the participants at the cafe at cell $(i, j)$.

令 $f(i, j)$ 表示将所有参加者集合到格子 $(i, j)$ 的咖啡馆时产生的总移动费用。AI (CI翻译)

For each cell $(i,j)$, find the following value, and output the bitwise XOR of these values over all cells.

对于每个格子 $(i,j)$,求以下值,并输出所有格子的这些值的按位 XOR:AI (CI翻译)

  • $f(i,j) + (i-1)N + (j-1)$
  • $f(i,j) + (i-1)N + (j-1)$
AI (CI翻译)

What is the bitwise XOR operation? The bitwise XOR of non-negative integers $A$ and $B$ (denoted $A \oplus B$) is defined as follows. The digit in the $2^k$'s place ($k \geq 0$) of $A \oplus B$ written in binary is $1$ if exactly one of the digits in the $2^k$'s place of $A$ and $B$ written in binary is $1$, and $0$ otherwise. For example, $3 \oplus 5 = 6$ (in binary: $011_{(2)} \oplus 101_{(2)} = 110_{(2)}$). More generally, the bitwise XOR of $k$ non-negative integers $p_1, p_2, p_3, \dots, p_k$ is defined as $(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k)$, and it can be proved that this does not depend on the order of $p_1, p_2, p_3, \dots, p_k$.

什么是按位 XOR 运算?非负整数 $A$ 和 $B$ 的按位 XOR(记作 $A \oplus B$)定义如下。将 $A \oplus B$ 写成二进制后,其 $2^k$ 位($k \geq 0$)上的数字为 $1$,当且仅当 $A$ 和 $B$ 的二进制表示在 $2^k$ 位上的数字中恰有一个为 $1$;否则为 $0$。例如,$3 \oplus 5 = 6$(二进制表示为:$011_{(2)} \oplus 101_{(2)} = 110_{(2)}$)。更一般地,$k$ 个非负整数 $p_1, p_2, p_3, \dots, p_k$ 的按位 XOR 定义为 $(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k)$,并且可以证明其结果与 $p_1, p_2, p_3, \dots, p_k$ 的排列顺序无关。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \leq N \leq 1500$
  • $2 \leq M \leq 2 \times 10^6$
  • $1 \leq A_i \leq M-1$ ($1 \leq i \leq N$)
  • $1 \leq B_j \leq M-1$ ($1 \leq j \leq N$)
  • All input values are integers.
  • $1 \leq N \leq 1500$
  • $2 \leq M \leq 2 \times 10^6$
  • $1 \leq A_i \leq M-1$($1 \leq i \leq N$)
  • $1 \leq B_j \leq M-1$($1 \leq j \leq N$)
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入中以以下格式给出:AI (CI翻译)

N M
A_1 \dots A_N
B_1 \dots B_N

N M
A_1 \dots A_N
B_1 \dots B_N
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output the answer.

输出答案。AI (CI翻译)

样例

Input #1
4 19
2 3 1 1
3 4 4 10
Output #1
467
The number of participants living in each cell is as follows: $\displaystyle \begin{bmatrix} 6 & 8 & 8 & 1 \\ 9 & 12 & 12 & 11 \\ 3 & 4 & 4 & 10 \\ 3 & 4 & 4 & 10 \end{bmatrix}$. For example, the value of $f(1,2)$ is obtained as the sum $176$ of the following $16$ values: - The total amount $6 \times 1 = 6$ paid by the $6$ residents of cell $(1,1)$, each of whom pays a travel cost of $\max(|1 - 1|,\ |1 - 2|) = 1$ - The total amount $8 \times 0 = 0$ paid by the $8$ residents of cell $(1,2)$, each of whom pays a travel cost of $\max(|1 - 1|,\ |2 - 2|) = 0$ - The total amount $8 \times 1 = 8$ paid by the $8$ residents of cell $(1,3)$, each of whom pays a travel cost of $\max(|1 - 1|,\ |3 - 2|) = 1$ - The total amount $1 \times 2 = 2$ paid by the $1$ resident of cell $(1,4)$, each of whom pays a travel cost of $\max(|1 - 1|,\ |4 - 2|) = 2$ - $\vdots$ - The total amount $3 \times 3 = 9$ paid by the $3$ residents of cell $(4,1)$, each of whom pays a travel cost of $\max(|4 - 1|,\ |1 - 2|) = 3$ - The total amount $4 \times 3 = 12$ paid by the $4$ residents of cell $(4,2)$, each of whom pays a travel cost of $\max(|4 - 1|,\ |2 - 2|) = 3$ - The total amount $4 \times 3 = 12$ paid by the $4$ residents of cell $(4,3)$, each of whom pays a travel cost of $\max(|4 - 1|,\ |3 - 2|) = 3$ - The total amount $10 \times 3 = 30$ paid by the $10$ residents of cell $(4,4)$, each of whom pays a travel cost of $\max(|4 - 1|,\ |4 - 2|) = 3$ The value of $f(i,j)$ for each cell is as follows: $\displaystyle \begin{bmatrix} 220 & 176 & 179 & 224 \\ 199 & 140 & 136 & 182 \\ 212 & 159 & 143 & 178 \\ 255 & 215 & 201 & 218 \end{bmatrix}$.
Input #2
6 67
1 6 9 2 3 1
1 10 3 7 2 2
Output #2
1021
Input #3
20 676767
60932 508200 22883 481150 426836 554098 276223 307959 133025 573906 17711 413230 274246 500277 524959 477728 546547 43099 324227 660187
552697 293978 246000 159233 213736 628198 394200 194076 618720 453035 615100 331522 669419 180856 530693 399531 223149 159334 102107 598633
Output #3
764668547
题目信息
题号
abc476_f
时间限制
3 s
内存限制
2048 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。