luogu#P16331. After All ~綴る想い~

    ID: 16154 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>洛谷原创Special JudgeO2优化扩展欧几里德算法中国剩余定理 CRT构造洛谷月赛Ad-hoc

After All ~綴る想い~

Problem Description

You are given a cycle of length nn. Initially, each position has value 00, and the positions are numbered from 11 to nn. You are also given two sequences aia_i and bib_i, both of length nn. You may perform the following operation any number of times:

  • Choose a point ii, and add or subtract bib_i to the consecutive segment of length ii starting from position ii and going forward along the cycle.

Determine whether there exists a sequence of operations such that the value at every position on the cycle is exactly equal to aia_i. If so, construct one. For each position, output the number of times you add bib_i minus the number of times you subtract bib_i.

It is guaranteed that lcm(bi)109\operatorname{lcm}(b_i)\le 10^9.

If for some test case you can only determine whether a solution exists but cannot construct one, you may still obtain partial score for that test case. See Scoring for details.

Input Format

The first line contains a positive integer TT, the number of test cases. For each test case:

  • The first line contains a positive integer nn, the length of the cycle.

  • The second line contains nn integers aia_i, the target values.

  • The third line contains nn positive integers bib_i.

Output Format

For each test case:

  • Output YES or NO on the first line, indicating your verdict.

  • If a solution exists, output nn integers on the second line, representing your construction. You must ensure that every integer lies between 1032-10^{32} and 103210^{32}.

7
5
0 2 2 6 6
1 2 2 2 2
5
-6 -8 -6 21 -6
1 4 2 4 4
5
-2 10 16 0 0
6 6 1 2 2
5
22 14 -2 14 14
4 4 4 8 1
5
2 6 2 2 2
2 1 1 2 1
5
2 3 7 8 8
2 1 2 1 2
5
-5 1 4 -2 -2
1 4 1 1 1
YES
-2 0 2 2 -1 
NO
YES
0 2 2 -2 1 
YES
0 -2 -2 1 14 
YES
0 4 0 2 -2 
YES
0 1 3 2 0 
YES
-2 1 1 -2 -1 

Hint

Constraints

For all test cases, 1n,T,n1061\le n,T,\sum n\le 10^6, 0ai1090\le |a_i|\le 10^9, 1bi,lcm(bi)1091\le b_i,\operatorname{lcm}(b_i)\le 10^9.

Subtask nn\le Special Property Score
11 55 A 1010
22 10610^6 B
33 C 2020
44 D
55 None 4040
  • Special Property A: It is guaranteed that T10T\le 10, and if a solution exists, then there is always one where the number of additions or subtractions for every bib_i does not exceed 55.

  • Special Property B: It is guaranteed that there exists a nonnegative integer kk such that n=2kn=2^k.

  • Special Property C: It is guaranteed that all aia_i are equal.

  • Special Property D: It is guaranteed that 2n2\nmid n.

Scoring

  • If you correctly determine the existence of a solution for all test cases, you can obtain 4040% of the score for that test point. Note that if you can only determine existence, then for the test cases where a solution exists, you should still output a second line consisting of nn integers within the required range.

  • If you correctly determine the existence of a solution for all test cases, and for every test case with a solution you also construct a valid one, you can obtain 6060% of the score for that test point.