5 / 5
Sep 2021


I got wrong Answer in Test-Case 15.Can someone please tell me where i am getting it wrong ?

#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007

using namespace std;

int main()
{

ll n;
cin>>n;

ll gcd = 0;

while(n--)
{
	ll m;
	cin >> m;

	ll num = 1;

	vector <ll> getn(m);
	
	for(int i = 0 ;i < m ;i++)
	{
		cin>>getn[i];
		num = ( 1LL * (num % mod) * (getn[i] % mod) ) % mod;
	}

	gcd = __gcd(gcd , num) % mod;

}

cout<<gcd;
return 0;

}

  • created

    Sep '21
  • last reply

    Sep '21
  • 4

    replies

  • 657

    views

  • 2

    users

  • 1

    link

Try this:

2
3 9999883 9999937 9999907
3 9999901 9999973 9999907

Expected answer 9999907, your answer 1

Ok i got that due to the test cases like that my code doesn’t work but i am not getting why ?Can you please tell the reason why this code doesn’t work rather than just test cases.

I’m no mathematician, but I can only assume the logic is wrong. I used a simple Python program to compare the answers from a simple GCD of two integers, and simulating what the code does.

import math
mod = 1000000007
print(math.gcd(9999883 * 9999937 * 9999907, 9999901 * 9999973 * 9999907) % mod)

num1 = 1
num1 = ((num1 % mod) * (9999883 % mod)) % mod;
num1 = ((num1 % mod) * (9999937 % mod)) % mod;
num1 = ((num1 % mod) * (9999907 % mod)) % mod;
num2 = 1
num2 = ((num2 % mod) * (9999901 % mod)) % mod;
num2 = ((num2 % mod) * (9999973 % mod)) % mod;
num2 = ((num2 % mod) * (9999907 % mod)) % mod;
print(math.gcd(num1, num2))

I had a thought. Try this, and follow it through your code.

2
2 2 500000004
1 24