#P9654. 『GROI-R2』 记忆碎片

    ID: 10012 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP数学数论洛谷原创Special JudgeO2优化构造洛谷月赛

『GROI-R2』 记忆碎片

Problem Description

Fragments of memory are scattered everywhere, and Alice wants to piece them back together.

The order of the fragments is fixed, because memory obviously cannot proceed backward. However, the shape and content of each fragment can be polished and adjusted—after all, everyone’s memories get distorted and forgotten.

The memory on each fragment can be represented by a non-negative integer. Two adjacent fragments can be perfectly joined if and only if the sum of their memories is a perfect square.

Now Alice can polish some fragments so that every pair of adjacent fragments can be perfectly joined. In each polishing operation, Alice may choose one fragment and change the memory on it to any non-negative integer. Alice wants to know the minimum number of fragments she must polish to achieve her goal. She also wants you to output what memory remains on each fragment after polishing.

Formal statement

Given a non-negative integer sequence {an}\{a_n\}, we define one operation as choosing any i[1,n]i\in [1,n] and changing aia_i to any non-negative integer xx.

Ask for the minimum number of operations needed so that i[1,n1]\forall i\in [1,n-1], ai+ai+1a_i+a_{i+1} is a perfect square, and output a modification scheme.

Input Format

The first line contains an integer nn, the number of memory fragments.

The second line contains a non-negative integer sequence aa of length nn, representing the memory on each fragment.

Output Format

The first line outputs an integer, the minimum number of polishing operations.

The second line outputs an integer sequence of length nn, representing the memory on every fragment after all polishing.

You must ensure that the memories after polishing satisfy the conditions in the statement, match the minimum number of operations you output, and that each fragment’s memory lies in the range [0,1018][0,10^{18}].

4
1 3 5 8
1
1 3 1 8
3
3 4 5
1
0 4 5

Hint

Sample explanation

For the first sample, it is not hard to prove that Alice must polish at least one fragment to make all memories satisfy the condition.

Please note that the order of the memory fragments cannot be changed.

Scoring rules

If the minimum number of polishing operations you output is correct for a test point, you will get 30%30\% of the score for that test point.

If, based on the correct minimum number, you also provide a correct construction, you will get 100%100\% of the score for that test point.

If you can only compute the minimum number of operations, please still output nn integers separated by spaces on the second line, each within [0,1018][0,10^{18}], otherwise you may receive 00 points.

Please note that in each subtask, the 30%30\% score you get will be floored.

Constraints

This problem uses bundled tests.

Subtask\text{Subtask} nn\le aia_i\le Special property Score
11 22 10810^8 55
22 33 2020
33 44 1515
44 10310^3
55 10610^6 10410^4 1010
66 10810^8 A\text{A}
77 2525

Special property A\text{A}: 1i,jn\forall 1\le i,j\le n, ai=aja_i=a_j.

For 100%100\% of the testdata, 1n1061\le n\le 10^6 and 0ai1080\le a_i\le 10^8.

Translated by ChatGPT 5