def multiply_matrix(a, b, mod): res = [[0]*2 for _ in range(2)] res[0][0] = (a[0][0] * b[0][0] + a[0][1] * b[1][0]) % mod res[0][1] = (a[0][0] * b[0][1] + a[0][1] * b[1][1]) % mod res[1][0] = (a[1][0] * b[0][0] + a[1][1] * b[1][0]) % mod res[1][1] = (a[1][0] * b[0][1] + a[1][1] * b[1][1]) % mod return res def matrix_power(mat, power, mod): result = [[1,0], [0,1]] # Identity matrix while power > 0: if power % 2 == 1: result = multiply_matrix(result, mat, mod) mat = multiply_matrix(mat, mat, mod) power = power // 2 return result def compute_new_n(n, M, mod): if M == 0: return 0 % mod mat = [[n, 1], [1, 0]] power = M - 1 mat_pow = matrix_power(mat, power, mod) # Multiply with [1, 0] vector b_m = (mat_pow[0][0] * 1 + mat_pow[0][1] * 0) % mod return b_m def main(): S = input().strip() M = int(input().strip()) L = int(input().strip()) current_n = int(S) mod = 10000 history = [] found_cycle = False cycle_start = 0 cycle_length = 0 for _ in range(L): # Check if current_n is in history to find cycle if current_n in history: idx = history.index(current_n) cycle_start = idx cycle_length = len(history) - idx remaining = L - _ total_remaining = remaining final_pos = idx + (total_remaining) % cycle_length current_n = history[final_pos] found_cycle = True break history.append(current_n) # Compute new_n new_n = compute_new_n(current_n, M, mod) current_n = new_n if not found_cycle: current_n = current_n % mod # Format the result to 4 digits print("{:04d}".format(current_n)) if __name__ == "__main__": main()