NEfu 639

发布于:2021-10-23 15:45:43

http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=639



description

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 ?

input


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

output


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.

sample_input


10 3
8 3
0 0

sample_output


1
9
2
3 6




#include
#include
#include
#include
using namespace std;
int cmp(int a,int b)
{
return a>b;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
if(m==0&&n==0)
break;
int k=n;
int x=0;
int t=1;
for(;;)
{
k=k/m;
x++;
if(k break;
}
//printf("(%d)
",x);
for(int i=0;i t=t*m;
printf("%d
",n/t);
for(int i=1;i<=n/t;i++)
printf(i==n/t?"%d
":"%d ",t*i);
}
return 0;
}





相关推荐

最新更新

猜你喜欢