ECMAR20E - Editorial

PROBLEM LINK:

Practice
Contest

Author: Sunita Sen
Tester: Arnab Chanda
Editorialist: Sunita Sen

DIFFICULTY:

SIMPLE

PREREQUISITES:

None

PROBLEM:

Given an array of N integers and an integer value K, find the sum of remainders when each element of A is divided by K.

EXPLANATION:

Iterate through the given array, and for each element find the remainder when the element is divided by K.
The remainder can be calculated using the MOD(%) operator.
Lastly, add all the remainders and print it.

sum = \sum_{i=1}^{N} (A_i \% K)

SOLUTIONS:

Cpp Solution
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define dfor(i,a,b) for(ll i=(a);i>=(b);i--)
#define mem(u, vis) memset(u, vis, sizeof(u))
#define ll long long
#define ull unsigned long long
#define MOD  1000000007
#define MAX  1e9
#define pb   push_back
#define F    first
#define S    second


int main() {
	int t=1;
	cin>>t;
	while(t--){
		ll n,k;
		cin>>n>>k;
		ll a[n],sum=0;
		for(int i=0;i<n;++i){
			cin>>a[i];
			sum += a[i]%k;
		}
		cout<<sum<<endl;
	}
	
}
3 Likes