Skip to content

HalloweenSale #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions HackerRank/HalloweenSales.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys
import math

def howManyGames(p, d, m, s):
t_l = 0
if p % d != 0:
t_l = p % d
else:
t_l = d
d = -d
t_n = ((t_l-p)/d) + 1
t_sum = (t_n*(p+t_l))/2
if s > t_sum:
n = ((m-p)/d) + 1
l = p+((n-1)*d)
sn = (n*(p+l))/2
return n+((s-sn)/m)

else:
x1 = abs(int(((-((2*p)-d))+math.sqrt((((2*p)-d)**2)-((4*d)*(-(2*s)))))/(2*d)))
x2 = abs(int(((-((2*p)-d))-math.sqrt((((2*p)-d)**2)-((4*d)*(-(2*s)))))/(2*d)))
l = p+((x1-1)*d)
s1 = (x1*(p+l))/2
l = p+((x2-1)*d)
s2 = (x2*(p+l))/2
if s1 <= s:
return x1
else:
return x2

if __name__ == "__main__":
p, d, m, s = raw_input().strip().split(' ')
p, d, m, s = [int(p), int(d), int(m), int(s)]
answer = howManyGames(p, d, m, s)
print answer