#P16542. [EGOI 2026] 人口普查 / Census
[EGOI 2026] 人口普查 / Census
Background
Because Luogu does not support interactive problems with communication among 100 processes, this problem provides a communication library but cannot be judged properly.
Problem Description
A little-known fact about Cesenatico is that there is a secret society living here, consisting of female informatics scientists. This society is very secretive; the members do not know each other. Each member has a unique ID: a non-negative integer .
The only way members can communicate is indirectly, by writing numbers with chalk at different locations in town. Every 100 years, the society conducts a population census to count how many members there are. After the census ends, each member should know the total number of members in the society.
The census lasts for multiple days. On each day, every member who is still participating in the process must choose and perform exactly one action: read, write, or stop participating.
-
If a member chooses read, she selects a location . During the day, she visits that location and reads the number written there.
-
If a member chooses write, she selects a location and a number . In the evening, she visits that location and changes the number there to . Because it is already late, she cannot read the old number before writing the new one.
-
If a member chooses stop, she will take no actions on subsequent days (i.e., she no longer participates in the process).
If a member sees another member writing a number, she might recognize the other person. Therefore, the society strictly forbids two or more members from choosing to write at the same location on the same day. (There is no such restriction for reading, because reading can be done discreetly.)
If one or more members read a location on a day when another member intends to write a new number to that same location, all reads happen before the write.
How should the society plan the census process so that the number of days needed for everyone to learn the correct total number of members is minimized?
Implementation Details
This is a communication (interactive) problem. Your program will have an unknown number () of instances running simultaneously. Each instance simulates one member of the society.
There are locations. A location must satisfy . Initially, the value written at every location is .
A newly written value must always be an integer satisfying . In most subtasks, can only be or . See the “Scoring” section for more details.
When an instance of your program starts, it should first read one line containing two integers and (): the unique ID of the member represented by this instance, and the total number of possible IDs. In each testdata, all instances will receive the same value of and different values of . Note that some IDs might not be assigned to any member.
Then, for each day of the census process, your program should choose the action it wants to perform and output one corresponding line:
| Action | Meaning |
|---|---|
| Read location . After outputting this line, your program should read one line containing the current value written at . | |
| Write the new value at location . If multiple instances write to the same on the same day, you will receive a Not correct verdict. Except for the samples and subtask , you must output ; see the “Scoring” section for details. | |
| Answer and stop: report that there are members and stop participating in the census. After answering, your program should exit normally. (Note that other instances of your program may continue running for a few more days.) |
If any instance of your program answers with an incorrect value of , violates the protocol, uses more than days, or exceeds the (per-process) time/memory limits, your submission will be judged as Not correct.
Otherwise, your program will be judged as (Partially) Correct on the testdata, and it will be scored based on the value (the maximum number of days taken by any instance to answer). To get full score, you need to solve every testdata with and . See the “Scoring” section for details.
Flush your output. If you do not use the provided template, make sure to flush standard output after printing each line, otherwise your program may be judged as Not correct. In Python, if you use input() to read lines, this happens automatically. In C++, cout << endl; flushes in addition to printing a newline; if you use printf, use fflush(stdout).
Input Format
Output Format
Hint
Samples
The first sample. Each pair of columns shows the interaction between the judge program and one instance.
:::align{center}
:::
The second sample.
:::align{center}
:::
Sample Explanation
First sample. The society has members, with IDs , and (for subtasks 1, 3, and 4). Instance corresponds to the member whose ID is . The interaction above is just one possible legal sequence of actions; it does not mean this is an efficient or reasonable strategy. It is only used to demonstrate how interaction works.
Second sample. The society has members, with IDs 0 and 3, and (for subtasks 2, 3, and 4). On day 1, the member with ID 0 writes 0 at location 0 (no change), and the member with ID 3 writes 1 at location 2.
:::align{center}
:::
On day 2, the member with ID 0 writes 1 at location 1, and the member with ID 3 reads the same location. Note that reading happens during the day, before the evening write. Therefore, the member with ID 3 still sees 0.
:::align{center}
:::
On day 3, they both read location 2, which contains 1.
On day 4, the member with ID 0 answers that there are members (correct), while the member with ID 3 reads the 1 at location 1. The member with ID 0 exits immediately after that and no longer participates in the following days.
Finally, on day , the remaining member also answers correctly with .
Constraints
- .
- .
- You can use at most days.
Scoring
Your program will be tested on testdata split into several subtasks. To receive the score for a subtask, you must solve all testdata in that subtask correctly.
- Subtask 0 [ points]: Samples (you may write any integer ).
- Subtask 1 [ points]: , and the members have IDs .
- Subtask 2 [ points]: .
- Subtask 3 [ points]: , and you may write any integer .
- Subtask 4 [ points]: No additional constraints.
In subtasks 1, 2, and 4, in each write operation you can only write or .
Let be the maximum score for subtask (as above), and let be the maximum number of days used by your program on the tests of subtask . Then:
$$\begin{aligned} \text{score}_s = &\begin{cases} X_s & \text{if } D_s \le 61 \\ X_s \cdot (0.2 + 0.8 \cdot 1.01^{(60-D_s)}) & \text{if } 61 < D_s \le 500 \\ 0 & \text{if } 500 < D_s. \end{cases} \end{aligned}$$The value of is rounded to the nearest integer for each subtask, and your total score is the sum of these scores. To get full score for this problem, you need and for every testdata.
:::align{center}
:::
Total score, assuming each subtask is solved with the same maximum .
Testing
To make it easier to test your solution, we provide a simple tool. Using this tool is optional. Note that Luogu’s judge is different from this testing tool.
To use the tool, you need an input file. You can use the provided sample inputs census.input0.txt and census.input1.txt, or create your own. The input file should start with a line containing the number of members and the number of possible IDs , followed by a line containing numbers specifying the IDs of the society members.
For a Python program, assumed to be census.py (typically run as pypy3 census.py), run the testing tool as follows:
python3 testing_tool.py pypy3 census.py < census.input0.txt
For a C++ program, first compile your solution:
g++ -DEVAL -std=gnu++20 -O2 -pipe -static -s -o census census.cpp
Then run the testing tool:
python3 testing_tool.py ./census < census.input0.txt
Note that in this problem, standard output is used to interact with the judge, so it should not be used for debugging. Instead, you can use standard error output (stderr). In C++, you can use cerr << msg << endl;. In Python, you can use print(msg, file=sys.stderr).
The testing tool will read and display these stderr messages, as well as all queries made by all instances of your program. Note that for technical reasons, these messages may be slightly out of sync.
Translated by ChatGPT 5