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

Comments

  1. Replies
    1. I have just used a formula of modular arithmetic which is known as:
      (a*b)%m = ((a%m)*(b%m))%m
      why?
      cause, this is the formula :P

      Delete

Post a Comment

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number