Great Arjit and Numbers

Arjit is a great coder. He is one of the best all in the world. He can solve almost any question.

Arjit's heart falls for a girl Milinda. She came to Arjit with a question.
Given an array Arr, where:
Arr = {X, A[0] * X, A[0] * A[1] * X, A[0] * A[1] * A[2] * X, ... }
where A[i] denotes a number between 1 and 106.

We all know about the notation:

enter image description here

Milinda asks Arjit to find the value of Q.

enter image description here
enter image description here

Arjit is unable to concentrate on the question due to Milinda. Please help him solve this problem.

Example:

Input:  n = 3, a = [1,2,4]
Output: 21

Approach

C++

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

#define MOD 1000000007
long long modInverse(long long clong long m)
{
    long long m0 = m;
    long long y = 0x = 1;

    if (m == 1)
        return 0;

    while (c > 1)
    {
        long long q = c / m;
        long long t = m;
        m = c % m;
        c = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
        x += m0;

    return x;
}
int main()
{
    long long n = 3;

    long long arr[n] = {124};

    long long x = arr[0];
    long long brr[n];
    brr[0] = 1;
    long long ans = 0;
    for (long long i = 1i < ni++)
    {
        long long x = (arr[i] *
 modInverse(arr[i - 1], MOD)) % MOD;
        ans = (ans + ((x % MOD * (x + 1) % MOD) *
  modInverse(2MOD))) % MOD;
    }
    ans = ((ans % MOD * (ans + 1) % MOD) % MOD * 
modInverse(2MOD)) % MOD;
    cout << ans << "\n";

    return 0;
}


No comments:

Post a Comment