Knowee
Questions
Features
Study Tools

Initially, we had one array, which was a permutation of size n๐‘› (an array of size n๐‘› where each integer from 11 to n๐‘› appears exactly once).We performed q๐‘ž operations. During the i๐‘–-th operation, we did the following:choose any array we have with at least 22 elements;split it into two non-empty arrays (prefix and suffix);write two integers li๐‘™๐‘– and ri๐‘Ÿ๐‘–, where li๐‘™๐‘– is the maximum element in the left part which we get after the split, and ri๐‘Ÿ๐‘– is the maximum element in the right part;remove the array we've chosen from the pool of arrays we can use, and add the two resulting parts into the pool.For example, suppose the initial array was [6,3,4,1,2,5][6,3,4,1,2,5], and we performed the following operations:choose the array [6,3,4,1,2,5][6,3,4,1,2,5] and split it into [6,3][6,3] and [4,1,2,5][4,1,2,5]. Then we write l1=6๐‘™1=6 and r1=5๐‘Ÿ1=5, and the arrays we have are [6,3][6,3] and [4,1,2,5][4,1,2,5];choose the array [4,1,2,5][4,1,2,5] and split it into [4,1,2][4,1,2] and [5][5]. Then we write l2=4๐‘™2=4 and r2=5๐‘Ÿ2=5, and the arrays we have are [6,3][6,3], [4,1,2][4,1,2] and [5][5];choose the array [4,1,2][4,1,2] and split it into [4][4] and [1,2][1,2]. Then we write l3=4๐‘™3=4 and r3=2๐‘Ÿ3=2, and the arrays we have are [6,3][6,3], [4][4], [1,2][1,2] and [5][5].You are given two integers n๐‘› and q๐‘ž, and two sequences [l1,l2,โ€ฆ,lq][๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž] and [r1,r2,โ€ฆ,rq][๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž]. A permutation of size n๐‘› is called valid if we can perform q๐‘ž operations and produce the given sequences [l1,l2,โ€ฆ,lq][๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž] and [r1,r2,โ€ฆ,rq][๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž].Calculate the number of valid permutations.InputThe first line contains two integers n๐‘› and q๐‘ž (1โ‰คq<nโ‰ค3โ‹…1051โ‰ค๐‘ž<๐‘›โ‰ค3โ‹…105).The second line contains q๐‘ž integers l1,l2,โ€ฆ,lq๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž (1โ‰คliโ‰คn1โ‰ค๐‘™๐‘–โ‰ค๐‘›).The third line contains q๐‘ž integers r1,r2,โ€ฆ,rq๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž (1โ‰คriโ‰คn1โ‰ค๐‘Ÿ๐‘–โ‰ค๐‘›).Additional constraint on the input: there exists at least one permutation which can produce the given sequences [l1,l2,โ€ฆ,lq][๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž] and [r1,r2,โ€ฆ,rq][๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž].OutputPrint one integer โ€” the number of valid permutations, taken modulo 998244353998244353.ExamplesinputCopy6 36 4 45 5 2outputCopy30inputCopy10 1109outputCopy1814400inputCopy4 124outputCopy8

Question

Initially, we had one array, which was a permutation of size n๐‘› (an array of size n๐‘› where each integer from 11 to n๐‘› appears exactly once).We performed q๐‘ž operations. During the i๐‘–-th operation, we did the following:choose any array we have with at least 22 elements;split it into two non-empty arrays (prefix and suffix);write two integers li๐‘™๐‘– and ri๐‘Ÿ๐‘–, where li๐‘™๐‘– is the maximum element in the left part which we get after the split, and ri๐‘Ÿ๐‘– is the maximum element in the right part;remove the array we've chosen from the pool of arrays we can use, and add the two resulting parts into the pool.For example, suppose the initial array was [6,3,4,1,2,5][6,3,4,1,2,5], and we performed the following operations:choose the array [6,3,4,1,2,5][6,3,4,1,2,5] and split it into [6,3][6,3] and [4,1,2,5][4,1,2,5]. Then we write l1=6๐‘™1=6 and r1=5๐‘Ÿ1=5, and the arrays we have are [6,3][6,3] and [4,1,2,5][4,1,2,5];choose the array [4,1,2,5][4,1,2,5] and split it into [4,1,2][4,1,2] and [5][5]. Then we write l2=4๐‘™2=4 and r2=5๐‘Ÿ2=5, and the arrays we have are [6,3][6,3], [4,1,2][4,1,2] and [5][5];choose the array [4,1,2][4,1,2] and split it into [4][4] and [1,2][1,2]. Then we write l3=4๐‘™3=4 and r3=2๐‘Ÿ3=2, and the arrays we have are [6,3][6,3], [4][4], [1,2][1,2] and [5][5].You are given two integers n๐‘› and q๐‘ž, and two sequences [l1,l2,โ€ฆ,lq][๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž] and [r1,r2,โ€ฆ,rq][๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž]. A permutation of size n๐‘› is called valid if we can perform q๐‘ž operations and produce the given sequences [l1,l2,โ€ฆ,lq][๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž] and [r1,r2,โ€ฆ,rq][๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž].Calculate the number of valid permutations.InputThe first line contains two integers n๐‘› and q๐‘ž (1โ‰คq<nโ‰ค3โ‹…1051โ‰ค๐‘ž<๐‘›โ‰ค3โ‹…105).The second line contains q๐‘ž integers l1,l2,โ€ฆ,lq๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž (1โ‰คliโ‰คn1โ‰ค๐‘™๐‘–โ‰ค๐‘›).The third line contains q๐‘ž integers r1,r2,โ€ฆ,rq๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž (1โ‰คriโ‰คn1โ‰ค๐‘Ÿ๐‘–โ‰ค๐‘›).Additional constraint on the input: there exists at least one permutation which can produce the given sequences [l1,l2,โ€ฆ,lq][๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž] and [r1,r2,โ€ฆ,rq][๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž].OutputPrint one integer โ€” the number of valid permutations, taken modulo 998244353998244353.ExamplesinputCopy6 36 4 45 5 2outputCopy30inputCopy10 1109outputCopy1814400inputCopy4 124outputCopy8

...expand
๐Ÿง Not the exact question you are looking for?Go ask a question

Solution

Para resolver este problema,

Similar Questions

You are given an array ๐ดA containing ๐‘N integers.Consider the following process:Let ๐‘†=0S=0 initially.For each ๐‘–i from 11 to ๐‘N in order, update ๐‘†S to either (๐‘†+๐ด๐‘–)(S+A iโ€‹ ) or (๐‘†ร—๐ด๐‘–)(Sร—A iโ€‹ ).That is, either add ๐ด๐‘–A iโ€‹ to ๐‘†S or multiply ๐‘†S by ๐ด๐‘–A iโ€‹ .Before performing the process, you're allowed to freely rearrange the elements of ๐ดA as you like.If you choose the rearrangement of ๐ดA and the sequence of operations optimally, what's the maximum possible value of ๐‘†S that you can obtain?This maximum value can be very large, so print it modulo 109+710 9 +7.Input FormatThe 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 number of elements in the array.The second line contains ๐‘N space-separated integers ๐ด1,๐ด2,โ€ฆ,๐ด๐‘A 1โ€‹ ,A 2โ€‹ ,โ€ฆ,A Nโ€‹ - the elements of the array.Output FormatFor each test case, output on a new line the maximum possible value of ๐‘†S, modulo 109+710 9 +7.Constraints1โ‰ค๐‘‡โ‰ค1031โ‰คTโ‰ค10 3 1โ‰ค๐‘โ‰ค2โ‹…1051โ‰คNโ‰ค2โ‹…10 5 1โ‰ค๐ด๐‘–โ‰ค1091โ‰คA iโ€‹ โ‰ค10 9 The sum of ๐‘N over all test cases won't exceed 2โ‹…1052โ‹…10 5 .Sample 1:InputOutput244 2 5 231 2 1804Explanation:Test case 11: Choose the rearrangement ๐ด=[2,2,5,4]A=[2,2,5,4]. Then,Add ๐ด1=2A 1โ€‹ =2 to ๐‘†S. Now, ๐‘†=2S=2.Add ๐ด2=2A 2โ€‹ =2 to ๐‘†S. Now, ๐‘†=4S=4.Multiply ๐‘†S by ๐ด3=5A 3โ€‹ =5. Now, ๐‘†=20S=20.Multiply ๐‘†S by ๐ด4=4A 4โ€‹ =4. Now, ๐‘†=80S=80.This is the maximum value that can be obtained.Test case 22: Choose any rearrangement and sum up all the numbers to get 1+1+2=41+1+2=4.This is the maximum value that can be obtained.

You are given two integer arrays: array a๐‘Ž of length n๐‘› and array b๐‘ of length n+1๐‘›+1.You can perform the following operations any number of times in any order:choose any element of the array a๐‘Ž and increase it by 11;choose any element of the array a๐‘Ž and decrease it by 11;choose any element of the array a๐‘Ž, copy it and append the copy to the end of the array a๐‘Ž.Your task is to calculate the minimum number of aforementioned operations (possibly zero) required to transform the array a๐‘Ž into the array b๐‘. It can be shown that under the constraints of the problem, it is always possible.InputThe first line contains a single integer t๐‘ก (1โ‰คtโ‰ค1041โ‰ค๐‘กโ‰ค104)ย โ€” the number of test cases.Each test case consists of three lines:the first line contains a single integer n๐‘› (1โ‰คnโ‰ค2โ‹…1051โ‰ค๐‘›โ‰ค2โ‹…105);the second line contains n๐‘› integers a1,a2,โ€ฆ,an๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘› (1โ‰คaiโ‰ค1091โ‰ค๐‘Ž๐‘–โ‰ค109);the third line contains n+1๐‘›+1 integers b1,b2,โ€ฆ,bn+1๐‘1,๐‘2,โ€ฆ,๐‘๐‘›+1 (1โ‰คbiโ‰ค1091โ‰ค๐‘๐‘–โ‰ค109).Additional constraint on the input: the sum of n๐‘› over all test cases doesn't exceed 2โ‹…1052โ‹…105.OutputFor each test case, print a single integerย โ€” the minimum number of operations (possibly zero) required to transform the array a๐‘Ž into the array b๐‘.ExampleinputCopy3121 323 33 3 344 2 1 22 1 5 2 3outputCopy318NoteIn the first example, you can transform a๐‘Ž into b๐‘ as follows: [2]โ†’[2,2]โ†’[1,2]โ†’[1,3][2]โ†’[2,2]โ†’[1,2]โ†’[1,3].

The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.You are given an array a๐‘Ž of length n๐‘›. Start with c=0๐‘=0. Then, for each i๐‘– from 11 to n๐‘› (in increasing order) do exactly one of the following:Option 11: set c๐‘ to c+ai๐‘+๐‘Ž๐‘–.Option 22: set c๐‘ to |c+ai||๐‘+๐‘Ž๐‘–|, where |x||๐‘ฅ| is the absolute value of x๐‘ฅ.Let the maximum final value of c๐‘ after the procedure described above be equal to k๐‘˜. Find k๐‘˜.InputThe first line contains a single integer t๐‘ก (1โ‰คtโ‰ค1041โ‰ค๐‘กโ‰ค104)ย โ€” the number of test cases.The first line of each test case contains a single integer n๐‘› (2โ‰คnโ‰ค2โ‹…1052โ‰ค๐‘›โ‰ค2โ‹…105).The second line of each case contains n๐‘› integers a1๐‘Ž1, a2๐‘Ž2, a3๐‘Ž3, โ€ฆโ€ฆ, an๐‘Ž๐‘› (โˆ’109โ‰คaiโ‰ค109โˆ’109โ‰ค๐‘Ž๐‘–โ‰ค109).The sum of n๐‘› over all test cases does not exceed 3โ‹…1053โ‹…105.OutputFor each test case, output a single integerย โ€” the value of k๐‘˜.ExampleinputCopy5410 -9 -3 481 4 3 4 1 4 3 43-1 -2 -34-1000000000 1000000000 1000000000 100000000041 9 8 4outputCopy6246400000000022NoteIn the first test case, if we set c๐‘ to its absolute value every time we add to it, we end up with 66. It can be shown that this is the maximum result.In the second test case, taking the absolute value will never change anything, so we can just sum the array without doing anything to get 2424.In the third test case, it is optimal to wait until the end to set c๐‘ to its absolute value, resulting in an answer of 66.

For a permutation ๐‘ƒP of size ๐‘N, and an integer ๐‘˜ย (1โ‰ค๐‘˜โ‰ค๐‘)kย (1โ‰คkโ‰คN), define ๐‘“(๐‘ƒ,๐‘˜)f(P,k) as โˆ‘๐‘–=1๐‘โˆ’๐‘˜+1maxโก(๐‘ƒ๐‘–,๐‘ƒ๐‘–+1,๐‘ƒ๐‘–+2,โ€ฆ,๐‘ƒ๐‘–+๐‘˜โˆ’1)i=1โˆ‘Nโˆ’k+1โ€‹ max(P iโ€‹ ,P i+1โ€‹ ,P i+2โ€‹ ,โ€ฆ,P i+kโˆ’1โ€‹ ), i.e. the sum of maximum elements in all ๐‘˜k - sized subarrays.Let ๐‘”(๐‘,๐‘˜)g(N,k) denote the number of permutations ๐‘ƒP of size ๐‘N which have the maximum value of ๐‘“(๐‘ƒ,๐‘˜)f(P,k).Given an integer ๐‘N, find the value of ๐‘”(๐‘,๐‘˜)g(N,k) for each 1โ‰ค๐‘˜โ‰ค๐‘1โ‰คkโ‰คN.Since the value can be huge, print it modulo 109+710 9 +7.Note that a permutation of size ๐‘N consists of all elements from 11 to ๐‘N exactly once.Input FormatThe first line of input will contain a single integer ๐‘‡T, denoting the number of test cases.Each test case consists of a single integer ๐‘N - the size of the permutation.Output FormatFor each test case, output on a new line, ๐‘N space-separated integers denoting the value of ๐‘”(๐‘,๐‘˜)g(N,k) for each 1โ‰ค๐‘˜โ‰ค๐‘1โ‰คkโ‰คN, modulo 109+710 9 +7.Constraints1โ‰ค๐‘‡โ‰ค5001โ‰คTโ‰ค5001โ‰ค๐‘โ‰ค3โ‹…1051โ‰คNโ‰ค3โ‹…10 5 The sum of ๐‘N over all test cases does not exceed 3โ‹…1053โ‹…10 5 .Sample 1:InputOutput312312 26 2 6Explanation:Test case 11: There is only one possible permutation of size 11. Thus, ๐‘“([1],1)=1f([1],1)=1 and ๐‘”(1,1)=1g(1,1)=1.Test case 22:๐‘”(2,1)=2g(2,1)=2 as there are two permutations with maximum ๐‘“(๐‘ƒ,1)f(P,1)๐‘“([1,2],1)=1+2=3f([1,2],1)=1+2=3๐‘“([2,1],1)=2+1=3f([2,1],1)=2+1=3๐‘”(2,2)=2g(2,2)=2 as there are two permutations with maximum ๐‘“(๐‘ƒ,2)f(P,2)๐‘“([1,2],2)=2f([1,2],2)=2๐‘“([2,1],2)=2f([2,1],2)=2Test case 33: For ๐‘˜=1k=1 and ๐‘˜=3k=3, all permutations have the same value of ๐‘“(๐‘ƒ,๐‘˜)f(P,k), thus the answer is 66. However, for ๐‘˜=2k=2, the optimal permutations are [2,3,1][2,3,1] and [1,3,2][1,3,2] only.

You are given an array ๐ดA of length ๐‘N, and a positive integer ๐พK.It is guaranteed that 1โ‰ค๐ด๐‘–โ‰ค๐พ1โ‰คA iโ€‹ โ‰คK for every index ๐‘–i from 11 to ๐‘N.You can do the following at most once:Choose an index ๐‘–i (1โ‰ค๐‘–โ‰ค๐‘1โ‰คiโ‰คN) and a value ๐‘ฅx (1โ‰ค๐‘ฅโ‰ค๐พ1โ‰คxโ‰คK).Then, set ๐ด๐‘–:=๐‘ฅA iโ€‹ :=x.Find the maximum possible value of the sum of adjacent differences of ๐ดA after performing this operation at most once.That is, maximize the quantityโˆ‘๐‘–=1๐‘โˆ’1โˆฃ๐ด๐‘–โˆ’๐ด๐‘–+1โˆฃi=1โˆ‘Nโˆ’1โ€‹ โˆฃA iโ€‹ โˆ’A i+1โ€‹ โˆฃInput FormatThe 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 two space-separated integers ๐‘N and ๐พK โ€” the length of the array and the maximum allowed integer ๐พK, respectively.The second line contains ๐‘N space-separated integers ๐ด1,๐ด2,โ€ฆ,๐ด๐‘A 1โ€‹ ,A 2โ€‹ ,โ€ฆ,A Nโ€‹ , the elements of array ๐ดA.Output FormatFor each test case, output on a new line the answer: the maximum possible sum of adjacent differences of ๐ดA after replacing exactly one element.Constraints1โ‰ค๐‘‡โ‰ค1001โ‰คTโ‰ค1001โ‰ค๐‘โ‰ค10001โ‰คNโ‰ค10001โ‰ค๐พโ‰ค2โ‹…1061โ‰คKโ‰ค2โ‹…10 6 1โ‰ค๐ด๐‘–โ‰ค๐พ1โ‰คA iโ€‹ โ‰คKThe sum of ๐‘N across all tests won't exceed 10001000.Sample 1:InputOutput32 51 53 87 2 75 2018 3 1 4 1941263Explanation:Test case 11: It's best to leave the array unchanged, giving us a difference of โˆฃ1โˆ’5โˆฃ=4โˆฃ1โˆ’5โˆฃ=4.Test case 22: It's optimal to set ๐ด2:=1A 2โ€‹ :=1, giving us the array [7,1,7][7,1,7]. The sum of adjacent differences is 6+6=126+6=12.Test case 33: It's optimal to set ๐ด3:=20A 3โ€‹ :=20, to obtain [18,3,20,4,19][18,3,20,4,19]. The sum of adjacent differences is 6363.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.