luogu#P7950. [✗✓OI R1] 后方之水

[✗✓OI R1] 后方之水

Background

This problem has no background, because I am a saint and also the right seat of god.

Problem Description

Define a process of merging stones as follows: there is a row of nn piles of stones, and the ii-th pile has ai(ai1)a_i(a_i\ge1) stones. Each time, you may choose two adjacent piles to merge. Suppose their sizes are x,yx,y, then you will get one pile of (x+y)(x+y) stones, and you need to pay a cost of xyxy. In the end, all piles must be merged into one pile. Let f(a1,,an)f(a_1,\ldots,a_n) be the minimum cost to merge these piles of stones.

Given the total number of stones SS, compute

ai=Sf(a1,,an)\sum_{\sum a_i=S}f(a_1,\ldots,a_n)

and output the answer modulo 998244353998244353.

Input Format

This problem contains multiple test cases.

The first line contains an integer TT, the number of test cases.
The next TT lines each contain two integers n,Sn,S.

Output Format

Output TT lines, each containing one integer, the answer modulo 998244353998244353.

3
2 6
3 5
4 7
35
45
336
5
182565 710825096
429580 541341177
741770 757408347
461909 941427258
114514 1919810

487324711
256967112
352532743
962265551
926494516

Hint

[Sample Explanation]

Explanation for the first test case in the sample:
The partitions are (1,5),(2,4),(3,3),(4,2),(5,1)(1,5),(2,4),(3,3),(4,2),(5,1), for a total of 55 cases.
The answer is $1\times 5 + 2 \times 4 + 3 \times 3 + 4 \times 2 + 5 \times 1 = 35$.

[Constraints]

Test Point ID nn SS
1,2 15\le15
3,4 40\le40
5,6,7 70\le70
8,9 200\le200
10,11 2000\le2000
12,13,14 106\le10^6
15 =2=2 109\le10^9
16,17,18 2000\le2000
19~25 106\le10^6

For 100%100\% of the testdata, 1T51\le T\le5, 1n1061\le n\le10^6, 1S1091\le S\le10^9.

Translated by ChatGPT 5