def print_result(x, B): if x < 10: print(x) else: print('A') M = int(input()) D = int(input()) N = int(input()) B = int(input()) x0 = M % B # 预计算每个x的下一个状态 next_x = {} for x in range(B): a = (x + D) % B if x == 0: next_x[x] = 1 % B else: next_x[x] = pow(a, x, B) memo = {} current_x = x0 for step in range(N + 1): if current_x in memo: # 发现循环 first_occurrence = memo[current_x] cycle_length = step - first_occurrence remaining_steps = N - first_occurrence if remaining_steps <= 0: # 无需进入循环,直接返回当前x break else: # 计算剩余步骤在循环中的位置 remainder = remaining_steps % cycle_length final_step = first_occurrence + remainder # 回溯到final_step temp_x = x0 for s in range(final_step): temp_x = next_x[temp_x] current_x = temp_x break memo[current_x] = step if step == N: break current_x = next_x[current_x] # 确保current_x在B范围内 current_x %= B print_result(current_x, B)