





from __future__ import annotations

UpperCAmelCase_      							=     list[list[int]]

# assigning initial values to the grid
UpperCAmelCase_      							=     [
    [3, 0, 6, 5, 0, 8, 4, 0, 0],
    [5, 2, 0, 0, 0, 0, 0, 0, 0],
    [0, 8, 7, 0, 0, 0, 0, 3, 1],
    [0, 0, 3, 0, 1, 0, 0, 8, 0],
    [9, 0, 0, 8, 6, 3, 0, 0, 5],
    [0, 5, 0, 0, 9, 0, 6, 0, 0],
    [1, 3, 0, 0, 0, 0, 2, 5, 0],
    [0, 0, 0, 0, 0, 0, 0, 7, 4],
    [0, 0, 5, 2, 0, 6, 3, 0, 0],
]

# a grid with no solution
UpperCAmelCase_      							=     [
    [5, 0, 6, 5, 0, 8, 4, 0, 3],
    [5, 2, 0, 0, 0, 0, 0, 0, 2],
    [1, 8, 7, 0, 0, 0, 0, 3, 1],
    [0, 0, 3, 0, 1, 0, 0, 8, 0],
    [9, 0, 0, 8, 6, 3, 0, 0, 5],
    [0, 5, 0, 0, 9, 0, 6, 0, 0],
    [1, 3, 0, 0, 0, 0, 2, 5, 0],
    [0, 0, 0, 0, 0, 0, 0, 7, 4],
    [0, 0, 5, 2, 0, 6, 3, 0, 0],
]





def 						lowerCamelCase__     (     UpperCamelCase__			:       Matrix       ,    UpperCamelCase__			:       int       ,    UpperCamelCase__			:       int       ,    UpperCamelCase__			:       int						)			-> bool:



					'''simple docstring'''




					for i in range(9						):
										if grid[row][i] == n or grid[i][column] == n:
															return False

					for i in range(3						):
										for j in range(3						):
															if grid[(row - row % 3) + i][(column - column % 3) + j] == n:
																				return False

					return True





def 						lowerCamelCase__     (     UpperCamelCase__			:       Matrix						)			-> tuple[int, int] | None:



					'''simple docstring'''




					for i in range(9						):
										for j in range(9						):
															if grid[i][j] == 0:
																				return i, j
					return None





def 						lowerCamelCase__     (     UpperCamelCase__			:       Matrix						)			-> Matrix | None:



					'''simple docstring'''




					if location := find_empty_location(UpperCamelCase__						):
										_snake_case							,     _snake_case									=				location
					else:
										# If the location is ``None``, then the grid is solved.
										return grid

					for digit in range(1       ,    10						):
										if is_safe(UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__						):
															_snake_case									=				digit

															if sudoku(UpperCamelCase__						) is not None:
																				return grid

															_snake_case									=				0

					return None





def 						lowerCamelCase__     (     UpperCamelCase__			:       Matrix						)			-> None:



					'''simple docstring'''




					for row in grid:
										for cell in row:
															print(UpperCamelCase__       ,    end=' '						)
										print()


if __name__ == "__main__":
					# make a copy of grid so that you can compare with the unmodified grid
					for example_grid in (initial_grid, no_solution):
										print("""\nExample grid:\n""" + """=""" * 20)
										print_solution(example_grid)
										print("""\nExample grid solution:""")
										UpperCAmelCase_      							=     sudoku(example_grid)
										if solution is not None:
															print_solution(solution)
										else:
															print("""Cannot find a solution.""")










UpperCAmelCase_      							=     range(2, 20 + 1)
UpperCAmelCase_      							=     [10**k for k in range(ks[-1] + 1)]
UpperCAmelCase_      							=     {}





def 						lowerCamelCase__     (     UpperCamelCase__			:       Optional[Any]       ,    UpperCamelCase__			:       List[Any]       ,    UpperCamelCase__			:       Union[str, Any]       ,    UpperCamelCase__			:       str						)			-> List[str]:



					'''simple docstring'''




					_snake_case									=				sum(a_i[j] for j in range(UpperCamelCase__       ,    len(UpperCamelCase__						)						)						)
					_snake_case									=				sum(a_i[j] * base[j] for j in range(min(len(UpperCamelCase__						)       ,    UpperCamelCase__						)						)						)

					_snake_case							,     _snake_case									=				0, 0
					_snake_case									=				n - i

					_snake_case									=				memo.get(UpperCamelCase__						)

					if sub_memo is not None:
										_snake_case									=				sub_memo.get(UpperCamelCase__						)

										if jumps is not None and len(UpperCamelCase__						) > 0:
															# find and make the largest jump without going over
															_snake_case									=				-1
															for _k in range(len(UpperCamelCase__						) - 1       ,    -1       ,    -1						):
																				if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
																									_snake_case									=				_k
																									break

															if max_jump >= 0:
																				_snake_case							,     _snake_case							,     _snake_case									=				jumps[max_jump]
																				# since the difference between jumps is cached, add c
																				_snake_case									=				diff + c
																				for j in range(min(UpperCamelCase__       ,    len(UpperCamelCase__						)						)						):
																									_snake_case							,     _snake_case									=				divmod(UpperCamelCase__       ,    10						)
																				if new_c > 0:
																									add(UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__						)

										else:
															_snake_case									=				[]
					else:
										_snake_case									=				{c: []}
										_snake_case									=				sub_memo

					if dn >= max_dn or c + diff >= base[k]:
										return diff, dn

					if k > ks[0]:
										while True:
															# keep doing smaller jumps
															_snake_case							,     _snake_case									=				next_term(UpperCamelCase__       ,    k - 1       ,    i + dn       ,    UpperCamelCase__						)
															diff += _diff
															dn += terms_jumped

															if dn >= max_dn or c + diff >= base[k]:
																				break
					else:
										# would be too small a jump, just compute sequential terms instead
										_snake_case							,     _snake_case									=				compute(UpperCamelCase__       ,    UpperCamelCase__       ,    i + dn       ,    UpperCamelCase__						)
										diff += _diff
										dn += terms_jumped

					_snake_case									=				sub_memo[c]

					# keep jumps sorted by # of terms skipped
					_snake_case									=				0
					while j < len(UpperCamelCase__						):
										if jumps[j][1] > dn:
															break
										j += 1

					# cache the jump for this value digitsum(b) and c
					sub_memo[c].insert(UpperCamelCase__       ,    (diff, dn, k)						)
					return (diff, dn)





def 						lowerCamelCase__     (     UpperCamelCase__			:       str       ,    UpperCamelCase__			:       str       ,    UpperCamelCase__			:       List[str]       ,    UpperCamelCase__			:       Union[str, Any]						)			-> Optional[Any]:



					'''simple docstring'''




					if i >= n:
										return 0, i
					if k > len(UpperCamelCase__						):
										a_i.extend([0 for _ in range(k - len(UpperCamelCase__						)						)]						)

					# note: a_i -> b * 10^k + c
					# ds_b -> digitsum(b)
					# ds_c -> digitsum(c)
					_snake_case									=				i
					_snake_case							,     _snake_case							,     _snake_case									=				0, 0, 0
					for j in range(len(UpperCamelCase__						)						):
										if j >= k:
															ds_b += a_i[j]
										else:
															ds_c += a_i[j]

					while i < n:
										i += 1
										_snake_case									=				ds_c + ds_b
										diff += addend
										_snake_case									=				0
										for j in range(UpperCamelCase__						):
															_snake_case									=				a_i[j] + addend
															_snake_case							,     _snake_case									=				divmod(UpperCamelCase__       ,    10						)

															ds_c += a_i[j]

										if addend > 0:
															break

					if addend > 0:
										add(UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__						)
					return diff, i - start_i





def 						lowerCamelCase__     (     UpperCamelCase__			:       Any       ,    UpperCamelCase__			:       Union[str, Any]       ,    UpperCamelCase__			:       Optional[Any]						)			-> Any:



					'''simple docstring'''




					for j in range(UpperCamelCase__       ,    len(UpperCamelCase__						)						):
										_snake_case									=				digits[j] + addend
										if s >= 10:
															_snake_case							,     _snake_case									=				divmod(UpperCamelCase__       ,    10						)
															_snake_case									=				addend // 10 + quotient
										else:
															_snake_case									=				s
															_snake_case									=				addend // 10

										if addend == 0:
															break

					while addend > 0:
										_snake_case							,     _snake_case									=				divmod(UpperCamelCase__       ,    10						)
										digits.append(UpperCamelCase__						)





def 						lowerCamelCase__     (     UpperCamelCase__			:       int = 10**15						)			-> int:



					'''simple docstring'''




					_snake_case									=				[1]
					_snake_case									=				1
					_snake_case									=				0
					while True:
										_snake_case							,     _snake_case									=				next_term(UpperCamelCase__       ,    20       ,    i + dn       ,    UpperCamelCase__						)
										dn += terms_jumped
										if dn == n - i:
															break

					_snake_case									=				0
					for j in range(len(UpperCamelCase__						)						):
										a_n += digits[j] * 10**j
					return a_n


if __name__ == "__main__":
					print(F"{solution() = }")



