Joseph's Puzzle |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB |
Total submit users: 134, Accepted users: 125 |
Problem 10045 : No special judgement |
Problem description |
Persons, whose number order is from 1 to N, hold a password (the straight integer less than 10000) each person and sit around clockwise. At first we choose a positive integer factor as the count off number M and start to number clockwise from number one .As we number to M the person who numbers M leaves the rank and whose password will be the new number M. And then start to number again from the person who is sit clockwise next to the person who has been out off the rank just before then. Continue like this, until all people are out off the rank completely. Please design a program of leaving order. |
Input |
There have some groups data. First line of each group data is two integers N, M (0<N, M<100),the second line is N positive integer, separately express every person’s password.The last line contins 0,0 ,which means the end of input data. |
Output |
To each group of data, according to the order which leaves outputs their serial number. Between the numeral separates with a blank space. Each group of data monopolize line of outputs. |
Sample Input |
7 20 3 1 7 2 4 8 4 4 3 1 2 3 4 0 0 |
Sample Output |
6 1 4 7 2 3 5 3 2 1 4 |
Problem Source |
HNU 1'st Contest |
/*
18:10~
*/
#include < stdio.h >
#define MAX 50000
int move( int num[], int Mr );
main()
{
int num[MAX],n,Mright = 0 ,p = 0 ,total = 0 ,M = 0 ,Mr = 0 ;
int i,j;
i = 0 ;
for (i = 0 ; i < MAX ; i ++ )num[i] =- 2 ;
scanf( " %d%d " , & n, & M);
for (i = 0 ; i < n ; i ++ )scanf( " %d " , & num[i]);
while ( n != 0 && M != 0 ){
total = n;
p = 0 ;
Mr = M % n;
while ( total > 0 ){
p = move(num,Mr);
if (total == 1 ){printf( " %d " ,p + 1 ); break ;}
else printf( " %d " ,p + 1 );
M = num[p]; // record M
num[p] =- 1 ;
total -- ;
p ++ ; // delete it
M = M % total;
// caculate the amount after this
i = p;
Mright = 0 ;
while (num[i] !=- 2 ){
if (num[i] !=- 1 )Mright ++ ;
i ++ ;
}
if (M == 0 && total == 1 ){Mr = 1 ; continue ;}
if (M == 0 )Mr = total - Mright;
else if (M > Mright)Mr = M - Mright;
else Mr = total + M - Mright;
} // while total
for (i = 0 ; i < MAX ; i ++ )num[i] =- 2 ;
scanf( " %d%d " , & n, & M);
for (i = 0 ; i < n ; i ++ )scanf( " %d " , & num[i]);
}
return 0 ;
}
// return current p,M < total
int move( int num[], int Mr )
{
int c = 0 ,p = 0 ;
while ( 1 ){
if ( num[p] != - 1 ){
c ++ ;
if (c == Mr) return p;
}
p ++ ;
}
}
#include < stdio.h >
#define MAX 50000
int move( int num[], int Mr );
main()
{
int num[MAX],n,Mright = 0 ,p = 0 ,total = 0 ,M = 0 ,Mr = 0 ;
int i,j;
i = 0 ;
for (i = 0 ; i < MAX ; i ++ )num[i] =- 2 ;
scanf( " %d%d " , & n, & M);
for (i = 0 ; i < n ; i ++ )scanf( " %d " , & num[i]);
while ( n != 0 && M != 0 ){
total = n;
p = 0 ;
Mr = M % n;
while ( total > 0 ){
p = move(num,Mr);
if (total == 1 ){printf( " %d " ,p + 1 ); break ;}
else printf( " %d " ,p + 1 );
M = num[p]; // record M
num[p] =- 1 ;
total -- ;
p ++ ; // delete it
M = M % total;
// caculate the amount after this
i = p;
Mright = 0 ;
while (num[i] !=- 2 ){
if (num[i] !=- 1 )Mright ++ ;
i ++ ;
}
if (M == 0 && total == 1 ){Mr = 1 ; continue ;}
if (M == 0 )Mr = total - Mright;
else if (M > Mright)Mr = M - Mright;
else Mr = total + M - Mright;
} // while total
for (i = 0 ; i < MAX ; i ++ )num[i] =- 2 ;
scanf( " %d%d " , & n, & M);
for (i = 0 ; i < n ; i ++ )scanf( " %d " , & num[i]);
}
return 0 ;
}
// return current p,M < total
int move( int num[], int Mr )
{
int c = 0 ,p = 0 ;
while ( 1 ){
if ( num[p] != - 1 ){
c ++ ;
if (c == Mr) return p;
}
p ++ ;
}
}