azberjibiou's blog

By azberjibiou, 21 month(s) ago, In English

1788A - One and Two

Problem idea: azberjibiou

Tutorial

1788B - Sum of Two Numbers

Problem idea: azberjibiou

Tutorial

1788C - Matching Numbers

Problem idea: azberjibiou

Tutorial

1788D - Moving Dots

Problem idea: azberjibiou

Tutorial

1788E - Sum Over Zero

Problem idea: azberjibiou

Problem solver: YeongTree

Tutorial

1788F - XOR, Tree, and Queries

Problem idea: azberjibiou

Tutorial
  • Vote: I like it
  • +148
  • Vote: I do not like it

| Write comment?
»
21 month(s) ago, # |
  Vote: I like it -17 Vote: I do not like it

binary searching on B worked

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

can anyone please explain problem A,

  • »
    »
    21 month(s) ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    you need to find the position k that divides the array in a way that the product of elements from the start until element in position k = the product of all elements after this position k

    • »
      »
      »
      21 month(s) ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      why prefix and suffix arrays for storing the multiplication will not work for problem A ?

      • »
        »
        »
        »
        21 month(s) ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        Because the maximum possible product is 2^1000 and there is not in C++ any data type that can store that value, actually when n equals to 65 and each element in A is equal to 2, the prefix, suffix idea fails

  • »
    »
    21 month(s) ago, # ^ |
    Rev. 2   Vote: I like it +5 Vote: I do not like it

    Array only consists of 1 and 2, you can notice that multiplying by 1 does nothing, so we count the number of 2's in the array, to divide the array into two equal parts, there must be an equal number of twos on each part, using a second variable we can loop from N to 2 and check if the array value is 2, if it is, we add 1 to the current variable, and subtract 1 from the original count, when 2 variables are equal, means we can create equal parts by splitting the array at current position

    • »
      »
      »
      21 month(s) ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Could you please tell me why my code is giving wrong answer instead of counting number of two's I just make prefix and suffix array of multiplication My submission

      • »
        »
        »
        »
        21 month(s) ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Its because of the constraints where n=1000. Take the worst case where all are 2 then in the suffix you will have to evaluate 2^1000 which will give TLE.

        • »
          »
          »
          »
          »
          21 month(s) ago, # ^ |
            Vote: I like it +6 Vote: I do not like it

          2^1000 will not give TLE. TLE means time limit exceeded. Instead, it will overflow and give WA — wrong answer.

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    in the multiplication, only 2 matters. So you need to divide the array such that number of 2's on left part is equal to number of 2's on right part.

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Divide the array into two arrays of i and check whether the number of twos in the two arrays is the same.

    def solve():
        n = int(input())
        a = list(map(int, input().split()))
    
        if 2 not in a:
    	print(1)
    	return
    
        for i in range(n):
            if a[:i].count(2) == a[i:].count(2):
    	    print(i)
    	    return
        else:
            print(-1)
    
    for i in range(int(input())):
        solve()
    
»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Can I get some help with debugging ?

https://codeforces.net/contest/1788/submission/192961698

I have implemented exactly same approach as mentioned in the editorial. During contest I got 2 WA :( ... CAN"T figure out what is wrong...

»
21 month(s) ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

Tutorial for Problem B:

Hint 1
Hint 2
Solution
  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Amazing! Can you explain your intuition behind this approach?

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

NlogXlogN with DSU passes in 2140ms

imho, giving this as final task of a 6-round 2-hour D2 is rough...

https://codeforces.net/contest/1788/submission/192975706

»
21 month(s) ago, # |
Rev. 2   Vote: I like it +97 Vote: I do not like it

C, D and F are great problems. I think the editorial for C kinda glosses over inspiration for the construction.

Here is my construction idea

Consider

14 13 12 11 10 9  8
1  2  3  4  5  6  7

Currently all pairs have equal sums. If I swap the bottom elements of 2 pairs with difference $$$d$$$, then the sum of one pair increases by $$$d$$$, and the other reduces by $$$d$$$. Over here if I am able to get $$$+d$$$ and $$$-d$$$ for $$$d$$$ from $$$0$$$ to $$$3$$$, it will produce consecutive sums.

14 13 12 11 10 9  8
1  2  3  4  5  6  7
|  |  |  |  |     |
|  |__|  |  |     |
|________|  |_____|

Notice that the odd differences are nested within each other, and so are the even differences. Swapping the shown pairs produces.

14 13 12 11 10 9  8
4  3  2  1  7  6  5

This has consecutive sums from $$$12$$$ to $$$18$$$. Generalising to arbitrary odd $$$n$$$ is not difficult.

This also makes it trivial to see, that even $$$n$$$ is impossible. You can produce any pairing by swapping elements. Each swap will make one pair increase by $$$x$$$, and the other decrease by $$$x$$$. Let us consider the final solution. Each final pair will be $$$2n + 1 + x$$$ for $$$x$$$ in some consecutive range, that adds to $$$0$$$. This is impossible for an even length range. If there are more positive elements, the range will be positive, and negative otherwise. This is very trivial to check. Therefore even $$$n$$$ is impossible.

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Notice that the odd differences are nested within each other, and so are the even differences. Swapping the shown pairs produces.

    Wow, this is exactly what I thought about but I couldn't get the last observation of nested swapping for the same parity. It took me the whole round thinking about how to perform swapping in vain.

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +7 Vote: I do not like it

    Nice idea, maybe better than my construction idea.

    Here is my construction idea.

    For some integer $$$x$$$ and $$$y$$$, $$$(x, y), (x+1, y+1), \ldots, (x+k, y+k)$$$ will give an arithmetic sequence of common difference $$$2$$$.

    Arithmetic sequence from $$$x_1+y_1$$$ will cover the even part, and another arithmetic sequence from $$$x_2+y_2$$$ will cover the odd part. After some work, you can find $$$x_1, y_1, x_2, y_2$$$.

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    how you were able to construct this idea so fast? Will studying number theory help me with this problem?

  • »
    »
    21 month(s) ago, # ^ |
    Rev. 4   Vote: I like it 0 Vote: I do not like it

    Hi Everule,

    I think I have a much simpler construction for this problem. A bit of math will tell you that the smallest sum will be 3(n + 1)/2.

    Let,
    S = 3(n + 1)/2

    Now break up the sequence [1, 2n] in two parts [1, n] and [n + 1, 2n].
    First pair is (1, S — 1)
    Second pair is (2, S — 1 + 1)
    and so on...
    Just make sure to loop around the first number of the pair in the range [1, n] and second number in the range [n + 1, 2n]. You will generate all sums in the range [S, S + n — 1].

    Proof left as an exercise for the reader. :)

    https://codeforces.net/contest/1788/submission/193235034

    • »
      »
      »
      21 month(s) ago, # ^ |
        Vote: I like it +11 Vote: I do not like it

      Adding proof because some people asked for it.

      Lemma 1:

      Smallest sum (S) = 3(n + 1)/2
      

      Lemma 2:

      S - 1 is the middle number, i.e, the ceil(n / 2)th number in the range [n + 1, 2n], so if [n + 1, 2n] had 5 numbers S - 1 would be the 3rd. For example if [n + 1, 2n] was [6, 10], S - 1 would be 8.
      

      Terminology:

      Lets call sums with even difference from S, even sums.
      Lets call sums with odd differences from S, odd sums.
      So [S, S + 2, S + 4, S + 6, ...] are even sums.
      And [S + 1, S + 3, S + 5, ...] are odd sums.

      Main Proof:
      We have to make n sums in the range [S, S + n - 1], since according to Lemma 1, smallest sum must be S and we have to make n consecutive sums.

      So, we have to make ceil(n / 2) even sums and floor(n / 2) odd sums.
      Visually, we have to make

       S   S + 1  S + 2  S + 3  ... S + n - 1  
       ^     ^      ^      ^           ^
      even  odd    even   odd         even 
      
      S + n - 1 will have same parity as S, since n is odd. (This also comes from Lemma 1.)
      

      Lets take this as our first pair.
      [1, S - 1], clearly this makes sum S one of even sum.

      Next pairs will be -

      [2, S],
      [3, S + 1] ....
      [ceil(n / 2), 2n]

      This is the key point.
      The last pair will have [ceil(n / 2), 2n] because of Lemma #2. S - 1 was the middle number and we move till end so the first number in the pair becomes ceil(n / 2).
      Consequently, we have ceil(n / 2) pairs and the sum of each pair is greater than the previous by 2, so we have generated all the Even Sums.

      After this we are going to loop around the range [n + 1, 2n] for the second number of the pair.

      So, next pair will be
      [ceil(n / 2) + 1, n + 1]

      The sum will be (n + 1) / 2 + 1 + n + 1 = S + 1.

      Now if we keep generating subsequent pairs until we reach
      [n, S - 2], we will have generated all the odd sums starting from S + 1.

      Both Lemmas can be easily proved with a bit of math.

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I suppose the edi glossed over it because there are a lot of valid constructions.

    For example mine https://codeforces.net/contest/1788/submission/193410581.

    I realized all the sums have to be different mod n so if I increase one of the numbers by 1 and decrease the other by 2 it it will always be different mod n.

»
21 month(s) ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

In problem C you provided one of the possible pairings but how can someone prove it? Or is it just random guesses?

  • »
    »
    20 months ago, # ^ |
    Rev. 4   Vote: I like it +1 Vote: I do not like it

    It became obvious for me after I wrote down on paper the pairings (and the sums) by the formula for n=9 (m=4, k=15).

»
21 month(s) ago, # |
  Vote: I like it -32 Vote: I do not like it

shit contest , constructive force

»
21 month(s) ago, # |
  Vote: I like it +11 Vote: I do not like it

Could someone give an explanation on why binary search works for problem B?

I've seen submissions like this one 192978163 and can't figure out why it passes.

»
21 month(s) ago, # |
  Vote: I like it +43 Vote: I do not like it

1788E and 1667B are similar

»
21 month(s) ago, # |
Rev. 3   Vote: I like it +3 Vote: I do not like it

For B, I brute forced many numbers and eventually found a pattern

For even you can just do n / 2, n / 2 For odd you will find answer (n + 1)/2 + x, (n — 1)/2 — x Where x = 4, 49, 499, 4999, ..., 8, 89, 899, 8999, ...

Can somebody please prove my solution?

#include <bits/stdc++.h>
#define int long long int

void solve(){
    int n;
    std::cin >> n;

    auto f = [&] (int x) {
        int s = 0;
        while(x) {
            s += x % 10;
            x /= 10;
        }
        return s;
    };

    if(std::abs(f(n / 2) - f(n - n / 2)) <= 1) {
        std::cout << n / 2 << " " << n - n / 2 << "\n";
        return;
    }

    int i = 4, x = (n + 1) / 2, y = (n - 1) / 2;
    while(i < n) {
        if(std::abs(f(x + i) - f(y - i)) <= 1) {
            std::cout << x + i << " " << y - i << "\n";
            return;
        }
        i = i * 10 + 9;
    }
    i = 8;
    while(i < n) {
        if(std::abs(f(x + i) - f(y - i)) <= 1) {
            std::cout << x + i << " " << y - i << "\n";
            return;
        }
        i = i * 10 + 9;
    }
}
     
signed main() {

    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE

    int t = 1;
    std::cin >> t;
    for(int i = 1; i <= t; i++) {
        solve();
    }
}
»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Why can't E use the method of binary search monotone stack to realize it

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    counter-test for your last submission:

    -1 -1 2 -2 -1 1

    your solution returns 4 (segment [3, 6]), while the answer is 5 (segments are [1, 3], [5, 6]).

  • »
    »
    21 month(s) ago, # ^ |
    Rev. 3   Vote: I like it +19 Vote: I do not like it

    Here's how alternate solution you said works.

    Note the recurrence in the official solution:

    $$$dp_i = \max(dp_{i-1}, \underset{p_k \leq p_i}{\max}(dp_k-k)+i)$$$

    Solving it with brute-force costs $O(n^2)$ time. But we can solving it in almost linear time with a technique similar to Monotonous Queue.

    We find that when there's a pair of index $$$(x,y)$$$ s.t. $$$dp_x-x<dp_y-y$$$ while $$$p_x\ge p_y$$$, if we use $$$k=x$$$ in one recurrence, we can always use $$$k=y$$$ (since $$$p_y \le p_x \le p_i$$$) with an better value, so $$$x$$$ can never be the optimal decision in a recurrence.

    We use a set to maintain all pair $$$(dp_k-k, p_k)$$$ (sorting by $$$p_k$$$) possibly optimal for a recurrence. When adding $$$i$$$, we can maintain it by erasing all $$$x$$$ with $$$dp_x-x<dp_i-i, p_x\ge p_i$$$, and check if $$$i$$$ will be erased after adding $$$i$$$. And then it satisfies something similar to Monotonous Queue: if $$$p_x<p_y$$$ then $$$dp_x-x<dp_y-y$$$. So we can search for the maximum $$$p_k$$$ for the optimal decision($$$O(\log n)$$$ per recurrence).

    code (Note that this solution calculate the minimum count of numbers not present in any segment you choose instead.)

»
21 month(s) ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

In E, what does it mean to "use coordinate compression on $$$p_i$$$" where $$$p$$$ is the prefix sum array of $$$a$$$ ?

  • »
    »
    21 month(s) ago, # ^ |
    Rev. 2   Vote: I like it +3 Vote: I do not like it

    precompute all the possible prefixes of the given array and then map them to the range 0 — k-1 based on their relative values (k is the number of unique prefixes). eg- if the prefixes or -3 4 2 -1 then this will be converted to 0 3 2 1.

    My Submission

    • »
      »
      »
      21 month(s) ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Makes total sense, thank you !

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Any dp solution for D?

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    my solution was something like dp[i][j][dir] -> number of collisions if you are standing on the ith index, you chose the jth index before you and the jth index had a direction left/right.

    • »
      »
      »
      21 month(s) ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      what would the transitions be?

      • »
        »
        »
        »
        21 month(s) ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        let K be the first index such that dist[i]-dist[j] <= dist[k]-dist[i]

        dp[i][j][dir] = dp[i+1][i][right] + ... dp[k-1][i][right] + dp[k][i][left] + ... dp[n-1][i][left] + {2^(n-k) if (dir == right) because for all these subsets we will have a collision point between i and j }
        

        My Submission

    • »
      »
      »
      21 month(s) ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thank you. Can you clarify a bit more about what do you mean by, you chose the j-th index before you and the j-th index had a direction left/right. But the problem says each dot moves in the direction of the closest dot (different from itself) until it meets another dot. So doesn't it mean each dot has their directions fixed ?

      • »
        »
        »
        »
        21 month(s) ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I am assuming that

        1. There is no element chosen between the j'th and the i'th element
        2. The j'th element has its direction fixed ie. either its moving right towards the ith element (which is closest for it), or left (to some other closer element on its left as compared to the ith element, we dont care what that element is, only the direction matters to us).

        My final answer is the sum of dp[i][[j][right] for all pairs of i and j.

        If you are thinking that maybe the dp[i][j][left] is not a valid state because j has no closer element on its left w.r.t to i. Then that state will never be calculated in our answer.

    • »
      »
      »
      21 month(s) ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I had a similar sol but couldn't implement it-tons of debugging :(

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

in problem D what according to author i will move to right and j will have to move left but while solving we are taking a lower-bound of 2*a[i]-a[j] and not 2*a[i]-a[j]-1, can any one explain where am i wrong

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It is because in the question it is mentioned that if its a tie the point will move left so if a point is equal to 2*a[i]-a[j] i will move left instead of right.

»
21 month(s) ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

For problem F, in the formula $$$\bigoplus_{k \in L} p_{r_k} \oplus c$$$, the value of $$$c$$$ is the xor-sum of every path from $$$p_{r_k}$$$ to every vertex in $$$G_k$$$, right?

Then, wouldn't changing the value of $$$p_{r_k}$$$ also change the values of $$$p_i$$$ for some vertices in $$$G_k$$$? If I understand this correctly, we are getting the initial values of $$$p_i$$$ with dfs in each component of $$$G'$$$, setting its value to the xor-path from the dfs root to vertex $$$i$$$. So, if $$$r_k$$$ is in the middle of the path, the value of $$$p_i$$$ would also change.

I'm guessing that I'm not understanding that part correctly, where does the constant $$$c$$$ comes from? Is that the way to get the initial values of $$$p_i$$$?

Thanks.

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    If you change $$$p_{r_k}$$$, $$$p_i$$$ would also change. Here is one example.

    If $$$G'$$$ has $$$3$$$ vertices, $$$r_k=2$$$ and the edges are $$$(1, 2, 3), (2, 3, 1)$$$

    $$$p_3=p_2 \oplus 1$$$ and $$$p_1=p_2 \oplus 3$$$.

    Odd degree vertices are $$$1$$$ and $$$3$$$, so we get $$$p_1 \oplus p_3 = p_2 \oplus 1 \oplus p_2 \oplus 3 = 2$$$

    In this case, $$$c=2$$$.

    • »
      »
      »
      21 month(s) ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Thanks a lot! I get it now. I should have reviewed that part more thoroughly.

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

For problem E,is there a conclusion that "for dp[i], we solve it greedyly.the answer is the leftmost k which p[k]<=p[i]."

it means: dp[i]=min(dp[i-1],dp[j](p[j]<=p[i]&&(∀kp[k]>p[i])));

thx.(pls forgive my poor English:-)

»
21 month(s) ago, # |
Rev. 5   Vote: I like it +1 Vote: I do not like it

With how vague the editorial is about finding a matching for C, I'm convinced that it's really just a pattern-finding problem after the initial steps of removing even $$$n$$$ from consideration and finding the target sums for odd $$$n$$$. Now I don't feel so bad for not being able to solve it.

For those who did solve C, can anyone please elaborate on the thought process on how you arrived at a correct matching?

  • »
    »
    21 month(s) ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Pattern finding is the case for me. I first tried getting the sum of $$$(a_1 + b_1)$$$ with a little bit of math which turned out to be $$$\frac{(3n + 3)}{2}$$$ and noticed that from pair $$$(a_i,b_i)$$$, we can get to the next sum $$$(a_{i+1}, b_{i+1})$$$ with a difference of $$$1$$$ by adding $$$+2$$$ to $$$a_i$$$ and $$$-1$$$ to $$$b_i$$$. After randomly testing many ways to do the matching, setting $$$a_1 = 1$$$ and $$$b_1 = \frac{(3n+3)}{2} - 1$$$ worked. I also stress-tested this with a bunch of random test cases to confirm. (I left out some details which you can check in my submission if interested).

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

can anyone please tell what i am doing wrong, wrong answer on test 2 , 61st numbers differ — expected: '316', found: '71' 193009289

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    You're trying to compute the products, but this would not even fit long long if there are many 2s. If there are 1000 $$$2$$$s, the result is $$$2^{1000}$$$, which requires around 1000 bits. The long long type only supports 64 bits.

    Rather than calculate the products directly, I would suggest that you instead keep track of the power of 2 that the product has, i.e., how many 2s were present in this desired product.

»
21 month(s) ago, # |
  Vote: I like it +1 Vote: I do not like it

Can someone explain the solution of D problem? Thanks in advance

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it +13 Vote: I do not like it

    Suppose you take a specific subset of $$$\geq 2$$$ dots. You can then represent each dot as L or R based on whether it moves left or right, and construct a string of Ls and Rs. String always begins with R and ends with L. The number of distinct stopping coordinates for this subset is equal to the number of instances of the substring "RL" (each R stops at the same place as its next L, and each L stops at the same place as its previous R).

    Therefore, the objective is to output the sum of the number of RL substrings in ALL possible subsets of size 2. However, this can be rephrased as follows: for each pair of dots $$$i$$$ and $$$j$$$, count the total number of subsets where dot $$$i$$$ and dot $$$j$$$ form an RL substring, and output the sum for all pairs.

    Given $$$i$$$ and $$$j$$$, when will dot $$$i$$$ and dot $$$j$$$ form an RL substring? This arises only in a subset for which dot $$$j$$$ is the closest dot to dot $$$i$$$ (no ties allowed, since $$$i$$$ breaks ties by going left, away from $$$j$$$) and dot $$$i$$$ is the closest dot to dot $$$j$$$ (ties allowed, since $$$j$$$ breaks ties by going left, towards $$$i$$$). To count how many of these there, we can first count the number of dots, excluding $$$i$$$ and $$$j$$$, such that the presence of these dots will not prevent $$$i$$$ and $$$j$$$ from moving towards each other.

    Let $$$d = x_j - x_i$$$. Dots with positions $$$< x_i - d$$$ are allowed, since $$$i$$$ still moves to $$$j$$$, and the dots with positions $$$\geq x_j + d$$$ are also allowed, since $$$j$$$ still moves to $$$i$$$. We can count how many such dots there are with two binary searches: one for $$$x_i - d$$$ and another for $$$x_j + d$$$. If we have $$$s$$$ such dots, then there are $$$2^s$$$ subsets of these $$$s$$$ dots that we can include alongside $$$i$$$ and $$$j$$$ to generate a subset (of all dots) where $$$i$$$ and $$$j$$$ form an RL pair.

    The final output is the sum of $$$2^s$$$ for each $$$(i, j)$$$ pair, modulo $$$10^9 + 7$$$.

    My submission: 193040606. The pw vector stores the powers of 2 modulo $$$10^9 + 7$$$. The variables lsz and rsz denote the number of allowed dots that are left of $$$i$$$ and right of $$$j$$$ respectively. The way the built-in lower_bound is defined conveniently fits with how ties are handled in this problem.

»
21 month(s) ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Why does question B take a brute force approach to the pre-test 1 error, can it provide the data of test 1?

»
21 month(s) ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I did not find the similar approach for C so here it is:
Let's pair all numbers at 1..n(left) with numbers from n+1..2n(right).
Let's start with 1 and some x, their sum is (x + 1) so for next pair sum should be (x + 2). I can't take 2 as next left number cause then I must take x as right number for sum to be (x + 2). So I take 3 as next left number and x-1 as right number. Next pair will be 5 and x-2 and so on. That's how I construct pair with odd numbers from 1..n: I increase left by 2 and decrease right by 1 to make step=1 in sums of pairs.
Then I need to deal with even numbers. After last odd number I need to go to first even number 2. It means I need to decrease left by (last_odd — 2) and increase right by (last_odd — 2) + 1.
Then again I increase left by 2 to iterate over even numbers and decrease right by 1 to make step=1 in sums of pairs.
To make it all work we need to choose x at the beginning. The most critical part is when we go from last odd number to first even number: we need to increase right by (last_odd — 2) + 1 which is equals to n-1 because last_odd=n. So to not overflow 2n limit we need to have right number as 2n-(n-1)=n+1 at most. Apparently this is the least right number we can get from n+1..2n.
So x at the beginning should be (n+1)+(count_odd_numbers_in_1..n — 1)

examples:

n=3
n=5
n=7

submission 192961794

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Great contest! you can find the video editorials for problem C and D here .

hope this helps you! happy coding!

»
21 month(s) ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

In question D can someone explain me

"Now let's solve the problem for subsets. Instead of counting number of adjacent dot that moves toward each other for each subset of dots, we will count the number of subset for each possible 1≤i<j≤N where dot i moves right and dot j moves left and there are no dots between i and j ."

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    We need to count pair of dots that adjacent to each other amony all subsets, and instead of iterate the subsets, it's eaiser to iterate all pairs of dots that moves toward each other and count how many subsets contain them. (my english is bad, if you still can't understand I don't blame you QAQ)

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Easiest div2 contest so far, yet I got slain by mere problem D :(

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

I have a question about Problem B: Since I need to find 2 numbers, A and B, for the input number N such that, a) A + B = N and b) sumofdigits(A) — sumofdigits(B) is at most 1, could I not just use the approach where I do the following:

  • if N is even, assign N/2 to both A and B; --> ensures that sumofdigits(A) — sumofdigits(B) = 0
  • if N is odd, assign N/2 to A, and (N/2)+1 to B; --> ensures that sumofdigits(A) — sumofdigits(B) = 1

Could someone please clarify this for me, since I think I misunderstood the question?

  • »
    »
    21 month(s) ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I think you understood the question, but your approach is incorrect. Consider $$$N = 19$$$, where $$$N/2 = 9$$$ (whose digits sum to 9) and $$$N/2 + 1 = 10$$$ (whose digits sum to 1). There are many other edge cases (like if you have consecutive 9s) that this kind of approach would not be suitable.

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

can someone explain me the the solution for problem E

I cant understand how the dp state is defined in the editorial?

I tried solving this using recursion with dp but getting TLE on Test 4 Submission Link

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

video editorial for Chinese:

BiliBili

»
21 month(s) ago, # |
  Vote: I like it +5 Vote: I do not like it

Lately I receive a message that my submission 192896600 is same as 192935411.I cheked it and find that his solution to the problem is different from mine.I guess may be comments at the end of the code is the reason why two submissions is thougt as same.

In addition, I can give evidence that I used this template before him. mine:188824606 his:189071540

[user:KAN][user:MikeMirzayanov][user:azberjibiou]

»
21 month(s) ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it
In problem B, my code passed in a weird way. Can someone help me prove it a little more rigorously?

For the case of n being odd, I set p=(n-1)/2 and q=(n+1)/2, and then I keep doing "p+=5" and "q-=5" to get the answer.Here is the link to my code:https://codeforces.net/contest/1788/submission/192988232 I hope someone can help, because I need to perfect my solution.

»
19 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Problem B can also be solved with just randomly choosing $$$x$$$ until you find good one.

Link to solution.

»
9 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Why does this code gives RUNTIME_ERROR : java.lang.ArithmeticException: / by zero. Submission : 243443776

  • »
    »
    9 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    long cannot store values larger than $$$2^{63}-1$$$. Due to how integer overflow works, after the true product you're trying to store in currProd becomes $$$2^{64}$$$, the actual value stored in currProd becomes $$$0$$$. You're trying to divide by this, so you get an error.