#1340
Unrated
Hack
인터랙티브
시간 제한
10s
메모리 제한
512MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Heidi is analyzing a peculiar device. This device takes an aa as input and computes ad(modn)a^d\pmod n using the following pseudocode and some integers dd and nn stored in this device:

modPow(a, d, n) {
  r = 1;
  for (i = 0; i < 60; ++i) {
    if ((d & (1 << i)) != 0) {
      r = r * a % n;
    }
    a = a * a % n;
  }
}

Note that the pseudocode assumes arbitrary sized integers, << denotes bitwise shift left, & denotes bitwise and, and % denotes modulo.

The device does not tell Heidi the result of the computation. However, Heidi can measure how long does the computation take. She knows that only multiplication modulo nn (lines 5 and 7 in the above pseudocode) takes any measurable amount of time, all other lines can be assumed to take 0 nanoseconds. Moreover, she knows that it takes (bits(x)+1)(bits(y)+1)(\operatorname{bits}(x) + 1) \cdot (\operatorname{bits}(y) + 1) nanoseconds to multiply xx by yy modulo nn, where bits(x)\operatorname{bits}(x) is the number of bits in the binary representation of xx without leading zeros, or more formally bits(x)=log2(x+1)\operatorname{bits}(x)=\lceil \log_2{(x+1)}\rceil.

Heidi knows the integer nn but does not know the integer dd. She wants to find dd by feeding the device different integers aa as input and measuring the time the computation takes for each aa.

She knows that nn and dd were chosen in the following way: first, two prime numbers pp and qq with 30 bits in binary representation (in other words, between 2292^{29} and 23012^{30}-1) were picked independently and uniformly at random. Then the number nn was computed as n=pqn=p\cdot q. Then the number m=ϕ(n)=(p1)(q1)m=\phi(n)=(p-1)\cdot(q-1) was computed. Then dd was picked uniformly at random between 1 and m1m-1 inclusive, such that it is coprime with mm.

입력

출력

상호작용

First, the testing system writes the integer nn — the modulo used by the device. Note that nn and the hidden number dd are guaranteed to have been generated according to the procedure described above.

Your solution shall print requests of two types:

  • "? aa" tells to feed aa as input to the device. aa must be an integer between 0 and n1n-1 inclusive. The testing system responds with the time it took the device to compute modPow(a,d,n)(a, d, n) in nanoseconds.
  • "! dd" tells the value of dd that your program has determined.

Don't forget to flush the output after each request!

Your solution must issue exactly one request of the second type, which must be the last request, and the solution must terminate gracefully after issuing it.

Your solution is allowed to issue at most 3000030\,000 requests of the first type.

Your solution will be run on 30 testcases, working with one (nn, dd) pair per run. For each testcase the numbers nn and dd are fixed and were generated using the procedure described above. The example below was not generated in that manner and thus will not be used for testing your solution; it only serves to illustrate the input/output format and provide a sanity check for your calculation of the computation time.

예제 입력 1

15
980
293

예제 출력 1

? 3
? 8
! 5

노트

In the first request in the example case, the following multiplications are done by the device when computing modPow(3,5,15)(3, 5, 15):

  • 13(mod15)=31\cdot 3 \pmod{15} = 3, taking 6 nanoseconds
  • 33(mod15)=93\cdot 3 \pmod{15} = 9, taking 9 nanoseconds
  • 99(mod15)=69\cdot 9 \pmod{15} = 6, taking 25 nanoseconds
  • 36(mod15)=33\cdot 6 \pmod{15} = 3, taking 12 nanoseconds
  • 66(mod15)=66\cdot 6 \pmod{15} = 6, taking 16 nanoseconds
  • 66(mod15)=66\cdot 6 \pmod{15} = 6, taking 16 nanoseconds
  • 66(mod15)=66\cdot 6 \pmod{15} = 6, taking 16 nanoseconds
  • ... (55 more repetitions of the last multiplication)

The computation takes 6+9+25+12+5816=9806+9+25+12+58\cdot 16=980 nanoseconds.

A positive integer is prime if it has exactly two divisors: 1 and itself. Two positive integers are coprime if their greatest common divisor is 1.

Here are a few first values of the function bits(x)\operatorname{bits}(x):

  • bits(0)=0\operatorname{bits}(0)=0
  • bits(1)=1\operatorname{bits}(1)=1
  • bits(2)=2\operatorname{bits}(2)=2
  • bits(3)=2\operatorname{bits}(3)=2
  • bits(4)=3\operatorname{bits}(4)=3
코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.