当前位置: 首页 > 工具软件 > shashlik > 使用案例 >

B. Shashlik Cooking(数学好题)

范甫
2023-12-01

http://codeforces.com/problemset/problem/1040/B

time limit per test

1 second

memory limit per test

512 megabytes

input

standard input

output

standard output

Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.

This time Miroslav laid out nn skewers parallel to each other, and enumerated them with consecutive integers from 11 to nn in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number ii, it leads to turning kk closest skewers from each side of the skewer ii, that is, skewers number i−ki−k, i−k+1i−k+1, ..., i−1i−1, i+1i+1, ..., i+k−1i+k−1, i+ki+k (if they exist).

For example, let n=6n=6 and k=1k=1. When Miroslav turns skewer number 33, then skewers with numbers 22, 33, and 44 will come up turned over. If after that he turns skewer number 11, then skewers number 11, 33, and 44 will be turned over, while skewer number 22 will be in the initial position (because it is turned again).

As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all nn skewers with the minimal possible number of actions. For example, for the above example n=6n=6 and k=1k=1, two turnings are sufficient: he can turn over skewers number 22 and 55.

Help Miroslav turn over all nn skewers.

Input

The first line contains two integers nn and kk (1≤n≤10001≤n≤1000, 0≤k≤10000≤k≤1000) — the number of skewers and the number of skewers from each side that are turned in one step.

Output

The first line should contain integer ll — the minimum number of actions needed by Miroslav to turn over all nn skewers. After than print llintegers from 11 to nn denoting the number of the skewer that is to be turned over at the corresponding step.

Examples

input

Copy

7 2

output

Copy

2
1 6 

input

Copy

5 1

output

Copy

2
1 4 

Note

In the first example the first operation turns over skewers 11, 22 and 33, the second operation turns over skewers 44, 55, 66 and 77.

In the second example it is also correct to turn over skewers 22 and 55, but turning skewers 22 and 44, or 11 and 55 are incorrect solutions because the skewer 33 is in the initial state after these operations.

题意:给出n,k,代表着n个东东(用饼代替)每次可选择一个位置i,那么i-k,i,i+k范围的饼都会被翻转,问选最少的点,是所有的饼都翻转一次,且不能有重复的翻。

做法:假设第一个点放在1的位置第二个点放在n位置,那么剩余1+k+1到n-k-1区间内的位置没有翻转。只要计算这个区间用2*k+1的范围覆盖时剩余多少没有覆盖到,剩余的数放在两边,即1的点往右移或n的点往左移。

 

计算剩余的数:int yushu=(n-2*(k+1))%m;

若yushu小于等于k,1的点往右移动yushu个位置,然后每隔2*k+1放一个点即可。

若yushu大于k,直接将1的点向右移动k个位置,然后每隔2*k+1放一个点。剩余的yushu-k自动的就放在了最右边点的右边。

 

由于yushu=(n-2*(k+1))%m;公式中有n-2*(k+1),那么就需要判断n是否大于2*(k+1)。小于的话这个公式就无效了,通过简单观察得知,n小于2*(k+1)时只需要放一个点在最中间就可以了。

#include<bits/stdc++.h>
using namespace std;
vector<int>ans;
int main()
{
	int n,k;
	cin>>n>>k;
	if(k==0)
	{
		printf("%d\n",n);
		for(int i=1;i<=n;i++) printf("%d ",i);
		return 0;
	}
	if(n<2*(k+1))
	{
		printf("1\n");
		int nu=n/2;
		if(n&1) nu++;
		printf("%d\n",nu);
		return 0;
	}
	int m=2*k+1;
	int yushu=(n-2*(k+1))%m;
	//printf("yushu:%d\n",yushu);
	if(yushu<=k)
	{
		for(int i=1+yushu;i<=n;i+=m)
		{
			ans.push_back(i);
		}
		printf("%d\n",ans.size());
		for(int i=0;i<ans.size();++i) printf("%d ",ans[i]);
	}
	else
	{
		yushu=k;
		for(int i=1+yushu;i<=n;i+=m)
		{
			ans.push_back(i);
		}
		printf("%d\n",ans.size());
		for(int i=0;i<ans.size();++i) printf("%d ",ans[i]);
	}
}
/*

*/

 

 类似资料: