1 / 2
Feb 2020

I am getting the corrrect answer but time limit is getting exceeded …please help
Program
#include

using namespace std;

int main()
{
int t,a,b,c;
cin>>t;
for(int j=0;j<t;j++)
{
cin>>a>>b>>c;
int ab=min(a,b);
int gcd=1;
for(int i=1;i<=ab;i++)
{
if(a%i==0 && b%i==0)
gcd=max(gcd,i);
}
if(c%gcd==0)
{
cout<<“Case “<<j+1<<”: Yes”<<endl;
}
else
{
cout<<“Case “<<j+1<<”: No”<<endl;
}
}
return 0;
}

  • created

    Feb '20
  • last reply

    Feb '20
  • 1

    reply

  • 595

    views

  • 2

    users

You need a better way to find the GCD. There’s a well-known algorithm that would help you here.

Suggested Topics

Want to read more? Browse other topics in Online Judge System or view latest topics.