CF1630A.And Matching

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a set of nn (nn is always a power of 22) elements containing all integers 0,1,2,…,n−10, 1, 2, \ldots, n-1 exactly once.

Find n2\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 of its elements must be exactly equal to kk. Formally, if aia_i and bib_i are the elements of the ii-th pair, then the following must hold: $$\sum_{i=1}^{n/2}{a_i \& b_i} = k,$$ where &\& denotes the bitwise AND operation.

If there are many solutions, print any of them, if there is no solution, print −1-1 instead.

给你一个包含 nn 个元素(nn 恒为 22 的幂)的集合,其中恰好包含整数 0,1,2,…,n−10, 1, 2, \ldots, n-1 各一次。

请找出 n2\frac{n}{2} 对元素,使得:

  • 集合中的每个元素恰好出现在一对中;
  • 所有对的按位与值之和恰好等于 kk。形式化地,若第 ii 对的两个元素为 aia_i 和 bib_i,则需满足:

∑i=1n/2ai&bi=k,\sum_{i=1}^{n/2}{a_i \& b_i} = k,

其中 &\& 表示按位与运算。

若存在多个解,输出任意一个即可;若无解,则输出 −1-1。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤4001 \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 nn and kk (4≤n≤2164 \leq n \leq 2^{16}, nn is a power of 22, 0≤k≤n−10 \leq k \leq n-1).

The sum of nn over all test cases does not exceed 2162^{16}. All test cases in each individual input will be pairwise different.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤4001 \leq t \leq 400),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例由一行组成,包含两个整数 nn 和 kk(4≤n≤2164 \leq n \leq 2^{16},且 nn 是 22 的幂,0≤k≤n−10 \leq k \leq n-1)。

所有测试用例中 nn 的总和不超过 2162^{16}。每个单独输入中的所有测试用例两两不同。

输出格式

For each test case, if there is no solution, print a single line with the integer −1-1.

Otherwise, print n2\frac{n}{2} lines, the ii-th of them must contain aia_i and bib_i, the elements in the ii-th pair.

If there are many solutions, print any of them. Print the pairs and the elements in the pairs in any order.

对于每个测试用例,若无解,则输出一行整数 −1-1。

否则,输出 n2\frac{n}{2} 行,其中第 ii 行必须包含第 ii 个数对中的两个元素 aia_i 和 bib_i。

若存在多个解,输出任意一个即可。数对之间以及每个数对内的元素顺序均不限。

输入输出样例

  • 输入#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(0\&3)+(1\&2) = 0.

In the second test, (0&2)+(1&3)=1(0\&2)+(1\&3) = 1.

In the third test, (0&1)+(2&3)=2(0\&1)+(2\&3) = 2.

In the fourth test, there is no solution.

在第一个测试中,(0&3)+(1&2)=0(0\&3)+(1\&2) = 0。

在第二个测试中,(0&2)+(1&3)=1(0\&2)+(1\&3) = 1。

在第三个测试中,(0&1)+(2&3)=2(0\&1)+(2\&3) = 2。

在第四个测试中,无解。

输入解题思路,AI测评打分。不知道怎么写?

首页