P10235: Modulo Solitaire
传统题
10.000s
时间限制
512MB
内存限制
2 提交
2 解决
【题目描述】
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 a
i and b
i. Finally, you pick a starting number s
0. Now, your goal is to go from s
0 to 0 in as few moves as possible. In each move, you choose an i
, then multiply your current number by a
i, add b
i to it, and reduce the result modulo m
. That is, s
j = (s
j-1 * a
ij + b
ij) % m
. 【输入】
The first line of input contains three integers 0 < m <= 1000000
, 0 <= n <= 10
, and 0 < s
0 < m
. The next n
lines each contain two integers, a pair 0 <= a
i <= 1000000000
and 0 <= b
i <= 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.