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
- Read entries with one running 1-based position.
- 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.
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';
}