DIVEO....my code is giving TLE can someone plz explain why?

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

vector v;

signed main()
{
int t,vec[] = {2,3,5,7,11,13,17,19,23,29},i;
cin>>t;

for(i=0;i<10;i++)
		v.push_back(vec[i]);
		
while(t--)
{
	int n,a,b,pt=0;
	cin>>n>>a>>b;
	
	if(a<=0&&n%2==0)
			{
				pt+=a;
				n=1;
			}
	
		while(n%2==0)
		{
			if(a>0)
			pt+=a;
			n/=2;
		}
		
	if(b<=0&&n!=1)
	{
		pt+=b;
		n=1;
	}
	else
	
		{
			while(n!=1)
		{
			for(i=0;i<10;i++)
			{
				if(n%v[i]==0)
				{
					pt+=b;
					n/=v[i];
					break;
				}
			}
		}
		}
	
	cout<<pt<<endl;
}
return 0;

}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

1 Like

Thanks for the suggestion…Actually i am new here so i didn’t knew that :sweat_smile:
here is the link
https://www.codechef.com/viewsolution/52386929

1 Like

Thanks! Consider the test input:

1
31 1 1
1 Like

Whoa…your reply was fast !
Appreciate the help man , i understood what’s wrong with my code :grinning_face_with_smiling_eyes:
btw did u used any software or any other sort of method to find this test case or u just get it by looking at it ?

1 Like

This time around, I got the testcase just by looking at it :slight_smile:

With lots of other problems, though, I often use a brute-force implementation + random testcase generator.

1 Like

Nice !

1 Like