#P16433. [APIO 2026 中国赛区] 上升

    ID: 18482 远端评测题 4000ms 1024MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>动态规划 DPAPIO交互题动态规划优化容斥原理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 q1,q2,,qnq_1, q_2, \dots, q_n of 1n1 \sim n, Little N also likes to study the positions where it rises. Specifically, she defines the set of all rising positions in permutation qq as S(q)={1i<nqi<qi+1}S(q) = \{1 \le i < n \mid q_i < q_{i+1}\}.

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 w1,w2,,wn1w_1, w_2, \dots, w_{n-1}, and defines the lucky value of permutation qq as f(q)=iS(q)wif(q) = \prod_{i \in S(q)} w_i. In particular, if S(q)=S(q) = \varnothing, then f(q)=1f(q) = 1.

Little C is Little N’s good friend. One day, Little C gave her a lucky permutation p1,p2,,pnp_1, p_2, \dots, p_n. But due to various accidents, some elements in the permutation were lost, and the values at those missing positions became 00.

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 f(q)f(q) over all permutations qq that match pp. A permutation qq matches pp if and only if: for all 1in1 \le i \le n, we have pi=0p_i = 0 or qi=piq_i = p_i.

Your task is to help Little N compute the sum of the lucky values f(q)f(q) over all permutations qq that match pp.

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);
  • c,nc, n represent the test point ID and the length of the permutation, respectively. c=0c = 0 indicates that this test point is the sample.
  • pp represents Little C’s permutation after some positions are missing. For 0i<n0 \le i < n, pip_i is the value at position i+1i + 1 in the permutation after missing.
  • ww represents Little N’s weight sequence. For 0i<n10 \le i < n - 1, wiw_i is the weight of position i+1i + 1.
  • This function should return the sum of the lucky values f(q)f(q) over all permutations qq that match pp, modulo mm.
  • For each test point, this function will be called by the grader exactly tt 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 c,tc, t, 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 n,mn, m.
      • The second line contains nn non-negative integers p1,p2,,pnp_1, p_2, \dots, p_n.
      • The third line contains n1n - 1 non-negative integers w1,w2,,wn1w_1, w_2, \dots, w_{n-1}.

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 ascend function.
0 1
3 6
0 2 0
2 3
1

Hint

Sample 1 Explanation

There are the following two permutations qq that match permutation pp:

  1. $q = [1, 2, 3], S(q) = \{1, 2\}, f(q) = w_1 \times w_2 = 2 \times 3 = 6$.
  2. q=[3,2,1],S(q)=,f(q)=1q = [3, 2, 1], S(q) = \varnothing, f(q) = 1.

Therefore, the sum of the lucky values over all permutations that match pp is 6+1=76 + 1 = 7, and the result modulo m=6m = 6 is 11.

Constraints

For all testdata, we have:

  • 1t51 \le t \le 5.
  • 2n5002 \le n \le 500, 2m1092 \le m \le 10^9.
  • For all 1in1 \le i \le n, 0pin0 \le p_i \le n.
  • All non-zero elements in sequence pp form a strictly increasing subsequence.
  • For all 1in11 \le i \le n-1, 0wi<m0 \le w_i < m.

::cute-table{tuack} |Test Point ID|nn \le|Special Property| |:-:|:-:|:-:| |1,21,2|1010|None| |3,43,4|2020|^ | |575 \sim 7|500500|A| |8108 \sim 10|5050|B| |111311 \sim 13|150150| ^| |141814 \sim 18|500500| ^| |19,2019,20| ^|None|

  • Special property A: For all 1in1 \le i \le n, pi=0p_i = 0.
  • Special property B: m5×108m \ge 5 \times 10^8 and mm is prime.

Translated by ChatGPT 5