Codeforces Round #814:B Mathematical Circus

蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!

Hello 大家好!本蒟蒻小学生来发题解啦,快来看看今天的题目!

=========================================================================

题目


原文:

B. Mathematical Circus

A new entertainment has appeared in Buryatia — a mathematical circus! The magician shows two numbers to the audience — nn and kk, where nn is even. Next, he takes all the integers from 11 to nn, 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.

Input

The first line contains one integer tt (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 nn and kk (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 nn over all test cases does not exceed 2⋅105.

Output

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 \frac{n}{2} lines output pairs of the split, in each line print 2 numbers — first the integer a, then the integer b. 

难度:

题目翻译:

本文的意思就是给一个n和k,从1-n把这些整数分成一对一对的,每一对(a,b),问能否保证(a+k)%b==0,若不行则输出No, 否则输出Yes,并且输出每一组的a和b。

思路


这道题一看就是一道数学题,所以我们要找规律!

首先,我们来看n=2的时候:

nkYes/No组成的对数
21Yes(1,2)
22Yes(2,1)
23Yes(1,2)
24No
25Yes(1,2)
26Yes(2,1)
27Yes(1,2)
28No
29Yes(1,2)

发现了什么?当k%4==0时,就是No。那组成的对有什么规律呢?现在能看出来的只有k%4=1和k%4=3的时候相等,并且k%4=1时,对都相等(2,3也一样)!那还有什么规律呢?我们接着往下看!

nkYes/No组成的对数
41Yes(1,2)(3,4)
42Yes(2,1)(3,4)
43Yes(1,2)(3,4)
61Yes(1,2)(3,4)(5,6)
62Yes(2,1)(3,4)(6,5)
63Yes(2,1)(3,4)(5,6)
81Yes(1,2)(3,4)(5,6)(7,8)
82Yes(2,1)(3,4)(6,5)(7,8)
83Yes(1,2)(3,4)(5,6)(7,8)

发现了么?k%4==1和k%4==3的时候,直接往后一个一个罗就行:(1,2)\rightarrow (1,2)(3,4)\rightarrow (1,2)(3,4)(5,6)\rightarrow (1,2)(3,4)(5,6)(7,8)……而k%4==2的时候,当n%4!=0的时候那么就到这往后罗,而n%4==0的时候,就正着往后罗就可以了!(2,1)\rightarrow (2,1)(3,4)\rightarrow (2,1)(3,4)(6,5)\rightarrow (2,1)(3,4)(6,5)(7,8)

                                                         

倒着             正着                      倒着                             正着

代码


#include<bits/stdc++.h>
using namespace std;
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int t,n,k;
	cin>>t;
	for (int i=0;i<t;i++){
		cin>>n>>k;
		if (k%4==0) cout<<"NO"<<endl;
		else {
			cout<<"YES"<<endl;
			if (k%4==1) {//如果等于1,直接正着往后罗
				for (int j=1;j<=n;j++) {
					cout<<j<<" ";
					if (j%2==0) cout<<endl;
				}
			}
			else if (k%4==2){//等于2
				for (int j=2;j<=n;j+=2){
					if (j%4!=0) cout<<j<<" "<<j-1<<endl;//如果不等于0,倒着
					else cout<<j-1<<" "<<j<<endl;//否则,正着!
				}
			}
			else {//等于3,也正着往后罗就行
				for (int j=1;j<=n;j++){
					cout<<j<<" ";
					if (j%2==0) cout<<endl;
				}
			}
		}
	}
	return 0;
}

今天的题解到这里了,喜欢的点个赞谢谢!

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值