Problem - B - Codeforces
codeforces.com
- 제목
Mathematical Circus
- 조건
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
- 문제
A new entertainment has appeared in Buryatia — a mathematical circus! The magician shows two numbers to the audience — n and k, where n is even. Next, he takes all the integers from 1 to n, and splits them all into pairs (a,b) (each integer must be in exactly one pair) so that for each pair the integer (a+k)⋅b is divisible by 4 (note that the order of the numbers in the pair matters), or reports that, unfortunately for viewers, such a split is impossible.
Burenka really likes such performances, so she asked her friend Tonya to be a magician, and also gave him the numbers n and k.
Tonya is a wolf, and as you know, wolves do not perform in the circus, even in a mathematical one. Therefore, he asks you to help him. Let him know if a suitable splitting into pairs is possible, and if possible, then tell it.
- 입력
The first line contains one integer t (1≤t≤104) — the number of test cases. The following is a description of the input data sets.
The single line of each test case contains two integers n and k (2≤n≤2⋅105, 0≤k≤109, n is even) — the number of integers and the number being added k.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
- 출력
For each test case, first output the string "YES" if there is a split into pairs, and "NO" if there is none.
If there is a split, then in the following n2 lines output pairs of the split, in each line print 2 numbers — first the integer a, then the integer b.
예제 입력1 | 예제 출력1 |
4 4 1 2 0 12 10 14 11 |
YES 1 2 3 4 NO YES 3 4 7 8 11 12 2 1 6 5 10 9 YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc, N, K;
cin >> tc;
for(int n = 0 ; n < tc ; n++){
cin >> N >> K;
if(K % 2 == 1){
cout << "YES" << endl;
for(int n = 2 ; n <= N ; n += 2){
cout << n - 1 << ' ' << n << endl;
}
}
else{ // K % 2 == 0
if(K % 4 == 0){
cout << "NO" << endl;
}
else{ // K % 4 != 0
cout << "YES" << endl;
for(int n = 2 ; n <= N ; n += 2){
if(n % 4 != 0) cout << n << ' ' << n - 1 << endl;
else cout << n - 1 << ' ' << n << endl;
}
}
}
}
return 0;
}