# Time Limit exceed

i am getting time limit exceed in my program and i am not getting whats the problem the question is Please Like me

``````    itr = int(raw_input())
arr = []
for i in xrange(0,itr):
inp = raw_input()
a , b , c , d = [int(d) for d in inp.split()]
for j in xrange(1 , b):
c= c + (c*d)
if c>=a:
e = "ALIVE AND KICKING"
else:
arr.append(e)
for i in xrange(0 ,itr):
print arr[i]
``````
1 Like

@dpmittal: you are getting TLE because you are iterating b times to calculate final c for every input. you are taking T*D computations to complete one test file and this will lead to TLE because T,D are very large and the time limit is 1sec in which you can compute 10^7 to 10^8 computations. you can optimize that to a very large extent by using basic maths.

• if you observe it carefully D2=(S)+©(S). if you take S common then you get S(1+C) for D2
• similarly D3=(D2)+©(D2) again taking D2 common you get D2(1+C) but D2=S(1+C) from above point. So finally D3=S(1+C)^2 similarly D4=S(1+C)^3 and so on…
• so you just need to check whether S(1+C)^D-1 is greater than or equal to L then print ALIVE else DEAD.
• you need not save it in an array because you can print it directly again traversing array is waste of time.

I hope you understand. If this is helpful to you upvote and accept my answer. happy coding

1 Like

The condition to be checked is:s((1+c)^d-1)>=l; this is the same condition you have used but to check this condition ,there is no need to find value of s((1+c)^d-1),which is the wrong you have commited.long long int can store up to 10^18 only but the left hand side value will be much more than that resulting in overflow which is the reason for your wrong answer ot tle. A simple implementation like this would suffice:

Psuedo Code:

{

c++;

long long int count=1;

while(s<l)

{

s=s*c;

count++;

}

if(d>=count)

print(“ALIVE AND KICKING”)

else