QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 100 Interactive

#8955. 排列

统计

這是一道互動題。

Alice 和 Bob 在玩猜數遊戲。首先,Alice 選擇 $b\in\{0,1\}$,Bob 不知道 $b$ 的值。隨後 Alice 按照如下方式生成一個 $\{0,1\}^{64}$ 上的排列。

  • 若 $b=0$,則 $P$ 從 $\{0,1\}^{64}$ 上的所有排列中均勻隨機抽取;
  • 若 $b=1$:
    • $f_1,f_2,f_3$ 分別從 $\{0,1\}^{32}\to\{0,1\}^{32}$ 的所有函數中獨立均勻隨機抽取;
    • 為了計算 $P(x)$,Alice 首先將 $x$ 拆分為 $x=x_0\circ x_1$,$x_0,x_1$ 各長 32 bit;
    • Alice 進行如下的計算:
      • $x_2=x_0\oplus f_1(x_1)$
      • $x_3=x_1\oplus f_2(x_2)$
      • $x_4=x_2\oplus f_3(x_3)$
    • Alice 將 $x_3\circ x_4$ 作為 $P(x)$ 的輸出。換言之,$P(x_0\circ x_1)=x_3\circ x_4$,其中 $x_3,x_4$ 的定義方式如上。

不難看出對於兩種情況,得到的 $P$ 都是一個良定義的排列。現在 Bob 需要確定 $b$ 的值。他可以向 Alice 進行如下兩種詢問:

  • Bob 任取 $x\in\{0,1\}^{64}$,並詢問 $P(x)$
  • Bob 任取 $x\in\{0,1\}^{64}$,並詢問 $P^{-1}(x)$

由於 Alice 還要去趕 DDL,她要求 Bob 只能進行不超過 $256$ 次詢問。然而 Bob 並不擅長應對隨機化的問題,於是他來向你尋求幫助。請幫 Bob 設計一個演算法來正確猜測 $b$。

互動

本題僅支援 c++。你應當在你提交的源文件中包含 interaction.h(見下發文件)。互動所使用的介面函數定義如下:

/**
 * @brief Queries P(x) or P^{-1}(x)
 * @param x The value to query
 * @param rev Set rev=0 to query P(x), rev=1 to query P^{-1}(x)
 * @return P(x) for rev=0, P^{-1}(x) for rev=1
 */
unsigned long long getperm(unsigned long long x,int rev);
/**
 * @brief Make no more than 256 calls to getperm, to guess b
 * @see getperm
 * @return The b you guesses
 */
int guessb();

你應當在你的源文件中實現 int guessb()。它應當通過調用 getperm 來進行不超過 $256$ 次詢問,並返回 $0/1$ 作為其對 $b$ 的猜測。如果有不清楚的地方,下發文件中的 permutation.cpp 是一個簡易實現,你可以在其基礎上進行修改得到你的源文件。

編譯與執行

使用

g++ -o grader grader.cpp permutation.cpp

來將你的源文件和互動庫一起編譯,並得到可執行文件 grader。其中 permutation.cpp 是你本地的源文件名。

隨後對於 Linux/MacOS,使用

./grader

來執行;對於 Windows,使用

grader.exe

來執行。

輸入格式

每一個測試點包含多組數據

第一行包含一個正整數 $t$,表示數據的組數。

第二行包含兩個 64 位無符號整數 $s_0,s_1$($0\le s_0,s_1\le 2^{64}-1$),代表 grader 所使用的隨機數種子。

這是 grader.cpp 的工作。你不應當在提交的源文件中從標準輸入讀取任何數據。

輸出格式

對於每組數據輸出一行。若調用了超過 256 次詢問,則輸出 QLE。否則若對於 $b$ 的猜測結果正確,則輸出 OK;若猜測錯誤,則輸出 WA

這是 grader.cpp 的工作。你不應當在提交的源文件中向標準輸出寫入任何數據。

資料範圍

對於 20% 的測試點,$t=1$。

對於另外 20% 的測試點,$t=4$。

對於剩下 60% 的測試點,$t=100$。

我們保證 OJ 上 grader.cpp 處理 $25600$ 次詢問所需總時間不超過 10ms。

關於用整數表示二進位字串:我們約定整數的最低位對應第一個二進位字元,最高位對應最後一個二進位字元。因此,對於 $x=x_0\circ x_1$,$x_0$ 是 $x$ 的低 32 位,而 $x_1$ 是 $x$ 的高 32 位。例如,對於 x=0xFEDCBA9876543210,我們有 x_0=0x76543210 以及 x_1=0xFEDCBA98。對於 $x_3\circ x_4$ 同理。

說明

下發的 grader.cpp 僅供參考。在 OJ 上評測時,我們會使用不同的 grader.cpp(保證介面的功能相同)。你提交的源文件只能訪問 interaction.h 中提供的介面,而不應當試圖訪問 grader.cpp 的內部。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.