Statements en
Statements en
Little A has a directed graph containing N points and M edges. For the i-th edge (ui , vi ), there are two
attributes (ai , bi ).
A path P is defined as a directed path from point 1 to point N , with its path weight given by:
! !
X X
ai × bi
i∈P i∈P
Little A is very eager to know what the minimum weight is among all possible paths from node 1 to node
N . Please help him calculate this minimum product weight.
Input
The first line contains a positive integer T (1 ≤ T ≤ 103 ), indicating the number of test cases.
For each test case, the first line contains two integers N, M (1 ≤ N ≤ 300, 1 ≤ M ≤ 103 ), representing
the number of points and edges in the graph, respectively.
The following M lines each contain four integers ui , vi , ai , bi (1 ≤ ui , vi ≤ N, ui 6= vi , 1 ≤ ai , bi ≤ 200),
representing the starting point, ending point, and weight attributes of a directed edge in the graph.
It is guaranteed that there exists at least one path from node 1 to node N.
It is guaranteed that the sum of N in each test case does not exceed 103 , and the sum of M does not
exceed 2 × 103 .
Output
For each test case, output the values of ai and bi corresponding to the path with the minimum
P P
i∈P i∈P
weight.
If there are multiple paths
P with the same minimum weight, please output the values corresponding to the
path with the smallest i∈P ai .
Example
standard input standard output
1 7 3
5 9
3 4 3 5
4 5 5 1
1 4 2 2
3 4 5 2
1 4 2 4
2 1 3 2
4 2 5 4
4 1 2 2
4 1 3 1
Page 1 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
Problem B. Ternary
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
an−1 , an−2 , · · · , a0
Then its encrypted form from the most significant to the least significant is:
To conveniently represent the encryption of each digit, we can denote the bijection
fi = {0 → x, 1 → y, 2 → z} (where x, y, z ∈ {0, 1, 2} and x, y, z are distinct) simply as fi = xyz. For
example, f0 = {0 → 1, 1 → 2, 2 → 0} can be represented as f0 = 120.
For instance, if Little A constructs the bijections f0 = 021, f1 = 120, f2 = 210 for a 3-digit ternary
number, then the encryption of 120 results in 100, and the encryption of 201 results in 012.
Of course, Little A does not want others to know the bijections he constructed, but the encryption
algorithm must be used, so Little A will tell you the size of n and allow you to inquire about the results
of addition in the encrypted state twice. Your task is to use these 2 inquiries to decipher the n bijections
f0 , f1 , · · · , fn−1 constructed by Little A.
Input
The first line contains a positive integer T (1 ≤ T ≤ 104 ), indicating the number of test cases.
Each test case consists of a single line with a positive integer n (1 ≤ n ≤ 105 ), representing the number
of ternary digits.
It is guaranteed that the sum of n in a single test case does not exceed 106 .
Interaction Protocol
You can make at most 2 inquiries, after which you must answer Little A’s encryption mappings.
For each inquiry, you should provide the two parameters a, b for addition in the format ? a b, where a, b
are both n-digit ternary numbers and must be provided in string format. After flushing the output stream,
you need to read a line containing an n + 1-digit ternary number c in string format, where c is the result
of adding the decrypted forms of a and b, followed by encryption, with the most significant digit serving
as an overflow flag that is not encrypted. Formally:
c = f (f −1 (a) + f −1 (b))
Page 2 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
flushing the output stream, the interactor will immediately determine whether your answer is correct, and
then proceed to the next test case or end the program without any extra output.
Note that Little A’s encryption mappings are fully determined before the inquiries and will not change
with the inquiries. In other words, the interactor is not adaptive.
To flush the output stream, you can:
• Use fflush(stdout) (if using printf) or cout.flush() (if using cout) in C/C++.
Example
standard input standard output
2 ? 011 102
3
? 010 202
0210
! 021 120 102
1102
? 0 1
1
! 012
01
Note
For the first inquiry of the first test case, 011 and 102 decrypt to 102 and 021, respectively, and their sum
is 0200, which encrypts to 0210.
For the second inquiry of the first test case, 010 and 202 decrypt to 100 and 221, respectively, and their
sum is 1021, which encrypts to 1102.
For the first inquiry of the second test case, 0 and 1 decrypt to 0 and 1, respectively, and their sum is 01,
which encrypts to 01.
Page 3 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
In simple terms, S(x) is the union of all divisors of x and all multiples of x that do not exceed N .
Assume your current state is x. Every second, you have two choices:
• Do nothing.
Input
The first line contains two positive integers N, Q (1 ≤ N, Q ≤ 105 ), representing the size of the value
range and the number of queries.
The next Q lines each contain a positive integer x (1 ≤ x ≤ N ), representing a single query.
Output
For each query, you need to output a real number.
If the absolute or relative error compared to the correct answer does not exceed 10−6 , it will be considered
a correct answer.
Examples
standard input standard output
3 2 1.7500000000
3 1.5000000000
2
1000 2 4.7506538205
114 3.7750763456
514
Page 4 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
Input
The first line contains a positive integer T (1 ≤ T ≤ 300), indicating the number of test cases.
For each test case, the first line contains a positive integer n (1 ≤ n ≤ 300), representing the length of
the permutation.
The second line contains n integers q1 , q2 , · · · , qn (1 ≤ qi ≤ n), with the same meaning as described in the
problem. It is guaranteed that qi 6= qj for i 6= j, meaning that q is a permutation.
Output
For each test case, output a single line containing an integer representing the answer, modulo 998244353.
Example
standard input standard output
2 102
5 96
1 2 3 4 5
5
1 3 2 4 5
Page 5 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
Input
The first line contains two positive integers n, k (2 ≤ n, k ≤ 105 ).
Output
Output a single integer representing the answer, taken modulo 998244353.
Examples
standard input standard output
3 2 0
3 3 2
Page 6 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
There is an integer sequence of length n, denoted as a1 , a2 , · · · , an , where the integers in the sequence can
only be 0 or 1, but some positions in the sequence have not yet been determined.
Now you want to fill the undetermined positions in the sequence with 0 or 1 in such a way that the number
of inversion pairs is maximized.
The number of inversion pairs in a sequence a1 , a2 , · · · , an is defined as the number of integer pairs (i, j)
such that 1 ≤ i < j ≤ n and ai > aj .
Input
The first line contains a positive integer T (1 ≤ T ≤ 104 ), indicating the number of test cases.
For each test case, the first line contains an integer n (2 ≤ n ≤ 106 ), representing the length of the
sequence.
The second line contains a string s of length n consisting only of 0, 1, and ?, representing the sequence,
where the i-th character si being ? indicates that ai is unknown and needs to be filled in, otherwise it
represents ai .
It is guaranteed that the sum of n across all test cases does not exceed 2 × 106 .
Output
For each test case, output a single integer on a new line, representing the maximum number of inversion
pairs.
Example
standard input standard output
4 2
3 2
110 4
3 8
1?0
4
????
7
1?0?0?1
Note
For the first sample case, the number of inversion pairs in 110 is 2.
For the second sample case, it can be filled as 100 or 110, both yielding 2 inversion pairs.
For the third sample case, it can be filled as 1100, resulting in 4 inversion pairs.
For the fourth sample case, it can be filled as 1100001 or 1101001, both yielding 8 inversion pairs.
Page 7 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
n
aji × xi ≡ tj
X
(mod 998244353).
i=1
Output the sequence x1 , x2 , · · · , xn . Note that for each i = 1, 2, · · · , n, the value xi must satisfy
0 ≤ xi < 998244353.
The data guarantees that the solution is unique.
Input
The first line contains two positive integers n, t (1 ≤ n ≤ 5 × 104 , 1 ≤ t < 998244353).
The second line contains n positive integers, representing a1 , a2 , . . . , an in order (1 ≤ ai < 998244353).
Output
Output one line containing n non-negative integers representing x1 , x2 , · · · , xn . Note that for each
i = 1, 2, · · · , n, the value xi must satisfy 0 ≤ xi < 998244353.
The solution is guaranteed to be unique.
Example
standard input standard output
2 3 998244352 2
1 2
Page 8 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
Problem H. Hututu
Input file: standard input
Output file: standard output
Time limit: 3 seconds
Memory limit: 256 megabytes
Input
The first line contains a positive integer T (1 ≤ T ≤ 106 ), indicating the number of test cases.
For each test case, there is a line with four integers x, y, X, Y (−109 ≤ x, y, X, Y ≤ 109 ), representing a
query.
Output
For each test case, output a line with a single integer representing the answer.
Example
standard input standard output
3 0
1 2 1 2 2
1 1 3 4 49
1 1 98 98
Page 9 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
One day, Little A received several strings, but he did not know the specific meanings of these strings. To
uncover the mystery, he decided to start with the essentially different suffixes of the strings and calculate
some interesting information.
A suffix of a string is a segment that starts from a certain position and goes to the end. For example, the
suffixes of the string abc include: abc, bc, c.
He wants to know how many essentially different suffixes there are.
Input
The first line contains a positive integer N (1 ≤ N ≤ 3 × 105 ), indicating the number of strings received
by Little A.
The next N lines each contain a string Si composed of lowercase English letters, representing one of the
strings received by Little A.
It is guaranteed that |Si | ≤ 3 × 105 .
P
Output
Output a single line containing an integer that represents the number of essentially different suffixes.
Example
standard input standard output
3 22
fbudpefs
cfhjoofs
edpouftu
Page 10 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
Input
The first line contains a positive integer T (1 ≤ T ≤ 104 ), indicating the number of test cases.
For each test case, the first line contains an integer n (1 ≤ n ≤ 106 ), representing the number of nodes in
the tree.
The second line contains a string S of length n, composed only of uppercase English letters, where the
i-th character Si is the character attached to the i-th node of the tree.
The next n − 1 lines each contain two integers xi , yi (1 ≤ xi , yi ≤ n, xi 6= yi ), indicating that there is an
edge connecting nodes xi and yi in the tree.
It is guaranteed that the sum of n across all test cases does not exceed 2 × 106 .
Output
For each test case, output a single integer on a new line representing the number of simple paths.
Example
standard input standard output
2 1
5 3
SCCPC
1 2
2 3
3 4
4 5
7
SCCPCCC
1 2
2 3
3 4
4 5
4 6
4 7
Page 11 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
For a tree T with n vertices (without a root), numbered 1, 2, . . . , n, Little A operates on it according to
the permutation p1 , p2 , . . . , pn to obtain a rooted tree T 0 as follows:
1. Find the vertex x in the tree T that appears first in the permutation p1 , p2 , . . . , pn .
3. The remaining vertices in T form several connected components T1 , T2 , . . . , Tk (where k may be 0),
each of which is still an unrooted tree. For each unrooted tree Ti , operate on it to obtain the rooted
tree Ti0 .
4. Add each rooted tree Ti0 to T 0 and set the parent of the root of Ti0 to be x.
Now given a tree T and the operation permutation p1 , p2 , . . . , pn , Little A wants to find the parent of each
vertex in the rooted tree T 0 obtained after operating on T according to the permutation p1 , p2 , . . . , pn .
Input
The first line contains a positive integer T (1 ≤ T ≤ 104 ), indicating the number of test cases.
For each test case, the first line contains an integer n (1 ≤ n ≤ 105 ) representing the number of vertices
in the tree.
The second line contains n integers p1 , p2 , . . . , pn (1 ≤ pi ≤ n, ∀i 6= j, pi 6= pj ), representing the
permutation.
The next n − 1 lines each contain two integers x, y (1 ≤ x, y ≤ n, x 6= y), representing an edge in the tree.
It is guaranteed that the sum of n across all test cases does not exceed 106 .
Output
For each test case, output a line with n integers, where the i-th integer represents the parent of vertex i
in the rooted tree T 0 obtained after the operation. If vertex i is the root, then its parent is denoted by 0.
Page 12 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
Example
standard input standard output
3 2 0 2
3 2 0 1 2 2
2 3 1 0 1 1 2 2
1 2
2 3
5
2 1 4 5 3
1 2
1 3
2 4
2 5
5
1 2 3 4 5
1 2
1 3
2 4
2 5
Note
For the first sample case, first p1 = 2, so the root of T 0 is 2, and T is divided into connected components
T1 = {2}, T2 = {3}. Thus, both 2 and 3 have 2 as their parent in T 0 .
For the second sample case, first p1 = 2, so the root of T 0 is 2, and T is divided into connected components
T1 = {1, 3}, T2 = {4}, T3 = {5}. Both T2 and T3 are trees consisting of single vertices, so both 4 and 5
have 2 as their parent in T 0 ; for T1 = {1, 3}, since 1 appears earlier in the sequence p (p2 = 1, p5 = 3),
the root of T10 is 1, so the parent of 1 in T 0 is 2, and the parent of 3 in T 0 is 1.
For the third sample case, first p1 = 1, so the root of T 0 is 1, and T is divided into connected components
T1 = {2, 4, 5}, T2 = {3}. T2 is a tree consisting of a single vertex, so the parent of 3 in T 0 is 1; for
T1 = {2, 4, 5}, since 2 appears earlier in the sequence p (p2 = 2, p4 = 4, p5 = 5), the root of T10 is 2, so the
parent of 2 in T 0 is 1. Continuing to split, 4 and 5 each form separate subtrees, and their parents in T 0
are both 2.
Page 13 of 14
The 2025 Sichuan Provincial Collegiate Programming Contest
China, Sichuan, June, 8, 2025
Problem L. abc
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Little A became very interested in the Atcoder Beginner Contest one day, so he decided to create a
problem that only involves abc.
Given a string S of length N that contains only the characters a, b, and c.
Define the value of a string S, denoted as val(S), to be the count of the most frequently occurring character
in S minus the count of the least frequently occurring character in S. Note that the count of the least
frequently occurring character in S is not 0, meaning that the least frequently occurring character must
appear in S.
For example, the value of aaa is 0, the value of aab is 1, and the value of abccc is 2.
He wants to know:
N X
X N
val(S[i, j])
i=1 j=i
where the substring S[i, j] represents the string formed by the i-th character to the j-th character of the
string S.
Little A now wants to participate in the Atcoder Beginner Contest, so he asks you to help him answer
this question.
Input
The first line contains an integer N (1 ≤ N ≤ 2 × 105 ), representing the length of the string.
The second line contains a string S of length N , guaranteed to contain only the characters a, b, and c.
Output
Output a single integer, representing the answer.
Example
standard input standard output
5 8
baaca
Note
The value of S[1, 3] is 1, the value of S[1, 4] is 1, the value of S[1, 5] is 2, the value of S[2, 4] is 1, the value
of S[2, 5] is 2, and the value of S[3, 5] is 1, so the total value is 8.
Page 14 of 14