#P9372. [APIO2023] Alice, Bob and Circuit

    ID: 20095 远端评测题 10000ms 2048MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>2023APIO交互题Special Judge通信题

[APIO2023] Alice, Bob and Circuit

Background

This is a communication problem and cannot be judged for now.

Problem Description

The Cyberland Circuit Society has a total of nn members. Each member has a favorite number and a unique name that is different from everyone else’s name. (It is not guaranteed that everyone’s favorite number is different.)

In total, these members have sent mm letters to each other. Each letter contains a sender and a recipient. The content of the letter is exactly the sender’s favorite number.

After all letters are sent, each member will compute the sum of the numbers contained in the letters they received (the senders’ favorite numbers), take this sum modulo 6553665536 (2162^{16}), and use the result as their final stored number.

Your task is to determine the final stored number for each person.

However, things are not as simple as they look. Alice, Bob, and Circuit plan to solve this problem in a slightly more complex way. Specifically:

  • Alice knows all information about the nn members (their names and their favorite numbers), but Alice does not know anything about the mm letters. She needs to send Circuit a binary string of length at most 10510^5.

  • Bob knows all information about the mm letters (the names of the sender and recipient of each letter), but Bob does not know anything about the nn members. He needs to send Circuit a binary string of length at most 10510^5.

  • Circuit is a circuit that can receive the binary strings sent by Alice and Bob, and then produce a binary string of 16n16n bits as its output. However, its computing power is limited: Circuit can only perform basic logical operations (such as AND, OR, NOT).

Below, we describe the working mechanism of the circuit in detail.

Circuit operation details

A gate is the basic component of the circuit. A gate has either zero or two boolean signals as input, and one boolean signal as output. There are two types of gates in the circuit: input gates and computation gates.

  • Input gates have no input boolean signals, and are used to represent one bit of the binary strings input by Alice and Bob.
    • There are a total of lA+lBl_A + l_B input gates, numbered from 00 to lA+lB−1l_A + l_B - 1 in order. Here lA,lBl_A, l_B are the lengths of the binary strings provided by Alice and Bob, respectively.
    • For any 0≤i<lA0\leq i < l_A, the output of gate ii is the ii-th bit of the binary string provided by Alice.
    • For any 0≤i<lB0\leq i < l_B, the output of gate i+lAi + l_A is the ii-th bit of the binary string provided by Bob.
  • Computation gates have two inputs and are used for computation.
    • The numbering of computation gates starts from lA+lBl_A + l_B.
    • For each computation gate, you need to provide the indices of two related gates, which represent the boolean signal inputs of this computation gate; and a number pp (0≤p≤150\leq p\leq 15), which represents the type of this computation gate.
      • To avoid circular dependencies, the two indices used as inputs must be smaller than the index of this computation gate.
      • Suppose the outputs of the two related gates are x0,x1x_0, x_1, then the output of this computation gate is:
$$f(p,x_0,x_1)=\left \lfloor \frac{p}{2^{x_0+2x_1}} \right \rfloor \bmod 2$$

Here are some examples that may be useful:

x0x_0 x1x_1 x0x_0 AND x1x_1(f(8,x0,x1)f(8,x_0,x_1)) x0x_0 OR x1x_1(f(14,x0,x1)f(14,x_0,x_1)) x0x_0 XOR x1x_1(f(6,x0,x1)f(6,x_0,x_1)) NOT x0x_0(f(5,x0,x1)f(5,x_0,x_1))
00 00 00 00 11
11 11 11 00
00 11 11
11 11 00

Implementation details

Please note:

  • All array indices start from 00. For example, if a is an array of length n, then a[0] through a[n-1] are valid elements of the array. Accessing an index outside this range may cause an out-of-bounds error.
  • All strings end with the null character \0.

You should implement the following procedures:

Alice

int alice(const int n, const char names[][5],
          const unsigned short numbers[], bool outputs_alice[]);
Type Value Length Meaning Constraints
Input n 11 nn 0≤n≤7000\leq n\leq 700
names nn The name of each member. All member names are distinct. Names contain only lowercase English letters and have length at most 44.
numbers Each member’s favorite number. Favorite numbers are integers from 00 to 6553565535.
Output outputs_alice lAl_A The binary string sent to Circuit.
(return value) 11 lAl_A You must ensure lAl_A does not exceed 10510^5. Also, when nn is the same, lAl_A must be the same.

Bob

int bob(const int m, const char senders[][5], 
        const char recipients[][5], bool outputs_bob[]);
Type Value Length Meaning Constraints
Input m 11 mm 0≤m≤10000\leq m\leq 1000
senders mm The sender name of each letter. All names must appear in Alice’s input.
recipients The recipient name of each letter.
Output outputs_bob lBl_B The binary string sent to Circuit.
(return value) 11 lBl_B You must ensure lBl_B does not exceed 10510^5. Also, when mm is the same, lBl_B must be the same.

Circuit

To make Circuit’s computation process the same as a normal circuit, you cannot directly access the binary strings that Alice and Bob send to Circuit. You only need to know the lengths of these two strings, and output the circuit structure based on the lengths.

Type Value Length Meaning Constraints
Input la 11 lAl_A
lb lBl_B
Output operations ll The operation type executed by each gate in the circuit. Each element is an integer from 00 to 1515.
operands The indices of the gates providing boolean signal inputs for each computation gate. Input gate indices are guaranteed to be smaller than the current computation gate index.
outputs_circuit nn The indices of the circuit’s output gates. outputs_circuit[i][j] represents the jj-th bit (counting from the least significant bit) of the final stored number of member ii. Members are indexed in the order of Alice’s input.
(return value) 11 ll, representing the number of gates used in the circuit (including input gates). You must ensure l≤2×107l\leq 2\times 10^7.

Although you may modify the information of gates with indices smaller than lA+lBl_A+l_B in the operations and operands arrays, the interactive library will ignore such modifications.

Example

Consider the following sample call sequence:

alice(3, {"alic", "bob", "circ"}, {10000, 20000, 30000}, outputs_alice); 
bob(5, {"alic", "bob", "bob", "circ", "circ"}, 
    {"circ", "circ", "alic", "circ", "circ"}, outputs_bob);

This sample represents the following scenario:

  • Alice knows that there are 33 members. The member named alic has favorite number 1000010000, and so on. A possible output of alice() is:

    • The return value of alice() is 22, meaning lA=2l_A=2.
    • In alice(), set outputs_alice[0] = 1, outputs_alice[1] = 0, meaning the binary string returned by Alice is 1010.
  • Bob knows that there are 55 letters. The first letter is sent from alic and received by circ, and so on. A possible output of bob() is:

    • The return value of bob() is 33, meaning lB=3l_B=3.
    • In bob(), set outputs_bob[0] = 1, outputs_bob[1] = 1, outputs_bob[2] = 0, meaning the binary string returned by Bob is 110110.

Based on the previous outputs of alice() and bob(), the interactive library will call:

circuit(2, 3, operations, operands, outputs_circuit);

One correct output of circuit() is:

  • The return value of circuit() is 77, meaning we added two computation gates, labeled 55 and 66.
  • In circuit(), set the operations, operands, and outputs_circuit arrays as follows:
    • operations = {-1, -1, -1, -1, -1, 8, 14}. Here we use -1 to represent ignored information from input gates.
    • operands = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {0, 4}, {2,5}}.
    • outputs_circuit = {{5, 5, 5, 5, 5, 6, 5, 5, 5, 6, 6, 6, 5, 5, 6, 5}, ...} . The array is long; see abc.cpp in the attachment for the full array.

According to the output of circuit(), Circuit’s computation process is:

  • Add a computation gate of type 88, numbered 55, whose inputs come from input gate 00 and input gate 44. The output of gate 00 is the 00-th bit of Alice’s string, i.e. 11; the output of gate 44 is the 22-nd bit of Bob’s string, i.e. 00. So the output of computation gate 55 is f(8,0,1)=0AND⁡1=0f(8,0,1)=0 \operatorname{AND} 1=0.
  • Add a computation gate of type 1414, numbered 66, whose inputs come from input gate 22 and computation gate 55. The output of gate 22 is the 00-th bit of Bob’s string, i.e. 11; the output of gate 55 is 00. So the output of computation gate 66 is f(14,1,0)=1OR⁡0=1f(14,1,0)= 1 \operatorname{OR} 0=1.
  • output_circuit[0] represents the final stored number of alic, i.e. (0100111000100000)2=20000(0100111000100000)_2 = 20000. Since alic received only one letter from bob, alic’s final result is 2000020000.
  • bob’s final stored number should be 00 because he did not receive any letters; circ’s final stored number should be $(10000 + 20000 + 30000 + 30000) \bmod 65536 = 24464$.

The attached abc.cpp can pass this sample, but we do not guarantee that it can pass other test cases.

Constraints

For all test cases:

  • 0≤n≤700,0≤m≤10000\leq n\leq 700,0\leq m\leq 1000.
  • All member names are distinct, have maximum length 44, and consist only of lowercase English letters.
  • Each member’s favorite number is an integer between 00 and 6553565535.
  • All sender and recipient names appear in Alice’s input array names.
  • The memory limit for both alice() and bob() is 20482048 MiB, and the time limit for both is 0.020.02 seconds.
  • The memory limit for circuit() is 20482048 MiB, and the time limit is 77 seconds.

In the final test, alice() and bob() may be called multiple times in a single test case. The time limit for each call is 0.020.02 seconds.

Subtasks

Subtask Type A (12 points)

Subtasks 1, 2, 3 are of Type A. Type A guarantees:

  • n=1n=1.

Each subtask has the following additional constraints:

  • Subtask 1 (4 points): m=0m=0.
  • Subtask 2 (4 points): 0≤m≤10\leq m\leq 1.
  • Subtask 3 (4 points): 0≤m≤10000\leq m\leq 1000.

Subtask Type B (54 points)

Subtasks 4, 5, 6 are of Type B. Type B guarantees:

  • 0≤n≤30,n2≤m≤n20\leq n\leq 30,\frac{n}{2}\leq m\leq n^2.
  • There do not exist two letters with the same sender and recipient.
  • All member names appear in Bob’s input (that is, each member either sent at least one letter or received at least one letter).

Each subtask has the following additional constraints:

  • Subtask 4 (24 points): n=26n = 26. All member names are single English letters, and in Alice’s input, the member names are in order from a to z.
  • Subtask 5 (24 points): n=26n = 26.
  • Subtask 6 (6 points): No additional constraints.

Subtask Type C (34 points)

Subtasks 7, 8, 9 are of Type C. Type C guarantees:

  • 0≤n≤700,0≤m≤10000\leq n\leq 700,0\leq m\leq 1000.

Each subtask has the following additional constraints:

  • Subtask 7 (18 points): n=676n=676. All member names are exactly two English letters, and in Alice’s input, member names are sorted in increasing lexicographical order (in other words, in the order aa, ab, ac, ..., az, ba, ..., bz, ca, ..., zz).
  • Subtask 8 (10 points): n=676n = 676.
  • Subtask 9 (6 points): No additional constraints.

Sample

Sample grader program

The sample grader program reads input in the following format:

  • Line 11: nmn\quad m.
  • Line 2+i2+i (0≤i≤n−10\leq i\leq n-1): namesinumbersinames_i\quad numbers_i.
  • Line 2+n+i2+n+i (0≤i≤m−10\leq i\leq m-1): sendersirecipientsisenders_i\quad recipients_i.

The sample grader program prints your answer in the following format:

  • If the program terminates successfully, the sample interactor will output nn lines, each containing one integer, representing the final stored number computed by your circuit for each member.
  • Otherwise, the sample interactor will not output anything to standard output, and will write an error message to the file abc.log in the directory.
  • In addition, the sample interactor will output the values of lA,lB,ll_A, l_B, l and the running time of each function to abc.log.

The sample grader program will not check the memory limit, and will not check the constraint that when nn / mm are the same, lAl_A / lBl_B must be the same.

Hint

Translated by ChatGPT 5