#P16530. [THUPC 2026 决赛] 积木消除游戏

    ID: 18902 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>动态规划 DP贪心前缀和2026THUPC线性 DP

[THUPC 2026 决赛] 积木消除游戏

Background

From the final round of the 2026 Tsinghua University Programming Contest and Collegiate Invitational (THUPC2026).

Resources such as the editorial can be found at https://github.com/dapingguo8/THUPC2026-final.

After enjoying the gorgeous illusion photos, everyone was attracted by a nearby block elimination mini-game area.

On the table, colorful blocks are neatly arranged in piles. As the booth owners, Little T and Little S each provide a magic sieve that can eliminate blocks in batches. The rules are simple: you may use these two sieves repeatedly to eliminate blocks, and the ranking is determined by the total number of blocks remaining on the table.

Problem Description

On the table, there are nn piles of blocks arranged neatly. The initial number of blocks in pile i (1in)i \ (1 \le i \le n) is aia_i.

Little T and Little S provide two magic sieves with mesh sizes p,qp, q. They can eliminate blocks in batches by taking the covered piles modulo the corresponding number. When naturally unfolded, each sieve spans exactly kk piles in width. They have special elasticity: they can be freely stretched to cover a longer range at both ends, but cannot be compressed inward to cover a shorter range. The sieves are used as follows:

  • Choose a consecutive block interval [l,r][l, r] with length at least kk, and place a sieve over it.
  • Choose one of the two magic sieves, i.e., choose m{p,q}m \in \{p, q\}.
  • For each pile within [l,r][l, r], take its count modulo mm, i.e., set aiaimodma_i \gets a_i \bmod m.

Since you have joined this game, you naturally do not want an average result. To take the top spot on the leaderboard, you want to know: by using the magic sieves any number of times, what is the minimum possible total number of blocks remaining on the table (i.e., i=1nai\sum_{i = 1} ^ n a_i)?

Input Format

Each test point contains multiple sets of testdata. The first line of the input contains a positive integer T (1T104)T \ (1 \le T \le 10 ^ 4), indicating the number of test cases. For each test case:

  • The first line contains four positive integers $n, k, p, q \ (1 \le k \le n \le 10 ^ 5, \ 1 \le p < q \le 10 ^ 9)$, representing the number of block piles, the number of piles spanned when a sieve is naturally unfolded, and the mesh sizes of the two magic sieves.
  • The second line contains nn positive integers a1,a2,,an (1ai109)a_1, a_2, \dots, a_n \ (1 \le a_i \le 10 ^ 9), representing the initial number of blocks in each pile.

It is guaranteed that the sum of nn over all test cases does not exceed 10510 ^ 5.

Output Format

For each test case, output one line with a non-negative integer, representing the minimum possible total number of blocks remaining on the table.

6
1 1 3 4
2026
3 2 10 20
31 41 59
4 3 3 4
1 2 3 4
6 4 9 20
18 27 180 9 45 99
7 4 3 5
6 7 14 12 100 78 4
9 4 244 353
9982 4435 3998 2443 5399 8244 3539 9824 4353
1
11
3
0
4
569

Hint

For the second test case, one sequence of operations that makes the minimum total number of remaining blocks equal to 1111 is:

  • Choose interval [1,4][1, 4] and use the magic sieve with mesh size 1010, then the remaining block counts become [1,1,9][1, 1, 9].

For the third test case, one sequence of operations that makes the minimum total number of remaining blocks equal to 33 is:

  • Choose interval [2,4][2, 4] and use the magic sieve with mesh size 44, then the remaining block counts become [1,2,3,0][1, 2, 3, 0].
  • Choose interval [1,3][1, 3] and use the magic sieve with mesh size 33, then the remaining block counts become [1,2,0,0][1, 2, 0, 0].

Translated by ChatGPT 5