#P16433. [APIO 2026 中国赛区] 上升
[APIO 2026 中国赛区] 上升
Background
When submitting, please choose a language standard higher than C++17, and do not include the header file ascend.h.
Problem Description
Little N is a girl who likes things to be on an upward trend, because that often means good things are happening.
Because of this hobby, for a permutation of , Little N also likes to study the positions where it rises. Specifically, she defines the set of all rising positions in permutation as .
A rise is a lucky thing, but it is hard to measure exactly how lucky it is. So Little N decides to assign a weight to each position in the permutation to measure the lucky value. Specifically, she gives a non-negative integer sequence , and defines the lucky value of permutation as . In particular, if , then .
Little C is Little N’s good friend. One day, Little C gave her a lucky permutation . But due to various accidents, some elements in the permutation were lost, and the values at those missing positions became .
After receiving the gift, Little N was not sad about the permutation being incomplete, because she was surprised to find that: all elements at the non-missing positions in the permutation are still strictly increasing, meaning that from left to right they form a strictly increasing subsequence.
Little N immediately felt that she was the happiest girl in the world. At the same time, she was also curious about how lucky the original permutation given by Little C was. Therefore, she wants to compute the sum of over all permutations that match . A permutation matches if and only if: for all , we have or .
Your task is to help Little N compute the sum of the lucky values over all permutations that match .
Implementation Details
Contestants do not need to, and should not, implement the main function.
Contestants must ensure that the submitted program includes the header file ascend.h, i.e., add the following code at the beginning of the program:
#include "ascend.h"
Contestants need to implement the following function in the submitted source file ascend.cpp:
int ascend(int c, int n, int m, std::vector<int> p, std::vector<int> w);
- represent the test point ID and the length of the permutation, respectively. indicates that this test point is the sample.
- represents Little C’s permutation after some positions are missing. For , is the value at position in the permutation after missing.
- represents Little N’s weight sequence. For , is the weight of position .
- This function should return the sum of the lucky values over all permutations that match , modulo .
- For each test point, this function will be called by the grader exactly times.
How to Run the Test Program
Contestants can compile an executable program in this task directory using the following command:
g++ grader.cpp ascend.cpp -o ascend -O2 -std=c++14 -static
Input Format
For the compiled executable program:
- The executable will read input from standard input in the following format:
- The first line contains two non-negative integers , representing the test point ID and the number of testdata groups.
- Then follow the testdata groups. For each testdata group:
- The first line contains two positive integers .
- The second line contains non-negative integers .
- The third line contains non-negative integers .
Output Format
- The executable will output data to standard output in the following format:
- For each testdata group, output one line with one non-negative integer, which is the return value of the
ascendfunction.
- For each testdata group, output one line with one non-negative integer, which is the return value of the
0 1
3 6
0 2 0
2 3
1
Hint
Sample 1 Explanation
There are the following two permutations that match permutation :
- $q = [1, 2, 3], S(q) = \{1, 2\}, f(q) = w_1 \times w_2 = 2 \times 3 = 6$.
- .
Therefore, the sum of the lucky values over all permutations that match is , and the result modulo is .
Constraints
For all testdata, we have:
- .
- , .
- For all , .
- All non-zero elements in sequence form a strictly increasing subsequence.
- For all , .
::cute-table{tuack} |Test Point ID||Special Property| |:-:|:-:|:-:| |||None| |||^ | |||A| |||B| ||| ^| ||| ^| || ^|None|
- Special property A: For all , .
- Special property B: and is prime.
Translated by ChatGPT 5