cppAcademy
FROM LEARNING TO COMPETING

Intermediate.
Put your skills to the test.

Bulgarian Informatics Group D · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group D also includes challenging national-level problems. The six 2017 National tasks shared by Groups C and D use their existing permanent URLs. Years follow Arena’s competition-season labels. Source clarifications and special grader requirements are noted on affected tasks.
GROUP D2014 · EasyInteger indexing

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

  1. 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.
  2. 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.
  3. 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.

Complexity: O(1) time and O(1) space.

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';
}

Source: Arena Olimpiici ↗