Logo
(追記) (追記ここまで)

4131번 - Modulo Solitaire 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB88564462.857%

문제

Modulo Solitaire is a game that can be played when you are bored. You can even play it without a phone, just on paper. First, you pick a number m. Then you pick two sequences of numbers ai and bi. Finally, you pick a starting number s0. Now, your goal is to go from s0 to 0 in as few moves as possible. In each move, you choose an i, then multiply your current number by ai, add bi to it, and reduce the result modulo m. That is, sj = (sj-1 * aij + bij) % m.

입력

The first line of input contains three integers 0 < m ≤ 1000000, 0 ≤ n ≤ 10, and 0 < s0 < m. The next n lines each contain two integers, a pair 0 ≤ ai ≤ 1000000000 and 0 ≤ bi ≤ 1000000000.

출력

Output a single integer, the shortest number of moves needed to reach 0 starting from s0. If it is not possible to reach 0 in any number of moves, output -1.

제한

예제 입력 1

5 2 1
2 1
3 1

예제 출력 1

2

힌트

출처

Contest > Waterloo's local Programming Contests > 16 September, 2012 D번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /