Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 700 点
問題文
整数 N,K が与えられます. (0,1,\cdots,2^N-1) の順列 P=(P_0,P_1,\cdots,P_{2^N-1}) であって,以下の条件を満たすものが存在するか判定し, また存在するなら一つ構成してください.P の添字が 0 から始まることに注意してください.
- すべての i (0 \leq i \leq 2^N-1) について,P_i と P_{i+1 \mod 2^N} は 2 進表記でちょうど K 桁だけ異なる. なお,比較の際はどちらも leading 0's を補って N 桁に揃えた上で比較する.
制約
- 1 \leq K \leq N \leq 18
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる.
N K
出力
条件を満たす P が存在しない場合,No
と出力せよ.
存在する場合,以下の形式で答えを出力せよ.
Yes P_0 P_1 \cdots P_{2^N-1}
条件を満たす解が複数存在する場合,どれを出力しても正解とみなされる.
入力例 1
3 1
出力例 1
Yes 0 1 3 2 6 7 5 4
P を 2 進表記で書くと,P=(000,001,011,010,110,111,101,100) です.
例えば P_1=001,P_2=011 なので,これらはちょうど 1 桁だけ異なっており,i=1 について条件が成立していることが確認できます. 同様に,すべての i についても条件を満たしています.
入力例 2
2 2
出力例 2
No
Score : 700 points
Problem Statement
You are given integers N and K. Determine whether there exists a permutation P=(P_0,P_1,\cdots,P_{2^N-1}) of (0,1,\cdots,2^N-1) satisfying the condition below, and construct one such sequence if it exists. Note that P is 0-indexed.
- For every i (0 \leq i \leq 2^N-1), P_i and P_{i+1 \mod 2^N} differ by exactly K bits in binary representation. The comparison is made after zero-padding both integers to N bits.
Constraints
- 1 \leq K \leq N \leq 18
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
If there is no P satisfying the condition, print No
.
If there is such a P, print it in the following format:
Yes P_0 P_1 \cdots P_{2^N-1}
If there are multiple solutions satisfying the condition, printing any of them will be accepted.
Sample Input 1
3 1
Sample Output 1
Yes 0 1 3 2 6 7 5 4
Here, we have P=(000,001,011,010,110,111,101,100) in binary representation.
We can see that P_1=001 and P_2=011, for example, differ by exactly 1 bit, satisfying the condition for i=1. The same goes for every i.
Sample Input 2
2 2
Sample Output 2
No