A14798 | And Matching
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a set of $n$ ( $n$ is always a power of $2$ ) elements containing all integers $0, 1, 2, \ldots, n-1$ exactly once.
Find $\frac{n}{2}$ pairs of elements such that:
- Each element in the set is in exactly one pair.
- The sum over all pairs of the [bitwise AND](https://en.wikipedia.org/wiki/Bitwise_operation#AND) of its elements must be exactly equal to $k$ . Formally, if $a_i$ and $b_i$ are the elements of the $i$ -th pair, then the following must hold: $$$$\sum_{i=1}^{n/2}{a_i \& b_i} = k, $$ where $\\&$ denotes the bitwise AND operation. </li></ul><p>If there are many solutions, print any of them, if there is no solution, print $-1$$$ instead.
Find $\frac{n}{2}$ pairs of elements such that:
- Each element in the set is in exactly one pair.
- The sum over all pairs of the [bitwise AND](https://en.wikipedia.org/wiki/Bitwise_operation#AND) of its elements must be exactly equal to $k$ . Formally, if $a_i$ and $b_i$ are the elements of the $i$ -th pair, then the following must hold: $$$$\sum_{i=1}^{n/2}{a_i \& b_i} = k, $$ where $\\&$ denotes the bitwise AND operation. </li></ul><p>If there are many solutions, print any of them, if there is no solution, print $-1$$$ instead.
输入格式
The input consists of multiple test cases. The first line contains a single integer $t$ ( $1 \leq t \leq 400$ ) — the number of test cases. Description of the test cases follows.
Each test case consists of a single line with two integers $n$ and $k$ ( $4 \leq n \leq 2^{16}$ , $n$ is a power of $2$ , $0 \leq k \leq n-1$ ).
The sum of $n$ over all test cases does not exceed $2^{16}$ . All test cases in each individual input will be pairwise different.
Each test case consists of a single line with two integers $n$ and $k$ ( $4 \leq n \leq 2^{16}$ , $n$ is a power of $2$ , $0 \leq k \leq n-1$ ).
The sum of $n$ over all test cases does not exceed $2^{16}$ . All test cases in each individual input will be pairwise different.
输出格式
For each test case, if there is no solution, print a single line with the integer $-1$ .
Otherwise, print $\frac{n}{2}$ lines, the $i$ -th of them must contain $a_i$ and $b_i$ , the elements in the $i$ -th pair.
If there are many solutions, print any of them. Print the pairs and the elements in the pairs in any order.
Otherwise, print $\frac{n}{2}$ lines, the $i$ -th of them must contain $a_i$ and $b_i$ , the elements in the $i$ -th pair.
If there are many solutions, print any of them. Print the pairs and the elements in the pairs in any order.
输入输出样例
输入 #1
4 4 0 4 1 4 2 4 3
输出 #1
0 3 1 2 0 2 1 3 0 1 2 3 -1
In the first test, $(0\&3)+(1\&2) = 0$ .
In the second test, $(0\&2)+(1\&3) = 1$ .
In the third test, $(0\&1)+(2\&3) = 2$ .
In the fourth test, there is no solution.
In the second test, $(0\&2)+(1\&3) = 1$ .
In the third test, $(0\&1)+(2\&3) = 2$ .
In the fourth test, there is no solution.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted