1. 程式人生 > >CodeForces 1040B(思維題)

CodeForces 1040B(思維題)

this 代號 else cau ood rect num 思維 sample

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 iki−k, ik+1i−k+1, ..., i1i−1, i+1i+1, ..., i+k1i+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 22will 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 (1n10001≤n≤1000, 0k10000≤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 ll integers from 11 to nndenoting the number of the skewer that is to be turned over at the corresponding step.

Examples

Input
7 2
Output
2
1 6
Input
5 1
Output
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.

一開始沒看清題意,wa了幾次,看懂了題以後,就好做了。

題意:給你一排餅,用數字1~n表示他們的代號。每次給你兩個數字,第一個數字表示餅的數目(即n),第二個數字(即m)表示假設選擇翻起的餅是代號為a的話,那麽代號為[a-m,a+m]的餅都會跟著被翻起。

然後題目要你求的是:把那些餅全翻起來最少的次數,然後輸出那個次數和每一次翻起的位置。。

思路:這題給出的數據不大,所以不用什麽算法,直接暴力了。我的思路是:假設每次給出的兩個數字是n和m,翻一次可以翻起的最多的餅的數目為2m+1,所以就計算一個n%(2*m+1),如果n%(2*m+1)等於0,那就一個循環輸出答案就好了,如果n%(2*m+1)!=0,那就把n%(2*m+1)和m+1比一下(因為比m+1小的話,會翻起前面翻過的),若是比m+1大的話,最後翻的那個為n%(2*m+1)的一半那個,比m+1更小的話,就向前查找。然後再依次輸出答案就可以了。

註意:要註意m等於0的情況(被這個害苦了)。

代碼:#include<stdio.h>
#include<string.h>
int main()
{
 int n,m;
 while(scanf("%d%d",&n,&m)!=EOF)
 {
  if(n<=2*m+1)//判斷是否小於2*m+1,若小於直接輸出n/2+1 
  {
   int a=1;
   printf("%d\n",a);
   printf("%d\n",n/2+1);
   continue;
  }
  else if(m==0)//考慮m等於0的情況 
  {
     printf("%d\n",n);
     for(int i=1;i<=n;i++)
     printf("%d ",i);//若m等於0直接循環輸出 
     continue; 
  }
  else
  {
   int a=n%(2*m+1);
   int b=n/(2*m+1);
   int d=m+1;
   if(a==0)
   {
    printf("%d\n",b);
    for(int i=1;i<=b;i++)
    {
     printf("%d ",d);//若a==0循環輸出d 
     d+=2*m+1;
    }
   }
    else if(a<m+1&&a!=0)
   {
    for(int i=1;i<=m+1;i++)//向前找 
    {
     d--;
     a++;//更新a的值 
     if(a==m+1)//a==m+1的時候就滿足翻最後一次的時候不會把前面翻過了的反過來 
     break;
    }
    printf("%d\n",b+1);
    for(int i=1;i<=b;i++)
    {
     printf("%d ",d);//循環輸出 
     d+=2*m+1;
    }
    printf("%d\n",n);
   }
   else//a>=m+1的情況 
   {
    printf("%d\n",b+1);
    for(int i=1;i<=b;i++)
    {
     printf("%d ",d);
     d+=2*m+1;
    }
    printf("%d\n",b*(2*m+1)+m+1);//保證不會把前面翻過了的翻過來 
   }
  }
 }
 return 0;
 }

學無止境,自己還是個菜雞。努力吧,少年!

CodeForces 1040B(思維題)