PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000390. Truy vấn GCD (GCD Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn GCD (GCD Queries)

GCD Queries

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

The jury fixes a hidden permutation pp of 0,1,…,n−10,1,\ldots,n-1. Find two indices x,yx,y (possibly equal) such that px=0p_x=0 or py=0p_y=0. You may issue no more than 2n2n GCD queries.

This is a real interactive problem; output is not compared to one predetermined file.

Interactive protocol

  • First read nn.
  • Print ? i j, flush, then read gcd⁡(pi,pj)\gcd(p_i,p_j). Both indices must be in [1,n][1,n] and distinct.
  • Print ! x y to answer, flush, and read verdict 1 or -1. Terminate immediately on -1.
  • The permutation does not change during the interaction; the query limit is 2n2n.

For the PHOJ interactor, each hidden test file stores nn followed by a permutation pp. Only nn is revealed to the contestant. Each file represents one independent session rather than multiple interactive cases.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le20; pp is a permutation of 0,…,n−10,\ldots,n-1; at most 2n2n queries.

  • Subtask 2 (30 points): 2≤n≤2 0002\le n\le2\,000; pp is a permutation of 0,…,n−10,\ldots,n-1; at most 2n2n queries.

  • Subtask 3 (50 points): 2≤n≤20 0002\le n\le20\,000; pp is a permutation of 0,…,n−10,\ldots,n-1; at most 2n2n queries.

Example interaction

Suppose the hidden permutation is [1,2,0][1,2,0]:

Jury: 3
Player: ? 1 3
Jury: 1
Player: ? 2 3
Jury: 2
Player: ! 3 2
Jury: 1

The replies are gcd⁡(1,0)=1\gcd(1,0)=1 and gcd⁡(2,0)=2\gcd(2,0)=2. Index 33 contains zero, so the final pair is accepted. This is a transcript illustration, not unique expected output.