WAMU-LTC2016

PARHI AND MULTIPLICATION– EDITORIAL


Author: bigo_admin

Tester and Editorialist: subhasis10

Problem Link:- Contest
and Practice

DIFFICULTY: CAKEWALK

PREREQUISITES: Modulo operation

PROBLEM:

Given an array of integers [A1,A2,…An], we have to multiply them and print the result.

EXPLANATION:

Multiplication of N integers is a trivial operation and can be performed easily. But since each element again is of larger size a Long variable of C/C++ will be unable to allocate it. Hence the trick is to reduce the range by using the MOD operator. Since the property of the mod operator keeps the result in the range of 0-(MOD-1) we use this property to reduce the large number in each step of multiplication.

CODE:

#include 
#define ll long long int
ll a[101],ans,i,n;
#define M 1000000007
int main()
{
int t;
scanf("%d",&t);
while(t-- )
{
scanf("%d",&n);
ans=1;
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
a[i]=a[i]%M;
ans=ans*a[i];
ans=ans%M;
}
ans=ans%M;
printf("%lld\n",ans);
}
return 0;
}