The ones to remain

描述

There are N soldiers standing in one line. They are marked from 1 to N, from right to left. And they are given a number m. Then the soldiers numbered off, straight from the right-hand man. The one who reported a number that is the multiple of m was kept in the line. Others have to leave the line. They continue doing this till the number of people in the line is less than m. For example, if there are 10 soldiers, and m = 3. For the first time the soldiers who are marked 3, 6, 9 remain in the line. For the second time the soldier who is marked 9 remains in the line. Because the number of soldiers in the line is less than m, so the soldier marked 9 was the only one to remain in the line.

    Now we want to know who will be the ones to remain, can you tell us ?

输入

There are several test cases in the input. Each test cases is only one line, contains two integers n and m.(3 <= n <= 109, 2 <= m <= n). The input ends when n = 0 and m = 0.

输出

For each test case, output two lines. The first line contains one integer x, the number of soldiers to remain. The second line contains x integers, the numbers marked on the soldiers who remain in the line. You should output them in increasing order.

样例输入

10 3
8 3
0 0

样例输出

1
9
2
3 6


/*输出在n以内 m的最大值的后几个  不能认为约瑟夫问题 数量太大必爆必超时*/
#include <stdio.h>
int main()
{

    int
 a,b,i,j,k,c,p;
    while
(scanf("%d%d",&a,&b)&&a&&b)
    {

        c=a;j=0;
        while
(c>=b)
        {

            j++;
            c/=b;
        }
  //进行了j轮筛选  剩下c个人
        if(j==1 && c==1)
            printf("1\n%d\n",a/b*b);
        else

        {

            p=1;
            for
(k=1;k<=j;k++)
                p*=b;
            
            printf("%d\n",c);
            for
(i=1;i<c;i++)
                printf("%d ",p*i);
            printf("%d\n",c*p);
        }
    }

    return
 33;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值