#P8493. [IOI 2022] 数字电路
[IOI 2022] 数字电路
Background
Due to technical limitations, please do not use C++ 14 (GCC 9) to submit this problem.
This is an interactive problem. You only need to implement the functions required in the code.
Your code does not need to include any extra header files, and you do not need to implement the main function.
Since this problem has too many test points, considering the current Luogu judging implementation, this problem will not be scored by the subtasks given in the statement.
Problem Description
There is a digital circuit consisting of gates numbered from to . Gates to are threshold gates, and gates to are input gates.
Every gate except gate is an input of exactly one threshold gate. Specifically, for each with , gate is an input of gate , where . Importantly, it is guaranteed that . Also, assume . Each threshold gate has one or more inputs. Input gates have no inputs.
Each gate has a state, either or . The initial states of input gates are given by an array of integers. That is, for each with , the initial state of input gate is .
The state of each threshold gate depends on the states of its inputs as follows. First, each threshold gate is assigned a threshold parameter. For a threshold gate with inputs, the assigned parameter must be an integer between and (inclusive). Then, for a threshold gate with parameter , if at least of its input gates have state , the threshold gate’s current state is ; otherwise, its state is .
For example, suppose threshold gates and input gates. Gate has inputs gates and , gate has inputs gates , , and , and gate has only one input gate .
An illustration of this example is shown in the figure below.

Assume input gates and have state , while gates and have state . Suppose threshold gates , , are assigned parameters , , , respectively. In this case, gate has state , gate has state , and gate has state . The following figure shows the parameter assignment and the resulting states. Gates with state are colored black.

The states of input gates will go through updates. Each update is described by two integers and (), meaning to flip the states of all input gates with indices between and (inclusive). That is, for all with , if input gate has state it will be flipped to ; if it has state it will be flipped to . After being flipped, each gate stays in the new state until it is flipped again in some later update.
Your goal is to compute, after each update, how many assignments of threshold-gate parameters make gate have state . Two parameter assignments are considered different if at least one threshold gate has a different parameter. Since the number of assignments can be large, you need to output it modulo .
Note that in the example above, there are different assignments of threshold-gate parameters in total, because gates , , have , , inputs, respectively. Among these assignments, there are assignments that make gate have state .
Input Format
Your task is to implement the following two functions.
void init(int N, int M, int[] P, int[] A)
- : the number of threshold gates.
- : the number of input gates.
- : an array of length , describing the inputs of threshold gates.
- : an array of length , giving the initial states of input gates.
- This function is called exactly once, and it is called before any calls to
count_ways.
int count_ways(int L, int R)
- , : the states of input gates with indices between and will be flipped.
- This function should first perform the required update, then return the number of parameter assignments that make gate have state , modulo .
- This function will be called exactly times.
Output Format
Consider the following sequence of function calls:
init(3, 4, [-1, 0, 1, 2, 1, 1, 0], [1, 0, 1, 0])
The statement has already explained this example.
count_ways(3, 4)
This call flips the states of gates and , meaning gate becomes and gate becomes . The following shows two valid parameter assignments that can make gate have state .
| Assignment | Assignment |
|---|---|
![]() |
![]() |
In all other parameter assignments, gate has state . Therefore, the function should return .
count_ways(4, 5)
This call flips the states of gates and . As a result, all input gates have state , and for every parameter assignment, gate has state . Therefore, the function should return .
count_ways(3, 6)
This call sets all input gates to state . As a result, for every parameter assignment, gate has state . Therefore, the function should return .
Hint
Constraints
- .
- .
- .
- and (for all with ).
- Each threshold gate has at least one input (for every with , there exists an index such that and ).
- (for all with ).
- .
Subtasks
- (2 points) , , .
- (7 points) , , each threshold gate has exactly two inputs.
- (9 points) , .
- (4 points) , (for some positive integer ), (for all with ), .
- (12 points) , (for some positive integer ), (for all with ).
- (27 points) Each threshold gate has exactly two inputs.
- (28 points) .
- (11 points) No additional constraints.
Sample Grader
The sample grader reads input in the following format:
- Line : .
- Line : .
- Line : .
- Line (): for the -th update.
The sample grader prints your answers in the following format:
- Line (): the return value of
count_waysfor the -th update.
Conventions
When giving function interfaces, the statement uses generic type names void, bool, int, int[] (array), and union(bool, int[]).
In C++, the grader will use suitable data types or implementations as shown in the table below:
void |
bool |
int |
int[] |
|---|---|---|---|
void |
bool |
int |
std::vector<int> |
union(bool, int[]) |
Length of array a |
|---|---|
std::variant<bool, std::vector<int>> |
a.size() |
In C++, std::variant is defined in the header <variant>.
A function with return type std::variant<bool, std::vector<int>> can return either a bool or a std::vector<int>.
The following sample code shows three functions that return std::variant, and all of them work correctly:
std::variant<bool, std::vector<int>> foo(int N) {
return N % 2 == 0;
}
std::variant<bool, std::vector<int>> goo(int N) {
return std::vector<int>(N, 0);
}
std::variant<bool, std::vector<int>> hoo(int N) {
if (N % 2 == 0) {
return false;
}
return std::vector<int>(N, 0);
}
Translated by ChatGPT 5

