Recover a Cinema Seat’s Row and Column
National Olympiad — Municipal round (2014 season)
Submission name: cinema.cpp
Problem summary
Seats in N rows of M seats are renumbered consecutively, row by row from left to right. Recover the original 1-based row and seat number from a new seat number.
Input
N, M and the new seat number.
Output
Its 1-based row and position within that row.
Constraints
1 ≤ N ≤ 10; 1 ≤ M ≤ 20; the seat number is valid.
Worked example
Input and output for the worked example discussed below.
Input
10 20 9
Output
1 9
Solution analysis
- Subtract one from the new number to convert it to a zero-based offset. Every complete block of M offsets corresponds to one earlier row.
- Integer division offset/M gives the zero-based row. The remainder offset%M gives the zero-based seat within that row. Add one to both results before printing.
- Seat 9 in rows of 20 has offset 8: quotient 0 and remainder 8, so row 1, seat 9. Seat 20 gives row 1, seat 20 rather than an invalid column zero because the subtraction is performed first.
Why it works
Euclidean division writes offset=qM+r with 0≤r<M. Exactly q full rows precede the seat, and r seats precede it within its row, proving the two converted indices.
Watch out. Subtract one before division, not after. Input N validates the overall capacity but M controls both formulas. Both returned indices are 1-based.
Commented C++17 solution
An independently written complete program with standard headers.
#include<algorithm>
#include<array>
#include<cmath>
#include<iostream>
#include<limits>
#include<map>
#include<numeric>
#include<set>
#include<string>
#include<vector>
using namespace std;
int main(){
int rows;
int columns;
int seat;
cin>>rows>>columns>>seat;
// Zero-based offsets avoid the exact-multiple boundary problem.
int offset=seat-1;
cout<<offset/columns+1<<' '<<offset%columns+1<<'\n';
}