#P9345. 夕阳西下几时回

    ID: 10327 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>贪心洛谷原创Special JudgeO2优化构造洛谷月赛

夕阳西下几时回

Background

As the sun sets behind the mountains, the last ray of afterglow gradually fades away; the beautiful evening clouds eventually turn into the dark night. In this scene, waves of homesickness rise in one’s heart again and again. For a traveler far from home, even if they do not die in a foreign land, when can they return?

Problem Description

The sunset can be viewed as a graph consisting of nn different colors. The ii-th color is aia_i, where aa is a permutation of length nn.

Define the homesickness value of a permutation as follows:

  • For all 1in1\le i\le n, let bi=gcd(ai,ai+1)b_i=\gcd(a_i,a_{i+1}). In particular, we define an+1=a1a_{n+1}=a_1.
  • The homesickness value of permutation aa is the number of distinct elements in the sequence bb.

Determine whether there exists a permutation pp of length nn whose homesickness value is kk. If it exists, output any such permutation.

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, representing the number of test cases.

For each test case, one line contains two integers n,kn,k.

Output Format

For each test case:

  • If no such permutation exists, output one line with the string No.
  • Otherwise, output one line with the string Yes, then output one line with nn positive integers p1,p2,,pnp_1,p_2,\dots,p_n, representing the permutation you found.

The checker ignores the case of the strings. For example, YES, yEs, and yes are all considered to indicate that an answer exists.

3
7 1
6 5
11 4

Yes
1 2 3 4 5 6 7
No
Yes
1 11 9 3 6 7 8 2 5 10 4

Hint

[Hint]

A permutation of length nn is an array in which every positive integer from 11 to nn appears exactly once. For example, [3,1,2][3,1,2] is a permutation of length 33, while [5,5,1,2,3][5,5,1,2,3] is not a permutation.

[Sample 1 Explanation]

  • For the first test case, b=[1,1,1,1,1,1,1]b=[1,1,1,1,1,1,1], so the homesickness value of pp is 11.
  • For the second test case, it can be proven that no such sequence exists.
  • For the third test case, b=[1,1,3,3,1,1,2,1,5,2,1]b=[1,1,3,3,1,1,2,1,5,2,1], which contains 44 distinct elements — 1,2,31,2,3 and 55 — so the homesickness value of pp is 44.

[Constraints and Notes]

This problem uses bundled tests.

  • Subtask 1 (4 points): n9n\le 9, n100\sum n\le 100.
  • Subtask 2 (5 points): k=1k=1.
  • Subtask 3 (13 points): n200\sum n\le 200.
  • Subtask 4 (30 points): For all testdata, a solution is guaranteed to exist.
  • Subtask 5 (48 points): No special restrictions.

For 100%100\% of the testdata, 1T1051\le T\le 10^5, 3n3×1053\le n\le 3\times 10^5, 1kn1\le k\le n, n6×105\sum n \le 6\times 10^5.

Translated by ChatGPT 5