Problem p03016 (python)

Fixed submission:
L, A, B, M = map(int, raw_input().split())

# first i such that A + i*B has d digits.
# A + i*B >= 10^d
# i*B >= 10^d - A
# i >= 
# i = (10^d - A + (B-1))/B

def get_indices(d):
	i_start = (10**d - A + (B-1)) / B
	i_end = (10**(d+1) - A + (B-1)) / B
	i_start = max(0, i_start)
	i_end = max(0, i_end)
	i_start = min(L, i_start)
	i_end = min(L, i_end)
	return (d+1, i_start, i_end)
	
index_pairs = [get_indices(n) for n in xrange(0, 19)]

def get_chunk_value(num_digs, i_start, i_end):
	if i_start == i_end:
		return (0, 0)
	
	a = A + B*(i_end - 1)
	d = -B
	n = i_end - i_start
	r = 10 ** num_digs
	
	Mt = M*((1-r)**2)
	
	numerator = (1-r)*(a - (a + n*d)*pow(r, n, Mt)) + d*r*(1 - pow(r, n, Mt))
	numerator %= Mt
	assert numerator % (1-r)**2 == 0
	return (num_digs*(i_end-i_start), numerator/(1-r)**2)
	
	

# print index_pairs
chunk_values = [get_chunk_value(n, i_start, i_end) for n, i_start, i_end 
													in index_pairs]
# print chunk_values

ans = 0
digits_offset = 0
for num_digs, value in reversed(chunk_values):
	ans += value * pow(10, digits_offset, M)
	digits_offset += num_digs
	
ans %= M
print ans

Buggy submission (verdict: Runtime Error):
L, A, B, M = map(int, input().split())

# first i such that A + i*B has d digits.
# A + i*B >= 10^d
# i*B >= 10^d - A
# i >= 
# i = (10^d - A + (B-1))/B

def get_indices(d):
	i_start = (10**d - A + (B-1)) / B
	i_end = (10**(d+1) - A + (B-1)) / B
	i_start = max(0, i_start)
	i_end = max(0, i_end)
	i_start = min(L, i_start)
	i_end = min(L, i_end)
	return (d+1, i_start, i_end)
	
index_pairs = [get_indices(n) for n in xrange(0, 19)]

def get_chunk_value(num_digs, i_start, i_end):
	if i_start == i_end:
		return (0, 0)
	
	a = A + B*(i_end - 1)
	d = -B
	n = i_end - i_start
	r = 10 ** num_digs
	
	Mt = M*((1-r)**2)
	
	numerator = (1-r)*(a - (a + n*d)*pow(r, n, Mt)) + d*r*(1 - pow(r, n, Mt))
	numerator %= Mt
	assert numerator % (1-r)**2 == 0
	return (num_digs*(i_end-i_start), numerator/(1-r)**2)
	
	

# print index_pairs
chunk_values = [get_chunk_value(n, i_start, i_end) for n, i_start, i_end 
													in index_pairs]
# print chunk_values

ans = 0
digits_offset = 0
for num_digs, value in reversed(chunk_values):
	ans += value * pow(10, digits_offset)
	digits_offset += num_digs
	
ans %= M
print ans

Buggy submission (verdict: Runtime Error):
L, A, B, M = map(int, input().split())

# first i such that A + i*B has d digits.
# A + i*B >= 10^d
# i*B >= 10^d - A
# i >= 
# i = (10^d - A + (B-1))/B

def get_indices(d):
	i_start = (10**d - A + (B-1)) / B
	i_end = (10**(d+1) - A + (B-1)) / B
	i_start = max(0, i_start)
	i_end = max(0, i_end)
	i_start = min(L, i_start)
	i_end = min(L, i_end)
	return (d+1, i_start, i_end)
	
index_pairs = [get_indices(n) for n in xrange(0, 19)]

def get_chunk_value(num_digs, i_start, i_end):
	if i_start == i_end:
		return (0, 0)
	
	a = A + B*(i_end - 1)
	d = -B
	n = i_end - i_start
	r = 10 ** num_digs
	
	Mt = M*((1-r)**2)
	
	numerator = (1-r)*(a - (a + n*d)*pow(r, n, Mt)) + d*r*(1 - pow(r, n, Mt))
	numerator %= Mt
	assert numerator % (1-r)**2 == 0
	return (num_digs*(i_end-i_start), numerator/(1-r)**2)
	
	

# print index_pairs
chunk_values = [get_chunk_value(n, i_start, i_end) for n, i_start, i_end 
													in index_pairs]
# print chunk_values

ans = 0
digits_offset = 0
for num_digs, value in reversed(chunk_values):
	ans += value * pow(10, digits_offset)
	digits_offset += num_digs
	
ans %= M
print ans