33OJ
登录 注册

#P18103. [CCPC 2026 长春站] 余镜

    ID: 20662 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>最大公约数 gcd2026线性筛法CCPC

[CCPC 2026 长春站] 余镜

Problem Description

While tidying up a folding mirror made of nn mirror pieces, Little R finds that after taking the sequence a1,…,ana_1,\dots,a_n modulo an integer m≥2m\ge 2, some intervals turn into palindromes.

For positive integers l,r,ml,r,m (1≤l≤r≤n1\le l\le r\le n), if [al mod m, al+1 mod m, …, ar mod m][a_l\bmod m,\ a_{l+1}\bmod m,\ \dots,\ a_r\bmod m] is a palindromic sequence (that is, (ai mod m)=(ar+l−i mod m)(a_i \bmod m)=(a_{r+l-i}\bmod m) holds for every l≤i≤rl\le i \le r), define f(l,r,m)=1f(l,r,m)=1; otherwise define f(l,r,m)=0f(l,r,m)=0.

Given the upper bound MM, please compute:

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

Input Format

The first line contains two integers n,Mn,M (1≤n≤80001\le n \le 8000, 2≤M≤1062\le M \le 10^6), representing the length of the sequence and the upper bound of the modulus.

The second line contains nn integers a1,a2,…,ana_1,a_2,\dots,a_n (0≤ai≤M0\le a_i \le M), representing the sequence aa.

Output Format

Output a single line with one integer, which is the required answer $\sum\limits_{1\le l<r\le n}\ \sum\limits_{2\le m\le M} f(l,r,m)$.

The answer may be very large, so please use 64-bit integers.

5 4
1 3 2 4 3
3

Hint

In total there are the following 33 triples (l,r,m)(l,r,m) with f(l,r,m)=1f(l,r,m)=1:

  • (l,r,m)=(1,2,2)(l,r,m)=(1,2,2): here [a1 mod 2, a2 mod 2]=[1,1][a_1\bmod 2,\ a_2\bmod 2]=[1,1] is a palindromic sequence.
  • (l,r,m)=(3,4,2)(l,r,m)=(3,4,2): here [a3 mod 2, a4 mod 2]=[0,0][a_3\bmod 2,\ a_4\bmod 2]=[0,0] is a palindromic sequence.
  • (l,r,m)=(2,5,2)(l,r,m)=(2,5,2): here $[a_2\bmod 2,\ a_3\bmod 2,\ a_4\bmod 2,\ a_5\bmod 2]=[1,0,0,1]$ is a palindromic sequence.

For (l,r,m)=(2,5,3)(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]$ is not a palindromic sequence, so f(2,5,3)=0f(2,5,3)=0.

Translated by QwenWork