#P9372. [APIO2023] Alice, Bob and Circuit
[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 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 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 (), 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 members (their names and their favorite numbers), but Alice does not know anything about the letters. She needs to send Circuit a binary string of length at most .
-
Bob knows all information about the letters (the names of the sender and recipient of each letter), but Bob does not know anything about the members. He needs to send Circuit a binary string of length at most .
-
Circuit is a circuit that can receive the binary strings sent by Alice and Bob, and then produce a binary string of 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 input gates, numbered from to in order. Here are the lengths of the binary strings provided by Alice and Bob, respectively.
- For any , the output of gate is the -th bit of the binary string provided by Alice.
- For any , the output of gate is the -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 .
- 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 (), 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 , then the output of this computation gate is:
Here are some examples that may be useful:
| AND () | OR () | XOR () | NOT () | ||
|---|---|---|---|---|---|
Implementation details
Please note:
- All array indices start from . For example, if
ais an array of lengthn, thena[0]througha[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 | |||
| names | The name of each member. | All member names are distinct. Names contain only lowercase English letters and have length at most . | ||
| numbers | Each member’s favorite number. | Favorite numbers are integers from to . | ||
| Output | outputs_alice | The binary string sent to Circuit. | ||
| (return value) | You must ensure does not exceed . Also, when is the same, 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 | |||
| senders | The sender name of each letter. | All names must appear in Alice’s input. | ||
| recipients | The recipient name of each letter. | |||
| Output | outputs_bob | The binary string sent to Circuit. | ||
| (return value) | You must ensure does not exceed . Also, when is the same, 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 | |||
| lb | ||||
| Output | operations | The operation type executed by each gate in the circuit. | Each element is an integer from to . | |
| 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 | The indices of the circuit’s output gates. | outputs_circuit[i][j] represents the -th bit (counting from the least significant bit) of the final stored number of member . Members are indexed in the order of Alice’s input. |
||
| (return value) | , representing the number of gates used in the circuit (including input gates). | You must ensure . |
Although you may modify the information of gates with indices smaller than 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 members. The member named
alichas favorite number , and so on. A possible output ofalice()is:- The return value of
alice()is , meaning . - In
alice(), setoutputs_alice[0] = 1,outputs_alice[1] = 0, meaning the binary string returned by Alice is .
- The return value of
-
Bob knows that there are letters. The first letter is sent from
alicand received bycirc, and so on. A possible output ofbob()is:- The return value of
bob()is , meaning . - In
bob(), setoutputs_bob[0] = 1,outputs_bob[1] = 1,outputs_bob[2] = 0, meaning the binary string returned by Bob is .
- The return value of
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 , meaning we added two computation gates, labeled and . - In
circuit(), set theoperations,operands, andoutputs_circuitarrays as follows:operations = {-1, -1, -1, -1, -1, 8, 14}. Here we use-1to 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; seeabc.cppin the attachment for the full array.
According to the output of circuit(), Circuit’s computation process is:
- Add a computation gate of type , numbered , whose inputs come from input gate and input gate . The output of gate is the -th bit of Alice’s string, i.e. ; the output of gate is the -nd bit of Bob’s string, i.e. . So the output of computation gate is .
- Add a computation gate of type , numbered , whose inputs come from input gate and computation gate . The output of gate is the -th bit of Bob’s string, i.e. ; the output of gate is . So the output of computation gate is .
output_circuit[0]represents the final stored number ofalic, i.e. . Sincealicreceived only one letter frombob,alic’s final result is .bob’s final stored number should be 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:
- .
- All member names are distinct, have maximum length , and consist only of lowercase English letters.
- Each member’s favorite number is an integer between and .
- All sender and recipient names appear in Alice’s input array
names. - The memory limit for both
alice()andbob()is MiB, and the time limit for both is seconds. - The memory limit for
circuit()is MiB, and the time limit is 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 seconds.
Subtasks
Subtask Type A (12 points)
Subtasks 1, 2, 3 are of Type A. Type A guarantees:
- .
Each subtask has the following additional constraints:
- Subtask 1 (4 points): .
- Subtask 2 (4 points): .
- Subtask 3 (4 points): .
Subtask Type B (54 points)
Subtasks 4, 5, 6 are of Type B. Type B guarantees:
- .
- 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): . All member names are single English letters, and in Alice’s input, the member names are in order from
atoz. - Subtask 5 (24 points): .
- Subtask 6 (6 points): No additional constraints.
Subtask Type C (34 points)
Subtasks 7, 8, 9 are of Type C. Type C guarantees:
- .
Each subtask has the following additional constraints:
- Subtask 7 (18 points): . 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): .
- Subtask 9 (6 points): No additional constraints.
Sample
Sample grader program
The sample grader program reads input in the following format:
- Line : .
- Line (): .
- Line (): .
The sample grader program prints your answer in the following format:
- If the program terminates successfully, the sample interactor will output 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.login the directory. - In addition, the sample interactor will output the values of 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 / are the same, / must be the same.
Hint
Translated by ChatGPT 5