1 / 2
Jan 2023

// I am geting TLE…please help
#include <bits/stdc++.h>
// #define int long long int
#define mod 1000000007
#define mia -1e18
#define maa 1e18
#define pii pair<int,int>
using namespace std;
int power(int a,int m,int p){
if(m==0){
return 1;
}
int ans=power(a,m/2,p)%p;
ans*=ans;
ans%=p;
return m%2==0?ans:(ans*a)%p;
}
#define k 10000
bool prime[k];
vector primes;
void sieve(){

fill(prime, prime+k, true);
for(int i = 3; i*i < k; i+=2){
	if(prime[i])
		for(int j = i*i; j < k; j += 2*i)
			prime[j] = false;
}
primes.push_back(2);
for(int i = 3; i < k; i += 2)
	if(prime[i])
		primes.push_back(i);

}
void get_all_factor(int n,map<int,int>&mp){
//call siev
for(auto &it:primes){
int y=it;
int cn=0;
if(y>n){
break;
}
while(n%y==0){
n/=y;
mp[y]++;
}

}
if(n>2){
    mp[n]++;
}

}
int main(){
sieve();
int tt;
cin>>tt;
int n;
int y;
map<int,int>mp;
vectorvv;
bool flag=true;
while(tt–)
{

            cin>>n;
            map<int,int>mp1;
            while(n--){
                cin>>y;
                get_all_factor(y,mp1);
            }
            if(flag==true){
                mp=mp1;
                flag=false;
            }
            else{
                for(auto &it:mp){
                mp[it.first]=min(it.second,mp1[it.first]);
            }
            }
            
           
            
}
int ans=1;
int y1;
for(auto it:mp){
    y1=power(it.first,it.second,mod);
    ans=(ans*y1)%mod;
}
cout<<ans%mod<<endl;

}

  • created

    Jan '23
  • last reply

    Feb '23
  • 1

    reply

  • 483

    views

  • 2

    users

21 days later

Yes, you will get a TLE.

I have to ask,- did you test the code with the input provided? The first number above the program reads is the number of test cases, then the number n.

From the problem: The first line of input consists of 2 ≤ N ≤ 10^6, the number of numbers for which Ada wants to find their gcd.

There is only one test case.