First of all ,we need to observe that F[] is monotone increasing, because of (C + 1) > 1.
Second, F[] is increasing exponentially, while L is smaller than 10^9. That means, only O(log L) days are needed for F[D] to exceed L.
Third, when F[] exceed L, it is possible about 10^18 (exceeding int in C++ and Java). So please choose the appropriate type to store.
In summary, what we need to do is calculate F[0ā¦D] one by one, until all D + 1 ones are got or some one is greater than L. The time complexity is in O(min{D, log L}).
AUTHORāS AND TESTERāS SOLUTIONS:
Authorās solution can be found here.
Testerās solution can be found here.
During the contest, I simply derived the general formula in terms of day 1, and, afterwards, some simple maths and logarithm application allowed me to test for the problemās codition simply like this:
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iostream>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int L,D,S,C;
scanf("%d %d %d %d",&L,&D,&S,&C);
if(log(C+1.0)*(D-1) >= log(L)-log(S))
puts("ALIVE AND KICKING");
else
puts("DEAD AND ROTTING");
}
return 0;
}
I guess this a lot less complicated than being mislead or caught in overflow errors
THIS ONE DERIVES GENERAL FORMULA AND COMPARES THE VALUE IN LOG SPACE INSTEAD,
ITS A FAR SIMPLER METHOD
import sys
from math import log
def solve(value):
if value[2] >= value[0]:
print āALIVE AND KICKINGā
return
n = (value[1]-1)*log(1+value[3])+log(value[2]) # total no of bits and bit representation of the product
p = log(value[0]) # value to be compared with (in log form)
diff = n-p
if abs(diff) < 0.000000000000001 : # checking difference upto 15 decimal places
i=1
product=value[2]
while i < value[1]:
product*=value[1]
if product >= value[0]:
print "ALIVE AND KICKING"
break
i+=1
print "DEAD AND ROTTING"
elif diff>= 0:
print "ALIVE AND KICKING"
else: print "DEAD AND ROTTING"
input = sys.stdin.readlines()
N=int(input.pop(0))
for line in input:
values = [] # values are L,D,S,C
values = map(int,line.split())
solve(values)
@vivek7119 size of long double at codechef is 12 bytes and size of long long int is 8 bytes, may be thatās why your program fails on taking long long int.
this will run for smaller D values, when D=10^9, C=10^9, it will be a 9*10^9 digit number :(ā¦ but u can optimize whenever D>32 print DEAD. and when D<32 do your algorithm ā¦( wondering about 32??? as min value of c is 1 so (c+1)^32 will have minimum value = (1+1)^32 which is graeater than 10^9) ans 10^9 is max range of L
def noOfLikes(d, s, c):
if d == 1:
return s
else:
return noOfLikes(d-1, s, c) + noOfLikes(d-1, s, c) * c
t = int(input())
for testCase in range(t):
l, d, s, c = map(int, input().split())
if noOfLikes(d,s,c) >= l:
print("ALIVE AND KICKING")
else:
print("DEAD AND ROTTING")
This is giving a runtime error(NZEC). Any reasons?