The Confused Monk

The Monk wants to teach all its disciples a lesson about patience since they are always in a hurry to do something crazy. To teach them this, he gives them a list of N numbers, which may or may not be distinct. The students are supposed to solve a simple Mathematical equation based on the array of these N numbers.

1.g(x) - GCD (a[ 0 ], a[ 1 ], a[ 2 ]... a[n-1] )

2.f(x) - (a[ 0 ] * a[ 1 ] * a[ 2 ]... * a[n-1] )

The value of the MonkQuotient is: 109 + 7.

The equation to be solved is: ( f(x)g(x) ) %MonkQuotient

Example:

Input:   n = 2, a = [2,6]
Output: 144

Approach

C++

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007

long long power(long long along long b)
{
    if (b == 0)
        return 1;
    if (b % 2 == 0)
        return power((a * a) % MODb / 2);
    return (a * power((a * a) % MOD, (b - 1) / 2)) % MOD;
}
long long product(long long a[], long long n)
{
    long long res = 1;
    for (long long i = 0i < ni++)
        res = (res * a[i]) % MOD;
    return res;
}
long long gcd(long long along long b)
{
    if (b == 0)
        return a;
    return gcd(ba % b);
}
long long FinalCGD(long long a[], long long n)
{
    long long result = a[0];
    for (long long i = 1i < ni++)
        result = gcd(a[i], result);
    return result;
}
int main()
{
    long long n = 2;

    long long a[n] = {26};

    long long x = FinalCGD(an);
    long long y = product(an);

    cout << power(yx);

    return 0;
}


Read Interview Questions

Exception Handling Interview Questions

DBMS Interview Questions Set -1

DBMS Interview Questions Set -2

SQL Interview Question Set -1

SQL Interview Question Set -2

JPA Interview Questions Set -1

JPA Interview Question Set -2

Hibernate Interview Questions

Spring Boot Interview Questions Set 1

Spring Boot Interview Questions Set 2


No comments:

Post a Comment