My both Solutions got AC in one go.
Problem: SURVIVE
My short solution in PYTHON-3.5:Mine is O(n) could be done in O(1) also.
te=int(input())
for _ in range(te):
n,k,s=input().split()
n,k,s=int(n),int(k),int(s)
capacity=k*s
availablity=n*s
availablity-=n*(s//7)
maans=0
if(capacity<=availablity):
answer=0
counter=0
for i in range(s):
counter+=1
counter%=7
if(counter!=0):
maans+=1
answer+=n
if(answer>=capacity):
break
print(maans)
else:
print("-1")
LOGIC:-
Maxmimum choclates he needs is ks which is capacity. And the availablity of chocolates is ns-(n*(s//7)) as on Sunday shop is closed and he can’t buy any chocolates. So if capacity > availablity print("-1") as it is not possible to purchase. Else Run a loop to find the number of days and dont forget not to calculate sundays and add n chocolates only on non sundays.
Problem: MULTHREE
My Short solution in PYTHON 3.5:-Time Complexity O(1)
test=int(input())
for y in range(test):
k,dig0,dig1=input().split()
k,dig0,dig1=int(k),int(dig0),int(dig1)
sum1=dig0+dig1
for i in range(3,k+2):
y=sum1%10
sum1+=y
if(y==2):
ter1=((k-i)//4)
ter1*=20
sum1+=ter1
ter=k-i
if(ter%4==3):
sum1+=18
elif(ter%4==2):
sum1+=12
elif(ter%4==1):
sum1+=4
break
if(y==0):
break
if(sum1%3==0):
print("YES")
else:
print("NO")
LOGIC:-
For a number to be divisible by Three sum should be divisisble by 3.Keep on adding sum and finding digits(sum%10); if digit is 0 then break as sum will remain same even after u do any operation so break.
Else break when u find a two as 2,4,8,6 block will keep on repeating. therefore add ((k-i)//4)(2+4+6+8)
to sum ((k-i)//4) is floor division plus remaining 4 12 or 18 depending on whether 4 or 4,8 or 4,8,6 is remaining. This block’s size is got by (k-i)%4.Then at last check whether sum is divisible by 3.
Essentially almost same as solution of @taran_1407
Like,Comment, Follow and award reputation points if u like. For any other queries or doubts contact me.
For Codes as files to download visit:-
THIS PAGE