Series Sum II


Problem Statement

You are given three integers nn, xx and kk.

You have the following infinite sequence S=[1 mod k,   (1+2) mod k,   (1+2+3) mod k, ...]S = [1 \small\text{ mod } \normalsize k,\ \ \ (1 + 2) \small\text{ mod }\normalsize k,\ \ \ (1 + 2 + 3) \small\text{ mod }\normalsize k,\ ... ], in other words Si=(∑j=1ij) mod kS_i = (\sum_{j=1}^ij) \small\text{ mod }\normalsize k.

Determine at which index (one-indexed) the nthn^{th} occurrence of xx is in the sequence SS if xx occurs at least nn times, otherwise output −1-1.

Input Format

Your first line will contain three space-separated integers nn, xx and kk respectively.

Output Format

You should output a single integer, representing the index (one-indexed) in SS at which the nthn^{th} occurrence of xx appears. If it does not appear at least nn times, then output −1-1.

Constraints

  • 1≤n≤1091 \leq n \leq 10^{9}
  • 1≤k≤1021 \leq k \leq 10^2
  • 0≤x<k0 \leq x \lt k

Sample Cases

Input 1
1 1 10
Output 1
1
Input 2
2 1 10
Output 2
6
Input 3
20 2 13
Output 3
124
Input 4
20 2 10
Output 4
-1

Template

Code 1
n, x, k = map(int, input().split())

# print your output

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.