시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 2048 MB22131365.000%

문제

Rem is playing a game that relies on random bits and is starting to get annoyed by all the random chance. Rem wants to win, not to gamble! So, Rem wants your help in avoiding the randomness as much as possible.

The game generates randomness by starting with a secret seed $x$ and a known prime $p$. Then the game generates a sequence of "random" numbers $x_1,x_2,\dots ,x_i, \dots$ and "random" bits $b_1,b_2, \dots ,b_i, \dots$ by defining

$x_i:=2^{i-1}x \bmod p$, and $b_i:=x_i \bmod 2$.

Rem has been playing for a while, and thinks they have enough information to guess the secret $x$. Given $p$ and the first $\left\lceil\log_2⁡(p)\right\rceil$ "random" bits, return the secret $x$ for Rem.

입력

Input consists of a prime number $p$, ($2≤p<10^9$) and a binary string $b_1b_2\cdots b_{\left\lceil\log_2⁡(p)\right\rceil}$ as described above.

출력

Display the value of $x$ reduced modulo $p$. That is, the value of the secret seed $x$.

예제 입력 1

2
1

예제 출력 1

1

예제 입력 2

5
110

예제 출력 2

3

예제 입력 3

7
110

예제 출력 3

5

예제 입력 4

257
100000010

예제 출력 4

3

예제 입력 5

257
110101100

예제 출력 5

173

출처

University > University of Alberta Programming Contest > UAPC 2025 > Division 1 J번

  • 문제를 만든 사람: Jacob Skitsko