A. Anti-knapsack
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two integers n and k. You are asked to choose maximum number of distinct integers from 1 to n so that there is no subset of chosen numbers with sum equal to k.
A subset of a set is a set that can be obtained from initial one by removing some (possibly all or none) elements of it.
Input
The first line contains the number of test cases T (1≤T≤100).
Each of the next T lines contains two integers n and k (1≤k≤n≤1000) — the description of test cases.
Output
For each test case output two lines. In the first line output a single integer m — the number of chosen integers.
In the second line output m distinct integers from 1 to n — the chosen numbers.
If there are multiple answers, print any. You can print the numbers in any order.
输入样例
3
3 2
5 3
1 1
输出样例
2
3 1
3
4 5 2
0
解题思路
选取最多数,使任意个数的所选数字之和不等于k
那么我们让最小的两个数相加都不等于k,且不存在单个数等于k
这里主要是对于k奇数和偶数的判定
不能简单地k/2
当k为奇数时,例如k=5,那么k/2=2,2+3=5
所以取奇数时我们要加一,我们可以用ceil函数直接搞定,这样连偶数的情况都不用考虑
(偶数的话k/2+(k+1)/2必定大于k;
AC代码
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int t;
cin>>t;
while(t--) {
int n,k;
cin>>n>>k;
int cx =ceil((double)k/2);
cout<<n-cx<<endl;
for(int i=cx; i<=n; i++) {
if(i==k)
continue;
cout<<i<<" ";
}
puts("");
}
}