Cards
InterviewTime limit1sMemory limit128 MB
Simulate a card-sorting game to find which numbers remain consistent with a sequence of column answers after repeated row/column reshuffling.
- Level
Medium5 of 10
- Topics
- Simulation, Math
- Solved
- No attempts yet
Problem
Dave and Hal play a game with cards. Dave has cards, where is a multiple of three (), and the cards are numbered from to .
The same number is printed on both sides of a card, no two cards share a number, and the cards start sorted in increasing order.
First, Hal secretly picks one number from the set .
Then Dave deals all the cards face up into a grid of rows and columns. He fills the grid row by row: card , then card , then card go into the first row from left to right, the next three cards fill the second row, and so on until the last card completes the last row.
Hal then tells Dave which column (the first, second, or third) currently holds the card with his secret number.
Dave gathers the cards column by column: he collects the entire first column from top to bottom (row , row , ..., row ), then the second column the same way, and finally the third column. Without shuffling, he deals this pile back onto the table exactly as before, row by row.
This repeats. Every time Dave finishes dealing, Hal again names the column that contains his card. After all of Hal's answers, several numbers may still be consistent with everything he said.
Write a program that uses Hal's answers to determine the smallest set of numbers that are still candidates for Hal's secret number.
Input
The first line contains an integer (, and is divisible by ), the number of cards.
The second line contains an integer (), the number of deals, that is, the number of Hal's answers.
Each of the next lines contains one of the words first, second, or third - Hal's answer for that deal, given in order.
Output
Print, on a single line, every number that is still a candidate for Hal's secret number, in increasing order, separated by single spaces.