Go Back Home
Difficulty: Hard

Corporal Klinger is a member of the 4077th Mobile Army Surgical 
Hospital in the Korean War; and he will do just about anything to get 
out.  The U.S. Army has made an offer for a lottery that will choose 
some number of lucky people (X) to return to the states for a 
recruiting tour.  Klinger needs your help getting out. 
 
The lottery is run by lining up all the members of the unit at 
attention and eliminating members by counting off the members from 1 to 
N where N is a number chosen by pulling cards off of the top of a deck.  
Every time N is reached, that person falls out of the line, and 
counting begins again at 1 with the next person in line.  When the end 
of the line has been reached (with whatever number that may be), the 
next card on the top of the deck will be taken, and counting starts 
again at 1 with the first person in the remaining line.  The last X 
people in line get to go home. 
 
Klinger has found a way to trade a stacked deck with the real deck just 
before the selection process begins.  However, he will not know how 
many people will show up for the selection until the last minute.  Your 
job is to write a program that will use the deck Klinger supplies and 
the number of people in line that he counts just before the selection 
process begins and tell him what position(s) in the line to get in to 
assure himself of a trip home.  You are assured that Klinger's deck 
will get the job done by the time the 20th card is used. 
 
A simple example with 10 people, 2 lucky spots, and the numbers from 
cards 3, 5, 4, 3, 2 would show that Klinger should get in positions 1 
or 8 to go home. 

**Input Format:** 
For each selection, you will be given a line of 22 integers.  The first 
integer (1 <= N <= 50) tells how many people will participate in the 
lottery.  The second integer (1 <= X <= N) is how many lucky "home" 
positions will be selected.  The next 20 integers are the values of the 
first 20 cards in the deck.  Card values are interpretted to integer 
values between 1 and 11 inclusive.
_'0' in a new line marks the end of input._
 
**Output Format:**
For each input line, you are to print the message "Selection #A" on a 
line by itself where A is the number of the selection starting with 1 
at the top of the input file.  The next line will contain a list of 
"lucky" positions that Klinger should attempt to get into.  *The list of 
"lucky" positions is then followed by a blank line.*


**Sample Input:**

	47 6 11 2 7 3 4 8 5 10 7 8 3 7 4 2 3 9 10 2 5 3    
	0
 
**Sample Output:** 
    
	Selection #1   
	1 3 16 23 31 47
    
	    

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
    #include <stdio.h>

    int cards[20], participants, lucky, sim_count;
    FILE *inp, *outp;
	
    void find_positions(void)
    { int positions[50], i, j, k, num_left;

	for (i=0; i<50; i++)
	{ positions[i] = 1; }

	for (i = 0, num_left = participants; num_left > lucky; i++)
	{ k = 0;
		for (j = 0; (j < participants) && num_left > lucky; j++)
		{ if (positions[j])
			{ if (++k == cards[i])
				{ num_left--;
					k = 0;
					positions[j] = 0;
				}
			}
		}
	}

	fprintf(outp, "Selection #%1d\n", ++sim_count);
	for (i = 0; i<participants; i++)
	{ if (positions[i])
		fprintf(outp, "%1d ", i+1);
	}
      fprintf(outp, "\n");
    }

    void main(void)
    { int i;

	inp  = fopen("mash.in",  "r");
	outp = fopen("mash.out", "w");
	sim_count = 0;

	while (!feof(inp))
	{ fscanf(inp, "%d %d", &participants, &lucky);
		for (i=0; i<20; i++)
		{ fscanf(inp, "%d", &cards[i]); }
		fscanf(inp, "\n");

		find_positions();
	}
	fclose(inp);
	fclose(outp);
    }