cppAcademy
FROM LEARNING TO COMPETING

Real problems.
Your next challenge.

Explore the Group C Olympiad archive.
Choose a topic. Find a challenge. Build your solution.

213 tasks across the Municipal, Regional, and National rounds (2009–2026), plus 4 previously featured problems. 146 tasks have independently written English summaries, analyses, and C++17 solutions; the remaining entries link to Arena. Topic tags are from Arena or our reviewed explanations. Difficulty labels are learning estimates, not official ratings.
GROUP C2011 · EasyMatrix Traversal

Numbered Table

National Olympiad — Municipal round

Submission name: table.cpp

Problem summary

Number an M-row, N-column table from 1 to M·N in row-major order. Sum entries whose cell number is divisible by K.

Input

M N K, followed by M rows of N integers.

Output

The requested sum.

Constraints

1 ≤ M,N ≤ 100; 2 ≤ K ≤ M·N; entries lie in [−100,100].

Worked example

An independently prepared example for this explanation.

Input

2 4 3
1 -2 3 4
5 6 7 8

Output

9

Solution analysis

  1. Read entries with one running 1-based position.
  2. Add the value if position modulo K is zero; storing the table is unnecessary.

Why it works

Row-major input order equals the prescribed numbering, so exactly the required entries are included.

Complexity: O(MN) time and O(1) extra memory.

Watch out. Cell numbering starts at 1, not 0; negative values must be included.

Commented C++17 solution

An independently written complete program with standard headers.

// Key idea: Row-major input order equals the prescribed numbering, so exactly the required entries are
// included.
#include<iostream>
using namespace std;
int main(){
    int m,n,k;
    cin>>m>>n>>k;
    int sum=0;
    for(int pos=1;pos<=m*n;pos++){
        int x;
        cin>>x;
        if(pos%k==0){
            sum+=x;
        }
    }
    cout<<sum<<'\n';
}

Source: Arena Olimpiici ↗