Uva 10489 - Boxes of Chocolates
// Be Careful about Big data and data type. :)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
int t;
cin >> t;
while(t--)
{
int frnd,box,k,i;
int ans=0, temp;
cin >> frnd >> box;
while(box--)
{
cin >> k;
temp=1;
for(i=0;i<k;i++)
{
int x;
cin >> x;
temp = (temp % frnd) * (x % frnd) % frnd; // Modular Arithmetic
}
//cout << "temp = " << temp << endl;
ans = (ans + temp) % frnd;
//cout << "ans = " << ans << endl;
}
cout << ans << endl;
}
return 0;
}
why use Modular Arithmetic??
ReplyDeleteI have just used a formula of modular arithmetic which is known as:
Delete(a*b)%m = ((a%m)*(b%m))%m
why?
cause, this is the formula :P