Generator Dream

시간 제한1초메모리 제한2048 MB

요약
소수 p와 x*2^(i-1) mod p의 하위 비트 ceil(log2 p)개가 주어질 때 비밀 시드 x를 복원한다.
난이도

어려움10점 중 8점

유형
정수론, 수학, 비트 연산, 이분 탐색
정답자
아직 제출이 없습니다

문제

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 xx and a known prime pp. Then the game generates a sequence of "random" numbers x_1,x_2,…,x_i,…x\_1,x\_2,\dots ,x\_i, \dots and "random" bits b_1,b_2,…,b_i,…b\_1,b\_2, \dots ,b\_i, \dots by defining

x_i:=2i−1x mod px\_i:=2^{i-1}x \bmod p, and b_i:=x_i mod 2b\_i:=x\_i \bmod 2.

Rem has been playing for a while, and thinks they have enough information to guess the secret xx. Given pp and the first ⌈log⁡_2⁡(p)⌉\left\lceil\log\_2⁡(p)\right\rceil "random" bits, return the secret xx for Rem.

입력

Input consists of a prime number pp, (2≤p<1092≤p<10^9) and a binary string b_1b_2⋯b_⌈log⁡_2⁡(p)⌉b\_1b\_2\cdots b\_{\left\lceil\log\_2⁡(p)\right\rceil} as described above.

출력

Display the value of xx reduced modulo pp. That is, the value of the secret seed xx.

예제5

  1. 예제 1

    입력
    2
    1
    
    예상 출력
    1
    
  2. 예제 2

    입력
    5
    110
    
    예상 출력
    3
    
  3. 예제 3

    입력
    7
    110
    
    예상 출력
    5
    
  4. 예제 4

    입력
    257
    100000010
    
    예상 출력
    3
    
  5. 예제 5

    입력
    257
    110101100
    
    예상 출력
    173