文文的构造游戏

· · 题解

文文的构造游戏

解法

观察到当 m=1 则必定无解。

m=2 时,对于偶数 s,可以将其拆成两个 \dfrac{s}{2},这样异或和为 0 且加起来为 s。即,当 s 为偶数且 m \geq 2 都存在一种构造方式。

s 为奇数时是必定无解的。将 s 拆成若干个正整数相加,会发现 a_1,a_2,\dots,a_n 的二进制最末位必定出现奇数次 1,这就使得 1 无法完全抵消,最后的 a_1 \operatorname{xor} a_2\operatorname{xor} \dots a_n 的最小值也是 1

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        long long s,m;
        cin >> s >> m;
        if (m==1 || (s&1))
            puts("-1");
        else
            cout << 2 << " " << s/2 << " " << s/2 << endl;
    }
    return 0;
}