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

#BS0000173. Tìm tất cả cặp số nghiệm của hàm ẩn (Find Positive Integer Solution for a Given Equation)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tìm tất cả cặp số nghiệm của hàm ẩn (Find Positive Integer Solution for a Given Equation)

Find Positive Integer Solution for a Given Equation

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You can query an undisclosed function f(x,y)f(x,y) of two positive integers. It is strictly increasing in each variable: f(x+1,y)>f(x,y)f(x+1,y)>f(x,y) and f(x,y+1)>f(x,y)f(x,y+1)>f(x,y). The target zz is known. Find every positive integer pair (x,y)(x,y) with 1≤x,y≤10001\le x,y\le1000 and f(x,y)=zf(x,y)=z. There may be no valid pairs.

PHOJ Extended interaction: The formula and function identifier stay private to the judge. You may print ? x y followed by a flush; the judge answers with the integer f(x,y)f(x,y). To submit the final answer, print ! k, followed by exactly kk lines containing x y for the different valid pairs, in any order. At most 20002000 function queries are allowed. Invalid queries, an incorrect protocol, or missing/extra pairs are rejected. This interactive protocol replaces the LeetCode CustomFunction interface for PHOJ.

Input

The judge first sends one line with the target integer zz. After each ? x y query, it replies on a separate line with the value f(x,y)f(x,y). The function identifier is private and is not provided to the contestant.

Output

For a query, print ? x y on one line and flush. For the final answer, print ! k, then exactly kk lines, each with x y. All output pairs must be distinct and the set must contain every valid pair.

Subtasks

  • Subtask 1 (20 points): 1≤z≤1001\le z\le100; queries have 1≤x,y≤10001\le x,y\le1000; at most 20002000 calls to a hidden function from the nine strictly increasing functions.
  • Subtask 2 (30 points): 1≤z≤1041\le z\le10^4; queries have 1≤x,y≤10001\le x,y\le1000; at most 20002000 calls to a hidden function from the nine strictly increasing functions.
  • Subtask 3 (50 points): 1≤z≤1061\le z\le10^6; queries have 1≤x,y≤10001\le x,y\le1000; at most 20002000 calls to a hidden function from the nine strictly increasing functions.

Examples

Input

5

Output

? 1 1
? 1 2
! 4
1 4
2 3
3 2
4 1

Explanation

In this demonstration, the hidden function is f(x,y)=x+yf(x,y)=x+y and the target is z=5z=5. The queries ? 1 1 and ? 1 2 return 22 and 33, respectively. The valid pairs are (1,4)(1,4), (2,3)(2,3), (3,2)(3,2), and (4,1)(4,1); each pair sums to 55. No other positive integer pair fits the target. After ! 4, print these four pairs. This illustrates the protocol only; the actual hidden function is not disclosed.