A. Anti-knapsack Codeforces Round #705 (Div. 2)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two integers nn and kk. You are asked to choose maximum number of distinct integers from 11 to nn so that there is no subset of chosen numbers with sum equal to kk.
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 TT (1≤T≤1001≤T≤100).
Each of the next TT lines contains two integers nn and kk (1≤k≤n≤10001≤k≤n≤1000) — the description of test cases.
Output
For each test case output two lines. In the first line output a single integer mm — the number of chosen integers.
In the second line output mm distinct integers from 11 to nn — the chosen numbers.
If there are multiple answers, print any. You can print the numbers in any order.
Example
input
Copy
3
3 2
5 3
1 1
output
Copy
2
3 1
3
4 5 2
0
题目大意:从1到n中找出一个集合,该集合的子集相加不能等于k。
解题思路:大于k必定符合题意,取到的数必达于k,小于k的取到(k + 1) / 2 ,(当k == 6时,小于6的不能取2,因为2 + 4等于6).
AC代码:
#include <iostream>
#include <queue>
#include <map>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f;
int main() {
int t;
cin >> t;
while (t--) {
int s, e;
cin >> s >> e;
int cnt = 0;
vector<int>a;
for (int i = e+1; i <= s; i++) {
a.push_back(i);
cnt++;
}
for (int i = e - 1; i >= (e + 1) / 2; i--){
a.push_back(i);
cnt++;
}
//if (e == 2) {a.push_back(1);cnt++;}
cout << cnt << '\n';
if (cnt!=0){
cout << a[0];
for (int i = 1; i < a.size();i++) {
cout << " " << a[i];
}
cout << "\n";
}
}
return 0;
}