QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB

#22922. 餘鏡

Statistics

小 R 在整理一面由 $n$ 塊鏡片組成的折鏡時發現:對序列 $a_1, \dots, a_n$ 和整數 $m \ge 2$ 取模後,有些區間會變成回文。

對正整數 $l, r, m$ ($1 \le l \le r \le n$),如果 $[a_l \bmod m, a_{l+1} \bmod m, \dots, a_r \bmod m]$ 是回文序列(即 $(a_i \bmod m) = (a_{r+l-i} \bmod m)$ 對 $l \le i \le r$ 均成立),則定義 $f(l, r, m) = 1$,否則定義 $f(l, r, m) = 0$。

給定上界 $M$,請你求出:

$$\sum_{1 \le l < r \le n} \sum_{2 \le m \le M} f(l, r, m)$$

輸入格式

第一行包含兩個整數 $n, M$ ($1 \le n \le 8000, 2 \le M \le 10^6$),分別代表序列長度與模數上界。

第二行包含 $n$ 個整數 $a_1, a_2, \dots, a_n$ ($0 \le a_i \le M$),代表序列 $a$。

輸出格式

輸出一行一個整數,即所求的答案 $\sum_{1 \le l < r \le n} \sum_{2 \le m \le M} f(l, r, m)$。

答案可能很大,請注意使用 $64$ 位元整數。

範例

輸入格式 1

5 4
1 3 2 4 3

輸出格式 1

3

說明

共有以下 $3$ 組 $(l, r, m)$ 使得 $f(l, r, m) = 1$:

  • $(l, r, m) = (1, 2, 2)$:此時 $[a_1 \bmod 2, a_2 \bmod 2] = [1, 1]$ 是回文序列。
  • $(l, r, m) = (3, 4, 2)$:此時 $[a_3 \bmod 2, a_4 \bmod 2] = [0, 0]$ 是回文序列。
  • $(l, r, m) = (2, 5, 2)$:此時 $[a_2 \bmod 2, a_3 \bmod 2, a_4 \bmod 2, a_5 \bmod 2] = [1, 0, 0, 1]$ 是回文序列。

對於 $(l, r, m) = (2, 5, 3)$,$[a_2 \bmod 3, a_3 \bmod 3, a_4 \bmod 3, a_5 \bmod 3] = [0, 2, 1, 0]$ 不是回文序列,因此 $f(2, 5, 3) = 0$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.