https://www.acmicpc.net/problem/23204
I've solved a programming challenge where you need to count the occurrences of a specific digit at the end of the product of multiples of a given number within a certain range.
Description
A large shipment of doodads has just arrived, and each doodad has a suggested retail price of b cents. You’ve noticed that consumers are much more likely to purchase goods when most of the trailing digits are the same. For example, items are more likely to be priced at 99 cents rather than 57 cents. So to make your goods more appealing, you’ve decided to sell your goods in bundles. To make a bundle, you choose a positive integer k, and sell k doodads for k × b cents. With an appropriate choice of k you can have a more pleasing price. For example, selling 57-cent doodads in bundles of size 7 means that each bundle sells for 399 cents, which has two trailing 9s, rather than no trailing 9s of 57. This idea of trailing 9s can be generalized to any other trailing digit: bundles of 692 57-cent doodads sell for 39 444 cents (three trailing 4s) and bundles of one million doodads sell for 57 000 000 cents (six trailing 0s).
After a little thought, you realize that you do not want to make your bundles too large—not only can the price be excessive, but who really needs several million doodads? For any type of doodad, your marketing department has a maximum bundle price of a.
Given the price of a doodad, the desired trailing digit, and the maximum price of a bundle, write a program that optimizes the trailing digits.
Input
Input consists of a single line containing three integers b, d, and a, where b (1 ≤ b < \$10^6\$) is the price of a doodad in cents, d (0 ≤ d ≤ 9) is the desired trailing digit, and a (b ≤ a < \$10^{10,000}\$) is the maximum price of a bundle.
Output
Output the maximum number of consecutive occurrences of d that can appear at the end of a bundle price, given that the price of the bundle cannot exceed a.
sample input 1
57 9 1000
sample output 1
2
sample input 2
57 4 40000
sample output 2
3
sample input 3
57 4 39000
sample output 3
2
code
def main(B,d,a):
count=0
max=0
price=0
i=1
while price<a:
price=B*i
#print(price)
#print(str(price))
count=0
for x in range(len(str(price))):
if(str(price)[len(str(price))-1-x]==str(d) ):
#print("price",str(price))
count+=1
#print(count)
else:
break
if(max<count):
#print("max",count)
max=count
i+=1
print(max)
if __name__ == '__main__':
main(57,9,1000)
main(57,4,40000)
main(57,4,39000 )
I know the code fits but I always exceed the time for the validation page. I could translate to C or c++ language but I receive any suggestions for improve logic or performance
I read it. I just suggest to think of another way to solve the problem. It is a mathematical question, not really a programming's one. (That kind of site is not good to teach programming).
– Jarod42
2023-11-17 16:07:13Z, License: CC BY-SA 4.0
How can I improve the performance?
code V.2
def reverse_iteration(a, b, d):
k = a // b # Start with the maximum possible value of k
# print(k)
max_repetition = 0
while k > 0:
total_cost = k * b
bestresult = len(total_cost.__str__())
if total_cost <= a:
print("k:", k, "Total Cost:", total_cost)
# repetition = total_cost % b
number_str = str(total_cost)
count = 0
last_digit = None
for digit in reversed(number_str):
if digit == str(d) and (digit == last_digit or last_digit is None):
count += 1
last_digit = digit
else:
break
# print(count)
if count > max_repetition:
max_repetition = count
if count == bestresult \
or (int(total_cost.__str__()[0])<d and max_repetition==bestresult-1):
k=-1
continue
k -= 1
print(max_repetition)
if __name__ == '__main__':
reverse_iteration(1000, 57, 9)
reverse_iteration(1000, 57, 5)
reverse_iteration(40000, 57, 4)
reverse_iteration(39000, 57, 4)
code V.3 calculate relevant factor
def most_trailing_digits(a, b, d):
""" Print the maximal amount of trailing digits "trailing"
a bundle of doodads priced "retail" may have up to
a bundle price of "maximum".
"""
k = a // b # Start with the maximum possible value of k
print("max iterations",k)
max_repetition = 0
result_with_max_repetition = None
factors=[]
reducer = []
iterations=0
#determine relevan factors
for i in range(1, 11): # Iterar sobre los números del 1 al 10
resultado = b * i
if resultado % 10 == d: # Verificar si el último dígito es el buscado
factors.append(i)
print("factors",factors)
print(k * b)
for i in factors:
if i != k % 10:
if k % 10 <i:
reducer.append((k % 10) + 10 - i)
else:
reducer.append((k % 10) - i)
else:
reducer.append(0)
#last conection
if reducer[0]==0 and len(reducer)>1:
reducer[0]=(factors[len(factors)-1] - factors[0])
#single reducer
if len(reducer)==1:
k=k-reducer[0]
reducer[0]=10
print("reducer",reducer)
iteratorReducer=0
while k > 0:
iterations+=1
# print(iterations)
total_cost = k * b
bestresult = len(total_cost.__str__())
if total_cost <= a:
print("k:", k, "Total Cost:", total_cost)
# repetition = total_cost % b
number_str = str(total_cost)
count = 0
last_digit = None
for digit in reversed(number_str):
if digit == str(d) and (digit == last_digit or last_digit is None):
count += 1
last_digit = digit
else:
break
# print(count)
if count > max_repetition:
max_repetition = count
if count == bestresult \
or (int(total_cost.__str__()[0])<d and max_repetition==bestresult-1):
k=-1
continue
iteratorReducer += 1
currenreducer=0
if len(reducer)==1:
currenreducer=reducer[0]
else:
currenreducer=reducer[iteratorReducer % len(reducer)]
k = k -currenreducer
print("iterations",iterations)
print(max_repetition)
## tests
most_trailing_digits(1000, 57, 9)
most_trailing_digits(1000, 57, 5)
most_trailing_digits(40000, 56, 4)
most_trailing_digits(39000, 57, 4)
range
is nonsensical, and you're counting all, rather than trailing, occurrences ofd
. The puzzle author was hoping to get you to reason about e.g.k * b % 100 == 99
ork * b % 1000 == 777
. And you don't need all ofb
for that --b % 100
orb % 1000
could suffice. \$\endgroup\$d
like6
, or7
. Ask yourself "what is the infinite set of positive integer pairs that multiply to end withd
?" Now write code which explores a portion of that set. Having solved the one-digit case, can you use that to get to the two-digit case? \$\endgroup\$