Блог пользователя awoo

Автор awoo, история, 23 месяца назад, По-русски

2043A - Преобразование монет

Идея: BledDest

Разбор
Решение (BledDest)

2043B - Цифры

Идея: AcidWrongGod

Разбор
Решение (BledDest)

2043C - Суммы на отрезках

Идея: Ferume

Разбор
Решение (awoo)

2043D - Задача про НОД

Идея: Ferume

Разбор
Решение (BledDest)

2043E - Преобразование матриц

Идея: Ferume

Разбор
Решение (BledDest)

2043F - Ним

Идея: Ferume

Разбор
Решение (awoo)

2043G - Задача на запросы

Идея: Ferume

Разбор
  • Проголосовать: нравится
  • +67
  • Проголосовать: не нравится

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится -47 Проголосовать: не нравится

second

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится +12 Проголосовать: не нравится

F can also be solved using divide & conquer.

If the recursion is at range [L, R], choose mid = (L + R) / 2 and answer all queries [ql, qr] such that ql <= tm and qr > tm.

This can be easily done using 2 DPs, one from mid to L, and one from mid + 1 to R and merging them.

Time complexity: O(NlogN * 63)

Submission link: 298444046

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится +21 Проголосовать: не нравится

Problem F can also be solved using divide and conquer.

If we are at range [L, R] with the recursion, let mid = (L + R) / 2. We will answer all queries [ql, qr] where ql <= mid < qr.

This can be easily done using 2 knapsacks, one from mid to L and one from mid+1 to R.

Submission link

  • »
    »
    23 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    very educational solution. thank you my bro

  • »
    »
    23 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    could you explain me sum of segments

    • »
      »
      »
      23 месяца назад, скрыть # ^ |
       
      Проголосовать: нравится +8 Проголосовать: не нравится

      Of course.

      First of all, notice that the number that is not -1 or 1 is there to make the problem harder, so let's think how would we solve the problem if all numbers were 1 or -1.

      I claim that if the minimum subarray sum is L, and the maximum subarray sum is R, than all sums in range [L, R] can be formed. The proof is in 2 parts:

      • Sums [0, R] can be made

      Let the shortest segment with the maximum sum be [x, y]. If you remove the first or last element of the segment while you can in any order(example below), you will get to an empty segment with sum 0. Because the elemets are 1 or -1, to get to the empty segment, we have to pass a segment with sum 1. The same argument can be made for all sums up to R.

      Example: 1 1 -1 1 1

      Here the subarray with maximum sum is [1, 1, -1, 1, 1] and the sum is 3. If we remove the first element the segment will be [1, -1, 1, 1] with sum 2. Now if we remove the last 1, the segment will be [1, -1, 1] with sum 1. Let's remove the first 1 again, we will have [-1, 1] sum 0. Remove the 1 to have [-1] with sum -1. And finally remove the -1.

      Notice that we saw every possible sum from 0 to 3, and consider the -1 we saw simply as a bonus. Any order of operations will lead to all sums in [0, R] to be seen.

      • The proof for [L, 0] is similar.

      Now we will consider the acutal problem. The biggest bottleneck is the number that is not 1 or -1. We need to find a way to find the possible sums of subarrays that pass through that element.

      Consider the example [1, -1, 10, 1, 1]. Let's break the array in 2 parts, one with all elements before the 10, one with elements after it. For example, [1, -1] and [1, 1]. Now choosing a subarray with tha 10 in it is simply choosing some suffix(empty allowed) from the first array , some prefix(empty allowed) from the second and appending the 10 between them. So, all sums will be in the form $$$suf + 10 + pref$$$. Since 10 is constant, we want to maximize and minimize $$$suf + pref$$$. To maximize, simply take the bigeest suffix and prefix, and to minimize take the minimums.

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I didn't found divisibility rule for 7 anywhere how it was figured out in problem B??

»
23 месяца назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

i want to explain easier approach of E but bad englis, you can check my sub tho

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

There is still a greedy way to pass tests in E by applying an operation when needed and running it min(n,m) times. Meaning complexity would be $$$O(tnm*min(n,m))$$$

Submission

I would like to either see it hacked or disproven. Unless that is correct which i highly doubt as I wasn't able to prove it.

  • »
    »
    23 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I solved exactly like this, would be very nice if someone prove or hack it

  • »
    »
    23 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I know right? I just solved it performing necessary operations on each row and column once, and i repeat it 100 times. It passes all the tests.

  • »
    »
    23 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    $$$min(n,m)$$$ seems logical, because you need space for a cycle. Let $$$min(m,n)=n$$$. Proof may be something like when $$$n=1$$$ it's obviously correct. For 2, maximum cycle is when setting some bits in one row breaks in another, and vice versa. For 3 same thing is for triangle, and so on.

    When in doubt detect cycle using hash: 298400755

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

11?

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For the problem E tutorial, can anyone help me with the bound on the number of edges in the operation graph?

I wrote a brute-force solution that passed with complexity $$$O(tnm*min(n,m)*\log{A})$$$.

»
23 месяца назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

In 2043B - Цифры definition of first way, I think there is a mistake.

(123 − 456 + 9) is -324, and it's not divisible by 7 because -324 % 7 is 5.

I think it must be (569 − 234 + 1). {(569−234+1) = 336 % 7 = 0}

awoo

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

2043F - Ним. Nah, I've forgotten the dp way for finding xor = 0, and I wrote meet in the middle, because we can remove all the elements except for 7. So I can test if I can leave exactly 1 element, 2 elements, ..., 6 elements. And to check if I can leave 6 elements, I can check a pair of (3, 3) elements. 298456482

  • »
    »
    23 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    can you explain this one bro? How did you arrive at this "because we can remove all the elements except for 7"?

    • »
      »
      »
      23 месяца назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Because the size of the vector basis can't be more than 6 because $$$a_i \le 50$$$.

    • »
      »
      »
      23 месяца назад, скрыть # ^ |
       
      Проголосовать: нравится +1 Проголосовать: не нравится

      Or another way to prove it, is that the maximum xor you can achieve by only using numbers in the range $$$[0; 50]$$$ is $$$63$$$, and the minimum one is $$$0$$$, so you can have at most $$$64$$$ different numbers. If you have $$$7$$$ numbers such that for every subset its xor is unique, then you will have $$$2^7 = 128$$$ different numbers; that is a contradiction. * If for subsets $$$A, B$$$ their xors are equal, then you can find $$$C = A \, \Delta \, B$$$ (symmetric difference) whose xor is $$$0$$$.

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

F is a cool problem

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem F, we can divide it into some easy sub-problems, We will calculate the minimum number of elements to get 0 xor from the subarray:

  1. If we have 0 in the range, the answer always r-1, freq(0)
  2. If we have an element with frequency >=2, the answer always r-l-1, sum of freq(i) *freq(i-1)/2
  3. Now the size of the subarray <=50 because no element exist 2 times and more ( piegonhole principle), Now we can prove that at most we will select 8 elements from the range ( using the xor bases), we can find the answer using 2d dp with state (Number of taken elements, current xor) which is very similar to knpasack.

The solution is a bit slow but I think there are some nice obeservations here , Nice problem !

solution: 298471356

»
23 месяца назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

For problem D: "This should mean that it is divisible by at least 15 primes which are greater than 37" Why? This one number can fix many pairs, by it, and the numbers it pairs with, all having 41 (say) as a prime divisor. The argument can be made to work, but what is written is I think wrong. Please clarify.

»
23 месяца назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

awoo Ferume BledDest

In problem G you said that : Unfortunately, it is practically impossible to fit this within time limits, so let's try to improve the solution.

But when I submit this solution with B=1300, it has got an accepted with time 7700ms

https://codeforces.me/contest/2043/submission/298343933

If it should get a TLE, try to add some big tests that makes B=1300 worst.

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem $$$D$$$, I miscalculated that if we consider all possible pairs of integers from intervals $$$[l,l+10)$$$ and $$$(r−10,r]$$$, we will find at least one coprime pair. But, the problem passed the system tests. Can anyone prove it or uphack the solution?
Submission — 298478051

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится +17 Проголосовать: не нравится

Nobody has mentioned the following method for Problem E, which was much more intuitive than the graph method for me:

For each binary matrix B, the final move must result in a column of all 1s or a row of all 0s. Therefore, we can go backwards from B to A and ignore all rows and columns once we know there is a later operation that can set it to be accurate in B. This can be done simply by keeping a counter and sum for each row/column.

Submission here: https://codeforces.me/contest/2043/submission/298482881

»
23 месяца назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +12 Проголосовать: не нравится

Problem G appeared in a contest on Luogu 5 years ago, here is the link: P6019 [Ynoi2010] Brodal queue

The problem statement on Luogu is:

1 l r x: for( i = l ; i <= r ; i++ ) a[i] = x;

2 l r: count the number of (i,j) that l<=i<j<=r and a[i]==a[j]

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone explain C a little better to me please. I solved D but just cannot understand C.

  • »
    »
    23 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    For an array of only 1 and -1, the range of subarray sums is simply from the minimum sum to the maximum sum, as the sum changes incrementally by +1 or -1.

    When the array contains x, the subarrays to the left and right of x consist only of 1 and -1. We calculate the range of sums for these subarrays independently. Since both ranges intersect at 0, the combined range will span from the minimum of the minimum sums to the maximum of the maximum sums.

    To determine the range for segments that include x, start from x and move outward to the left and right, summing the values as you go. As you move, x will increment or decrement by 1 at each step, eventually reaching its maximum and minimum contributions.

    So range is : Minimum Sum: min_prefix + min_suffix + x, Maximum Sum: max_suffix + max_prefix + x.

  • »
    »
    10 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Can you explain me B, pls. I trying solve it using Divisibility rule, but i stuck, when i need output 9.

    • »
      »
      »
      10 месяцев назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится
      Spoiler
»
23 месяца назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I noticed that in test case 2 of Problem C that the test case

2

-5 -1

is repeated many, many times. It seems there is an error in the test case, as I would assume it was meant to iterate through several cases. Most notably the tests during the contest miss a notable class of test cases such as 1 -1, -1 1, 1 1 -1, -1 -1 1, 1 -1 1, -1 1 -1, etc., and I am aware that test 2 is meant to iterate through such small cases, not repeat the same case repeatedly.

Specifically, this is the class of test cases of 1s and -1s whose prefix sums are always >= or <= 0. This mistake cost me and other contestants since our incorrect solutions passed during contest. Can a writer please address this?

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Problem with a similar approach to the C problem: 1695C. Zero Path

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In A.

Why this doesn't work. (ll)pow(2LL,(ll)(floor)(0.5*log2(n)))

It is also doing 2 to the power of floor of log4(n)

  • »
    »
    23 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Floating point values are imprecise (especially if they are big). You can easily experiment that

    (int)log2((1LL << 60) - 1)
    

    returns 60, while $$$\lfloor \log_2(2^{60} - 1) \rfloor = 59$$$.

    The takeaway is: don't use floating points, and stick to integers (unless floating point values are really necessary).

»
23 месяца назад, скрыть # |
← Rev. 3  
Проголосовать: нравится +14 Проголосовать: не нравится

In solution for f, shouldn't the condition

$$$if (dp[i][val][fl].cnt \gt 0)$$$

instead be $$$if(dp[i][val][fl].cnt \gt = 0)$$$??

As we are modding and that may cause cnt to be zero.

Edit : Now they fixed it

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem G, the input format requires us to solve the query in an online way. May I know how to solve it in an offline way?

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem F, I use long long to store the number of ways, and get a TLE.

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can somebody explain why this fails? 298283845

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится -6 Проголосовать: не нравится

Adding even digits (except 0 ) to B would have made the problem much more elegant.

BTW for people who don't know, a number is divisible by 2 if the last digit is divisible by 2

a number is divisible by 4 if the concatenation of the last two digits ( with leading zeros if less than two digits) is divisible by 4

a number is divisible by 8 if the concatenation of the last three digits ( with leading zeros if less than three digits) is divisible by 8

a number is divisible by 6 if it is divisible by 2 and 3 ( sum of digits is divisible by three and the last digit is even).

»
23 месяца назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone give me advice to solve problem G in $$$O((n+q)\sqrt n\log(\sqrt n))$$$ or it is impossible, thanks.

298614069

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

If you do block decomposition of size $$$O(n^{1/4})$$$ on the $$$blocks$$$ array, you could also attain $$$O(1)$$$ update and $$$O(sqrt(n))$$$ query complexity. I think it's more intuitive, but it seems a bit slower.

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Hello everyone. I am not able to fully understand solution for problem E (Matrix Transformation). In the binary matrix of A and B, if a bit is not matched then operations must be performed on A(Row or column) to get the matrix equal to B and if it changes anything else then an operation must also be performed to change it back. But how that results in a directed graph of operations which results the answer?

One way I thought was to perform operation for every row or column in place and take the operation and then when I go ahead(left top corner to right bottom simple matrix travel) if I find any unequal again, then again make the operation but here the previous values may also change then how we think from here?

After the graph is constructed, I am fine with topological part, that is simple.

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

for 2043E - Преобразование матриц can anyone explain why a solution like 298348918 works?

I mean why doing the operation 30 times is enough? my submission was the same but the loop runs min(n,m)+1 times

»
23 месяца назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +21 Проголосовать: не нравится

2043D — Problem about GCD

Upon request, I wrote a proof for the following fact.

Given $$$R - L \geq 21$$$ and $$$1 \leq L \leq R \leq 10^{18}$$$, there exists $$$(x, y)$$$ such that $$$L \leq x, y \leq R$$$, $$$\gcd(x, y) = 1$$$, and $$$R - L - 21 \leq y - x \leq R - L$$$.

https://atcoder.jp/contests/arc137/editorial/11700

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone figure out what error I'm making over here for C problem Here is the submission link https://codeforces.me/contest/2043/submission/298358310

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Why is problem F so easy :( :( :(, I just spent about 5 minutes to think, but in the original contest, I did not read this :(

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Resuming Codeforces after a couple of years,

my python solution for 2043E — Matrix Transformation is giving TLE in test case 2 and time limit is just 1 second for pypy and python. Should I shift to C++ for codeforces?

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone debug my code on problem E? I kept TLE on test 3 and I discovered that a lot of people TLE3 changed their solution at last but I don't want to change my solution

»
23 месяца назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

E has a (in my opinion) simpler solution.

As mentioned in the editorial, consider the case when $$$A_{i,j}, B_{i,j} \in \{0, 1\}$$$.

Observation 1: If a row of $$$B$$$ is all 0s, then we can disregard that row (and the corresponding row in $$$A$$$) and solve the problem for the remaining sub-matrix. This is because we can reserve the last operation in our final sequence of operations to update that row, after we have dealt with the rest of the matrix. Same observation holds if a column of $$$B$$$ is all 1s.

Observation 2: Suppose no row of $$$B$$$ is all 0s, and no column of $$$B$$$ is all 1s. Then, the answer is true if an only if $$$A=B$$$. This is because any non-empty sequence of operations will end up with a matrix that either has at least one row of all 0s (if the last operation was a row update) or at least one column of all 1s (if the last operation was a column update)

With these two observations in mind, we have the following pseudo-code

while(true){
  // delete a row of all 0s from B if it exists
  // delete a column of all 1s from B if it exists 
  // if nothing was delete, break  
}
// check if the A=B for the surviving rows and columns

To actually implement it, you can just maintain two sets/priority queues — one for rows (sorted by the number of 0s in the row) and one for columns (sorted by the number of 1s in the column). Also maintain rows_deleted and columns_deleted. Then, in the loop, you just need to check if the top member of the set/priority queue for rows has m-columns_deleted elements (and if the top member of the set/priority queue for columns has n-rows_deleted elements). The complexity is O(log(A)(nm + (n+m)log(n+m)))

Here is my submission: 299025009

»
23 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem E, I mistakenly reversed the edges of graph(kind of a transpose of graph). Then the test case failed. But, for a directed graph, even if the edges are reversed, it should not affect cyclicality of the graph. ~~~~~ Your code here... bool check(int idx){

int sz=n+m+1;
 Graph G(sz);
 vector<bool>rows(n+1,false),cols(m+1,false);
 for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        int a1=get_bit(a[i][j],idx);
        int b1=get_bit(b[i][j],idx);
        if(a1!=b1){
            if(b1==0){
             rows[i]=true;
            }
            else{
                cols[j]=true;

            }
        }
        if(b1==0){
            G.addEdge(i, j+n);            
        }
        else{
            G.addEdge(j+n,i);            
        }
    }
}
for(int i=1;i<=m;i++){
    if(cols[i] && G.dfs(i+n)){
        return false;
    }
}
for(int i=1;i<=n;i++){
    if(rows[i] && G.dfs(i)){
            return false;
    }
}

return true;

}

~~~~~

I even tried using cols[j]=0 instead of rows[i]=0 and the vice-versa. Can you give a test case? I had thoughts on the lines like n!=m. Please help

»
23 месяца назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

https://codeforces.me/contest/2043/submission/299169457
My solution for 2043F — Nim:
- 'The maximum number of piles that can remove' means 'The minimum number of piles that can keep'
- Due to 0<=a[i]<=50, we can use prefix sum to count the elements between l and r
- If there is 0 in the elements, there are count[0] number of ways
- If there is any elements that occurs at least twice, the minimum number of piles that can keep are 2
- Else now each elements occurs at most 1, count the minimum number of piles to reach 0 and the number of ways for that:

		ca, cb := Counter(Repeat(10, 64)), Counter{}
		for i := range count {
			if count[i] == 0 {
				continue
			}
			for i2 := range ca {
				if ca[i^i2] < ca[i2]+1 {
					continue
				} else if ca[i^i2] == ca[i2]+1 {
					cb[i^i2] += cb[i2]
				} else {
					ca[i^i2] = ca[i2] + 1
					cb[i^i2] = cb[i2]
				}
			}
			ca[i] = 1
			cb[i] = 1
		}
»
23 месяца назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

For problem D, it is mentioned that the problem for each testcase can be solved in K^2*log(A), where K is the number of numbers we check from L and R, and A is the bound on the numbers(for gcd). It has also been shown that the bounds for K are <=40. But then number of operations per testcase is 40*40*log(1e18) = 95671.52913 operations. Multiplying by the maximum number of testcases(1000) the overall number of operations should be 95671.52913*1000 = approximately 9.6*1e7 operations. The rule of thumb I know is that 1e8 operations take 1s, and we are not doing simple operations in the code since we use modulo(%), which takes more time, to compute gcd. So, even after the rigorous proof, it seems that the solution will barely pass the 1s time limit. This does not seem like a good solution and that the only reason solutions of this form passed was because the testcases were not that stringent.

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

About problem E I've got an alternative solution.

Like the editorial, we check for each bits.

For every bit, consider the operation reversely, that is, each operation on a row can change a row of zeros in $$$B[i]$$$ into whatever in $$$A[i]$$$. (Formally, if $$$B[i][1] = B[i][2] = ... = B[i][m] = 0$$$, applying the operation reversely can obtain an $$$A$$$ with arbitrary $$$A[i][j]$$$, $$$\forall j \in [1, m]$$$)

Likewise, an operation on column considered reversely can change a column of ones in $$$B[][j]$$$ into arbitrary $$$A[i][j]$$$, $$$\forall i \in [1, n]$$$$

By examining the final matrix B, we can obtain all positions that can be changed and we just need to check whether all positions different in A and B are included.

Submission: 328167989

»
10 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Не понимаю случай с 9, в задаче B. Что именно в таком случае в if ложить, не понимаю.

»
5 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For F, a slightly different solution is to notice that:

  • If the interval contains $$$0$$$s, then you can keep $$$r-l$$$ piles, and the number of ways is just the number of 0s

  • If the interval has any duplicate elements, then you can keep $$$r-l-1$$$ piles, and the number of ways is just the number of pairs of equal elements.

  • Otherwise, the interval has all distinct nonzero elements, so you can do a knapsack DP (like the editorial), but with only 1 transition