0% found this document useful (0 votes)
1K views22 pages

ICPC Asia West Finals Statements

The document contains a problem set for the 2025 ICPC Asia West Finals, detailing various algorithmic challenges including string construction, tree properties, array inversions, deletable subarrays, auction pricing, jagged arrays, LCM ranges, and majority graphs. Each problem includes specific constraints, input/output formats, and sample cases to illustrate the requirements. The problems are designed to test participants' skills in algorithm design and implementation under given time and memory limits.

Uploaded by

Md murad hosen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views22 pages

ICPC Asia West Finals Statements

The document contains a problem set for the 2025 ICPC Asia West Finals, detailing various algorithmic challenges including string construction, tree properties, array inversions, deletable subarrays, auction pricing, jagged arrays, LCM ranges, and majority graphs. Each problem includes specific constraints, input/output formats, and sample cases to illustrate the requirements. The problems are designed to test participants' skills in algorithm design and implementation under given time and memory limits.

Uploaded by

Md murad hosen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2025 ICPC Asia West Finals Problemset

Problem A
Construct uwu
Time limit: 4 seconds
Memory limit: 1.5 Gigabytes

Problem Description
You are given a positive integer N .

Construct a minimal length string S consisting of letters ’u’ and ’w’ only, satisfying the
following condition:
• The number of subsequences of S which equal "uwu" is exactly N . Note that a subsequence
need not be continuous.
It can be proven that at least one valid string exists. You have to find the shortest possible
string for each test. The input is generated in such a way that the sum of the lengths of the
minimal strings is at most 107 over all test cases.

Input Format
• The first line of input contains a single integer T , denoting the number of test cases.
• The first and only line of each test case contains N — the required number of subsequences.

Output Format
For each test case, output a minimal length string S with exactly N "uwu" subsequences.

Constraints
• 1 ≤ T ≤ 103
• 1 ≤ N ≤ 1018
• The sum of the minimum lengths of the required strings does not exceed 107 .

Samples
Sample Input 1
6
1
2
3
4
5
6

Sample Output 1
uwu
uwwu
uwwwu
uwwuu
uwwwwwu
uwwwuu

1
2025 ICPC Asia West Finals Problemset

Sample Explanation
Test Case 1: "uwu" has exactly one subsequence which equals "uwu", which is the whole string
itself.
Test Case 2: "uwwu" has 2 subsequences which equal "uwu", using 1-based indexing:
• The subsequence formed by indices (1, 2, 4).

• The subsequence formed by indices (1, 3, 4).

2
2025 ICPC Asia West Finals Problemset

Problem B
Counting Distance Arrays
Time limit: 2 seconds
Memory limit: 1.5 Gigabytes

Problem Description
Given a tree with N nodes numbered 1 to N , and a subset of nodes S, we define an array A of
size N in the following way:

• Ai = max(dist(x, i)).
x∈S

Here, dist(x, y) represents the number of edges on the unique shortest path between x and y.

There are 2N − 1 non-empty subsets S possible. Find the number of possible distinct arrays A
over all 2N − 1 choices of S. Since the answer may be large, output it modulo 998244353.

Input Format
• The first line of input contains a single integer T , denoting the number of test cases.

• Each test case consists of multiple lines of input.

– The first line of each test case contains N — the number of nodes in the tree.
– The next N − 1 lines each contain 2 integers u and v — representing an edge (u, v)
in the tree.

Output Format
For each test case, output on a new line the number of distinct arrays A possible over all 2N − 1
non-empty subsets S, modulo 998244353.

Constraints
• 1 ≤ T ≤ 104

• 2 ≤ N ≤ 2 · 105

• 1 ≤ u, v ≤ N

• The set of N − 1 input edges form a tree.

• The sum of N does not exceed 2 · 105 over all test cases.

3
2025 ICPC Asia West Finals Problemset

Samples
Sample Input 1
3
2
1 2
3
1 2
2 3
7
1 2
2 3
2 4
3 5
1 6
5 7

Sample Output 1
3
6
23

Sample Explanation
Test Case 1: There are 3 subsets S possible. Each of them produces a distinct result, enu-
merated as follows:

• S = {1}, the array A = [0, 1]

• S = {2}, the array A = [1, 0]

• S = {1, 2}, the array A = [1, 1]

4
2025 ICPC Asia West Finals Problemset

Problem C
Counting is Fun
Time limit: 2 seconds
Memory limit: 1.5 Gigabytes

Problem Description
You are given an integer S.

Let f (N ) denote the sum of inversion numbers† for all arrays A with positive integers, and
of length N with sum S.

Formally, f (N ) is the sum of inversion numbers for arrays satisfying the following conditions:
• |A| = N
• 1 ≤ Ai ≤ S and Ai are integers
PN
• i=1 Ai = S

Find the values of f (1), f (2), . . . , f (S) modulo 998244353.

†The inversion number of an array A is the number of pairs satisfying 1 ≤ i < j ≤ |A| and
Ai > Aj .

Input Format
The first and only line of input contains a single integer S.

Output Format
Print S integers, the values of f (1), f (2), ..., f (S) modulo 998244353.

Constraints
2 ≤ S ≤ 2 · 105

Samples
Sample Input 1
4

Sample Output 1
0 1 3 0

Sample Input 2
10

Sample Output 2
0 4 48 204 460 600 462 196 36 0

5
2025 ICPC Asia West Finals Problemset

Sample Explanation
Test case 1: The answers are computed as below:

• N = 1 : There is only one array [4]. This has no inversions.

• N = 2 : There are 3 arrays, [1, 3], [2, 2] and [3, 1]. There is 1 inversion in the first array,
and 0 in the others.

• N = 3 : There are 3 arrays, [1, 1, 2], [1, 2, 1] and [2, 1, 1]. The inversion numbers are 0, 1, 2
respectively. Hence, the sum is 3.

• N = 4 : Only one array [1, 1, 1, 1]. This has no inversions.

6
2025 ICPC Asia West Finals Problemset

Problem D
Deletable Subarrays
Time limit: 1 second
Memory limit: 1.5 Gigabytes

Problem Description
An array B is called deletable if and only if it can be made completely empty by using the
following operation several times:

• Choose 1 ≤ i < |B| such that Bi = Bi+1 , and remove both Bi and Bi+1 from the array.

Given an array A of length N , count the number of deletable subarrays. Formally, compute the
following sum:
N X
X N
[Is A[L, R] deletable?]
L=1 R=L
where A[L, R] represents the subarray [AL , AL+1 , . . . , AR ].

Input Format
• The first line of input contains a single integer T , denoting the number of test cases.

• Each test case consists of two lines of input.

– The first line of each test case contains one integer N .


– The second line contains N integers: A1 , A2 , . . . , AN .

Output Format
For each test case, output on a new line the number of deletable subarrays of A.

Constraints
• 1 ≤ T ≤ 104

• 2 ≤ N ≤ 2 · 105

• 1 ≤ Ai ≤ N

• The sum of N over all test cases does not exceed 2 · 105 .

7
2025 ICPC Asia West Finals Problemset

Samples
Sample Input 1
3
4
1 2 2 1
4
1 1 1 1
12
12 7 2 3 2 2 3 2 7 1 1 12

Sample Output 1
2
4
7

Sample Explanation
Test Case 1: The subarrays [1, 2, 2, 1] and [2, 2] are deletable.

Test Case 2: All even-length subarrays are deletable.

8
2025 ICPC Asia West Finals Problemset

Problem E
Greedy Prices
Time limit: 2 seconds
Memory limit: 1.5 Gigabytes

Problem Description
There are N items for sale at an auction. However, the auction is greedy and has made the
prices variable depending on how much it perceives you will be able to pay. If you have already
spent some amount of money, then it will increase the cost proportionally, as you are more likely
to be willing to buy the item even at a higher price.

Formally, the i-th item has 2 parameters Mi and Ci , and the cost of the item is Mi · X + Ci ,
where X is the amount of money you have already spent.
Each item can be bought at most once. You can decide the order of buying items.

You have to answer Q queries of the following form:


• If you had a budget of Pi , what is the maximum number of items you can buy?

Input Format
• The first line of input contains a single integer T , denoting the number of test cases.

• Each test case consists of multiple lines of input.

– The first line of each test case contains 2 integers — N and Q, the number of items
and the number of queries.
– The second line contains N integers — M1 , M2 , . . . , MN , the linear coefficients of the
items.
– The third line contains N integers — C1 , C2 , . . . , CN , the constant coefficients of the
items.
– The next Q lines each contain 1 integer — Pi , representing a budget query.

Output Format
For each test case, output Q integers - the answer to the queries in order.

Constraints
• 1 ≤ T ≤ 104

• 1 ≤ N, Q ≤ 2 · 105

• 0 ≤ Mi , Ci , Pi ≤ 109

• The sum of N and the sum of Q both do not exceed 2 · 105 over all test cases.

9
2025 ICPC Asia West Finals Problemset

Samples
Sample Input 1
2
3 4
1 1000 0
3 6 4
2
6
7
19
6 6
0 0 1 1 2 3
0 1 0 2 3 4
0
1
8
15
3
10000

Sample Output 1
0 1 2 3
2 3 4 5 4 6

Sample Explanation
Test Case 1: Here are the answers to the respective queries:

• Budget 2: Impossible to buy any item.

• Budget 6: We can buy any of the 3 items for the prices 3, 6, 4 respectively. However, we
cannot buy more than 1 item.

• Budget 7: First, buy the 1st item for a cost of 3, and then buy the 2nd item for 4.

• Budget 19: Buy all 3 of the items in the following order:

– X = 0, Buy item 2 for a cost of 6.


– X = 6, Buy item 1 for a cost of 1 · 6 + 3 = 9.
– X = 15, Buy item 3 for a cost of 0 · 15 + 4.
– We bought all 3 items spending exactly 19.

10
2025 ICPC Asia West Finals Problemset

Problem F
Jagged Operations
Time limit: 1 second
Memory limit: 1.5 Gigabytes

Problem Description
An array A of size N is called jagged if and only if there exists some triplet of indices i, j, and
k (1 ≤ i < j < k ≤ N ) such that Ai > Aj and Aj < Ak .

You are given an array A and you want to make it not jagged. To do this, you can perform
the following operation as many times as needed:

• Choose L, R, X such that 1 ≤ L ≤ R ≤ N and X is an integer, then add X to all indices


between L and R.

Formally, set Ai ← Ai + X for all L ≤ i ≤ R.

Find the minimum number of operations needed.

Input Format
• The first line of input contains a single integer T , denoting the number of test cases.

• Each test case consists of two lines of input.

– The first line of each test case contains N — the size of the array.
– The second line contains N integers — A1 , A2 , . . . , AN .

Output Format
For each test case, output on a new line the minimum number of operations needed.

Constraints
• 1 ≤ T ≤ 104

• 3 ≤ N ≤ 2 · 105

• 1 ≤ Ai ≤ 109

• The sum of N does not exceed 2 · 105 over all test cases.

11
2025 ICPC Asia West Finals Problemset

Samples
Sample Input 1
2
3
1 1 1
3
2 1 2

Sample Output 1
0
1

Sample Explanation
Test Case 1: The array is already not jagged.

Test Case 2: The initial array is jagged as (i, j, k) = (1, 2, 3) satisfies the conditions.
We can choose L = 2, R = 2, X = 1, and modify the array to [2, 2, 2]. This is not jagged.

12
2025 ICPC Asia West Finals Problemset

Problem G
LCM on Range
Time limit: 4 seconds
Memory limit: 1.5 Gigabytes

Problem Description
Given an integer N , you need to find a range [L, R] satisfying the following conditions:

• 1 ≤ L < R ≤ 1018

• LCM(L, L+1, L+2, . . . , R−1, R) = N , where LCM represents the least common multiple.

If no such range exists, output −1 instead.

Input Format
• The first line of input contains a single integer T , denoting the number of test cases.

• The first and only line of each test case contains N — the target value.

Output Format
For each test case, output the following:

• If a valid range L, R exists, output the integers L and R satisfying 1 ≤ L < R ≤ 1018 .

• Otherwise, output −1.

Constraints
• 1 ≤ T ≤ 105

• 1 ≤ N ≤ 1018

Samples
Sample Input 1
4
1
2
6
12

Sample Output 1
-1
1 2
1 3
1 4

13
2025 ICPC Asia West Finals Problemset

Sample Explanation
Test Case 1: There exists no valid range. Note that L = R = 1 is not valid as we need L < R.

Test Case 2: LCM(1, 2) = 2.

14
2025 ICPC Asia West Finals Problemset

Problem H
Majority Graph
Time limit: 5 seconds
Memory limit: 1.5 Gigabytes

Problem Description
You are given an array A with N elements - A1 , A2 , . . . , AN .

Construct a graph G on N nodes in the following way:

• Draw an edge (i, j) if and only if the following conditions are satisfied:

– 1≤i<j≤N
– The subarray A[i, j] has a majority element.

Find the number of connected components of G.

The subarray A[i, j] refers to the array [Ai , Ai+1 , . . . , Aj ].


An array B of length M has a majority element if and only if there is some element X that
occurs strictly more than M 2 times in B.

Input Format
• The first line of input will contain a single integer T , denoting the number of test cases.

• Each test case consists of two lines of input.

– The first line of each test case contains a single integer N - the size of the array and
graph.
– The second line contains N integers - A1 , A2 , . . . , AN .

Output Format
For each test case, output on a new line the number of connected components of the graph G.

Constraints
• 1 ≤ T ≤ 105

• 2 ≤ N ≤ 2 · 106

• 1 ≤ Ai ≤ N

• The sum of N over all test cases does not exceed 2 · 106 .

• Note that the constraint on N is higher than usual.

15
2025 ICPC Asia West Finals Problemset

Samples
Sample Input 1
4
4
1 2 1 2
5
1 2 3 2 1
2
1 1
3
2 2 1

Sample Output 1
2
4
1
1

Sample Explanation
Test Case 1: There are 2 pairs (i, j) that satisfy the majority condition: (1, 3) and (2, 4). The
subarray A[1, 3] has 1 as a majority element, and A[2, 4] has 2. Thus, the graph has 2 connected
components.

Test Case 2: There is only one edge (2, 4). Hence, there are 4 connected components.

16
2025 ICPC Asia West Finals Problemset

Problem I
Suffix Array
Time limit: 1 second
Memory limit: 1.5 Gigabytes

Problem Description
You are given a string S of length N .

The suffix strings of S are the non-empty strings which can be formed by deleting some (possibly
0) characters from the beginning of S, i.e. for each i (1 ≤ i ≤ N ), the substring Si Si+1 . . . SN
is a suffix of S.

There are N − 1 suffix strings of S that have length ≥ 2.


Your task is to sort these suffix strings of S in lexicographic increasing† order and then print
the 2nd characters of each of the strings.

† A string A is lexicographically smaller than B if any of these conditions hold:

• There exists i, 1 ≤ i ≤ min(|A|, |B|) such that Ai < Bi , and Aj = Bj for all 1 ≤ j < i.

• |A| < |B| and for all 1 ≤ i ≤ |A|, Ai = Bi .

Input Format
• The first line of input will contain a single integer T , denoting the number of test cases.

• Each test case consists of two lines of input.

– The first line of each test case contains a single integer N - the length of the string.
– The second line contains a string S of length N .

Output Format
For each test case, output the 2nd characters of the sorted suffix strings of S (without spaces
between them).

Constraints
• 1 ≤ T ≤ 104

• 2 ≤ N ≤ 2 · 105

• |S| = N

• S only contains lowercase Latin characters.

• The sum of N over all test cases does not exceed 2 · 105 .

17
2025 ICPC Asia West Finals Problemset

Samples
Sample Input 1
3
4
asia
4
west
6
abbaab

Sample Output 1
sai
ste
abbab

Sample Explanation
Test Case 1: There are 3 suffix strings of S with length ≥ 2, namely asia, sia, and ia.

The sorted order is asia, ia, sia. The 2nd characters in this order are s, a, and i. Hence, the
output is sai.

18
2025 ICPC Asia West Finals Problemset

Problem J
Tri-Knight
Time limit: 1 second
Memory limit: 1.5 Gigabytes

Problem Description
Have you ever imagined how a chess knight will move on a triangular grid? A triangular grid
is a grid where each point has 6 neighbors, formed in 60◦ angles. The coordinates are written
with respect to two axes, and in this problem, the two axes have an inner angle of 120◦ .

(X, Y ) is the point that is reached by taking X steps of unit length along the horizontal axis,
and then Y steps along the oblique axis. Please check the diagram.

The points adjacent to (X, Y ) are (X +1, Y ), (X +1, Y +1), (X, Y +1), (X −1, Y ), (X −1, Y −1),
and (X, Y − 1).

On the grid, a tri-knight’s movement on one point A is defined as follows.

• First, it selects a point B adjacent to A.

• Then, it moves to a point C adjacent to B, so that the angle ̸ ABC is obtuse.

The above picture shows the grid and the coordinate system, and how one can move to (3, 3)
in 2 moves. Note that the grid extends infinitely in all directions (including negative).

Given a point (X, Y ) on an infinite triangular grid, please find the minimum number of
movements required for a tri-knight to move from (0, 0) to (X, Y ). If the tri-knight is never
able to visit the given point, report −1.

Input Format
• The first line of input will contain a single integer T , denoting the number of test cases.

• The first and only line of each test case contains 2 integers, X and Y — representing the
coordinates in the triangular grid system.

19
2025 ICPC Asia West Finals Problemset

Output Format
For each test case, output the following:

• If the tri-knight can visit (X, Y ), output the minimum moves it needs.

• Otherwise, output −1.

Constraints
• 1 ≤ T ≤ 104

• −109 ≤ X, Y ≤ 109

Samples
Sample Input 1
5
3 3
1 1
1 -1
-3 -3
-2 998244353

Sample Output 1
2
-1
1
2
665496236

Sample Explanation
Test Case 1: As written in the statement, the tri-knight can reach (3, 3) in 2 moves by going
to (2, 1) and then (3, 3).

Test Case 2: It can be proven that it is impossible to reach (1, 1).

Test Case 3: The tri-knight can reach the point (1, −1) in 1 move. For this, A = (0, 0) initially,
it chooses B (adjacent to A) as (−1, 0) and then the point C (adjacent to B) as (−1, 1). Note
that here, ̸ ABC = 120◦ which is obtuse.

20
2025 ICPC Asia West Finals Problemset

Problem K
Unique Subsequences Counting
Time limit: 1 second
Memory limit: 1.5 Gigabytes

Problem Description
An array A of length N is said to be good if and only if every subsequence of length M appears
at most once in the array.

For example, A = [1, 2, 2] is not good with length parameter M = 2, because the subsequence
[1, 2] appears twice. On the other hand, it would be good with length parameter M = 3.

Given N and M , count the number of arrays A satisfying the following conditions:

• |A| = N .

• 1 ≤ Ai ≤ N .

• A is good.

Since the answer may be large, output it modulo 109 + 8. Please note the unusual modulo.

Input Format
• The first line of input will contain a single integer T , denoting the number of test cases.

• The first and only line of each test case contains 2 integers N and M .

Output Format
For each test case, output on a new line the number of sequences satisfying the above constraints
modulo 109 + 8.

Constraints
• 1 ≤ T ≤ 104

• 2 ≤ M ≤ N ≤ 109

Samples
Sample Input 1
2
2 2
50 25

Sample Output 1
4
837455472

21
2025 ICPC Asia West Finals Problemset

Sample Explanation
Test Case 1: There are 4 arrays satisfying all conditions:

• [1, 1]

• [1, 2]

• [2, 1]

• [2, 2]

22

You might also like