#P5418. [CTSC2016] NOIP十合一(数据疑似有误)

[CTSC2016] NOIP十合一(数据疑似有误)

Background

This is an output-only problem.

Problem Description

At the contest site of NOIP2044, Little D encountered the following problem:

You are given a graph with nn vertices, mm directed edges with weights, and qq queries. Each query asks: how many paths are there from vertex uu to vertex vv with total length ww? You only need to output the answer modulo PP.

Little D thought for a long time but still could not solve it. After leaving in disappointment, he obtained the testdata for this problem from the official website. There are 1010 test cases in total. Little D really wants to solve this problem, so he asks for your help and gives you the input of these 1010 test cases. You, being smart, can surely help Little D compute the output for each test case.

Input Format

The input files noip1.in~noip10.in are already included in the download package.

The first line of each input file contains 44 positive integers n,m,q,Pn,m,q,P, representing the number of vertices, the number of edges, the number of queries, and the modulus. The vertices are numbered as integers from 11 to nn.

The next mm lines describe the weighted directed edges. In the ii-th line there are 33 integers ai,bi,cia_i,b_i,c_i, meaning the ii-th edge starts at aia_i, ends at bib_i, and has weight cic_i.

The next qq lines describe the queries. In the kk-th line there are 33 integers uk,vk,wku_k,v_k,w_k, meaning the kk-th query asks for the number of paths from vertex uku_k to vertex vkv_k with total length wkw_k, modulo PP.

Output Format

The output files are noip1.out~noip10.out, corresponding to the respective input files.

Each output file contains at most qq lines. Each line contains a non-negative integer less than PP, representing the answers to the first qq queries of that test case.

3 4 2 10
1 1 2
1 2 2
1 3 1
2 3 3
1 3 5
1 3 3

2
1

Hint

Explanation for Sample 1:

For the first query, there are exactly two paths from vertex 11 to vertex 33 with length 55. Assume the edges are numbered from 11 to 44. Then the edges passed by these two paths are:

Path 11: 242 \rightarrow 4

Path 22: 1131 \rightarrow 1 \rightarrow 3.

About the other samples

The download package provides sampleout for each test case, which corresponds to the correct output for the first query of each test case.

Input data download

Download link

Scoring

There is no special judge. The judging follows the traditional problem scoring method. You get points for a test case only if your output is exactly the same as the standard output.

Notes

This problem may use the following knowledge:

Characteristic polynomial: For an n×nn \times n matrix AA, define an nn-th degree polynomial in λ\lambda as f(λ)=det(λIA)f(\lambda)=\det(\lambda I-A), where II is the n×nn \times n identity matrix and det\det is the determinant. We call f(λ)f(\lambda) the characteristic polynomial of AA.

Cayley-Hamilton theorem: For the characteristic polynomial f(λ)f(\lambda) of a square matrix AA, we always have f(A)=0f(A)=0. That is, if we substitute the matrix itself into the characteristic polynomial, the result is the zero matrix.

Translated by ChatGPT 5