Mainak and Interesting Sequence (Codeforces 1726B)
Tags: Math, bitmasks

Mainak has two positive integers $$$n$$$ and $$$m$$$.Mainak finds a sequence $$$a_1, a_2, \ldots, a_n$$$ of $$$n$$$ positive integers interesting, if for all integers $$$i$$$ ($$$1 \le i \le n$$$), the bitwise XOR of all elements in $$$a$$$ which are strictly less than $$$a_i$$$ is $$$0$$$. Formally if $$$p_i$$$ is the bitwise XOR of all elements in $$$a$$$ which are strictly less than $$$a_i$$$, then $$$a$$$ is an interesting sequence if $$$p_1 = p_2 = \ldots = p_n = 0$$$.For example, sequences $$$[1,3,2,3,1,2,3]$$$, $$$[4,4,4,4]$$$, $$$[25]$$$ are interesting, whereas $$$[1,2,3,4]$$$ ($$$p_2 = 1 \ne 0$$$), $$$[4,1,1,2,4]$$$ ($$$p_1 = 1 \oplus 1 \oplus 2 = 2 \ne 0$$$), $$$[29,30,30]$$$ ($$$p_2 = 29 \ne 0$$$) aren't interesting.Here $$$a \oplus b$$$ denotes bitwise XOR of integers $$$a$$$ and $$$b$$$.Find any interesting sequence $$$a_1, a_2, \ldots, a_n$$$ (or report that there exists no such sequence) such that the sum of the elements in the sequence $$$a$$$ is equal to $$$m$$$, i.e. $$$a_1 + a_2 \ldots + a_n = m$$$.As a reminder, the bitwise XOR of an empty sequence is considered to be $$$0$$$.

Input: Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Description of the test cases follows.The first line and the only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le m \le 10^9$$$) — the length of the sequence and the sum of the elements.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: For each test case, if there exists some interesting sequence, output "Yes" on the first line, otherwise output "No". You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).If the answer is "Yes", output $$$n$$$ positive integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i \ge 1$$$), forming an interesting sequence such that $$$a_1 + a_2 \ldots + a_n = m$$$. If there are multiple solutions, output any.

Note:  In the first test case, $$$[3]$$$ is the only interesting sequence of length $$$1$$$ having sum $$$3$$$. In the third test case, there is no sequence of length $$$2$$$ having sum of elements equal to $$$1$$$, so there is no such interesting sequence. In the fourth test case, $$$p_1 = p_2 = p_3 = 0$$$, because bitwise XOR of an empty sequence is $$$0$$$.

Sample Inputs:
4
1 3
6 12
2 1
3 6

Sample Outputs:
Yes
3
Yes
1 3 2 2 3 1
No
Yes
2 2 2

Solution (Python):
import sys
INF = 10 ** 20
MOD = 10 ** 9 + 7
from math import gcd
from math import ceil, log2, factorial
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br

def fastio():
    import sys
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
I = lambda : list(map(int, input().split()))
TESTS = 1
''

def solve():
    (n, m) = I()
    if m < n:
        print('NO')
        return
    if n == 1:
        print('YES')
        print(m)
        return
    ans = []
    if n % 2:
        print('YES')
        for i in range(n - 1):
            ans.append(1)
        m -= sum(ans)
        ans.append(m)
        print(*ans)
    elif m % 2:
        print('NO')
        return
    else:
        print('YES')
        for i in range(n - 2):
            ans.append(1)
        m -= sum(ans)
        ans += [m // 2, m // 2]
        print(*ans)

def main():
    if not TESTS:
        solve()
        return
    (t,) = I()
    while t:
        solve()
        t -= 1
if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin, stdout
(input, print) = (stdin.readline, stdout.write)

def main():
    for _ in range(int(input())):
        (n, m) = map(int, input().split())
        if m % n == 0:
            print('Yes\n')
            print((str(m // n) + ' ') * n + '\n')
        elif m < n:
            print('No\n')
        elif n % 2 == 1:
            print('Yes\n')
            print((str(m // n) + ' ') * (n - 1) + str(m // n + m % n) + '\n')
        elif m % 2 == 1:
            print('No\n')
        else:
            print('Yes\n')
            print('1 ' * (n - 2) + str((m - n + 2) // 2) + ' ' + str((m - n + 2) // 2) + '\n')
main()

Solution (Python):
from operator import inv
import sys
input = sys.stdin.readline

def inp():
    return int(input())

def inlt():
    return list(map(int, input().split()))

def insr():
    s = input()
    return list(s[:len(s) - 1])

def invr():
    return map(int, input().split())
for _ in range(inp()):
    (n, k) = invr()
    if n == 1:
        print(f'Yes\n{k}')
    elif k >= n:
        if k % 2 == 0 and n % 2 == 0:
            print('Yes')
            for i in range(n - 2):
                print(1, end=' ')
                k -= 1
            print(f'{k // 2} {k // 2}')
        elif n % 2 != 0:
            print('Yes')
            for i in range(n - 1):
                print(1, end=' ')
                k -= 1
            print(k)
        else:
            print('No')
    else:
        print('No')