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

Автор NemanjaSo2005, 2 года назад, По-английски

1900A - Cover in Water

Author: NemanjaSo2005

Hint 1
Hint 2
Solution
Bonus

1900B - Laura and Operations

Author: NemanjaSo2005

Hint 1
Hint 2
Hint 3
Hint 4
Solution
Bonus

1900C - Anji's Binary Tree

Author: Riblji_Keksic

Hint 1
Hint 2
Hint 3
Hint 4
Solution
Bonus

1900D - Small GCD

Author:NemanjaSo2005

Hint 1
Hint 2
Solution part 1
Hint 3
The rest of the solution
Bonus

1900E - Transitive Graph

Author:NemanjaSo2005

Hint 1
Hint 2
Hint 3
Solution
Bonus

1900F - Local Deletions

Author: NemanjaSo2005

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution
Разбор задач Codeforces Round 911 (Div. 2)
  • Проголосовать: нравится
  • +103
  • Проголосовать: не нравится

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

Problem B could be solved with XOR's as well. Here's a somewhat related problem: http://www.usaco.org/index.php?page=viewproblem2&cpid=1232

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

I solved B using DP 234444052

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

Nice problems, I liked the contest. I had a different solution (which generalises to harder versions easily) for D though.

solution
»
2 года назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

thanks for the tutorial with hints problem E was one of the best problems i have seen live in a contest

»
2 года назад, скрыть # |
Rev. 6  
Проголосовать: нравится +9 Проголосовать: не нравится

Great editorial ! It is always appreciated to have hint based editorials.

Btw I might have been a bit rash in my feedback. Considering the amount of solves by div2 contestants, E made the job a div2 E should do (also, the bonus task looks super cool!!). Also, after thinking a bit more about it, I think it's not that bad to have a div2 C which isn't adhoc as it gives all contestants the opportunity to solve "algorithmic" problems and not "1-observation" problems. I'm interested in others advice

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

I'm facing a problem with solving problems with number theory tags and focused on GCD, can I have a resource to learn from so I can pass this kind of problems faster please?

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

    Not really a resource but all you really need is prime decomposition of a number, the gcd trick used in D, the fact that numbers usually have up to O(m^1/3) divisors, the number of prime divisors is logarithmic, seeve, √N factoring. You can learn those by either looking it up, or solving number theory on codeforces/other platform.

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

bruh why is D so tough. Can someone simplify please..

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

I had a slightly different solution for D. I iterated over i from 1 to maxi in order to compute x[i] (which is defined in the editorial). To compute each individual x[i], I had a list of indices of numbers in the sorted array divisible by i and I started from the third element in this list all the way to the last element. Something interesting is that if you pick some value for C with an index in the interval (x, y] in the sorted array where x and y are the indices of 2 multiples of i with no multiples of i between them with an index in that interval, then the number of possible choices for a and b such that both are divisible by i will always be the same. So you can iterate through the list of indices of multiples of i and get the #of ways to pick A and B and multiply this by the length of the current interval that tells us the #of ways to pick C. A nice implementation trick is to add the index n-1 to the end of every list of indices of multiples in order to catch all possible values of C for all x[i]. Once you've computed that, the rest of my solution is the same.

234519989

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

Can someone help me understand the solution to Problem D? I have been trying to understand the solution for a bit but I just can't understand it at all. Can someone give a simpler explanation because my brain just can't understand the solution for whatever reason?

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

    Can provide a simple solution.

    I am not a native English speaker and I use translation software. If you have any translation questions, please feel free to reply to me :)

    The first step, as in the solution, is to find all divisors of all numbers in the range in $$$mlogm$$$ time, which can be preprocessed and stored through $$$10^5$$$ vectors. Note that this step precedes multiple rounds of testing. The second step is to sort the input array and enumerate $$$a_i$$$ from small to large as $$$b$$$, then $$$c$$$ can be selected arbitrarily after $$$b$$$, so the original problem can be converted into $$$\sum_{i=1 }^n\left((n-i)\sum_{j=1}^{i-1}\gcd(a_j,a_i)\right)$$$. That is, we now need to quickly find $$$\sum_{j=1}^{i-1}\gcd(a_j,a_i)$$$ for each $$$i$$$.

    The third step is to find that when $$$a_i$$$ is fixed, the result of $$$\gcd(a_j,a_i)$$$ must be a divisor of $$$a_i$$$. So enumerate all the factors $$$d$$$ of $$$a_i$$$ from large to small, try to check how many $$$a_j$$$ are multiples of $$$d$$$, and $$$d=\gcd(a_j,a_i)$$$, multiply this number by $$$ d$$$ , then add it to the answer.

    More specifically, we have an array $$$cnt$$$ in the range. $$$cnt_d$$$ represents how many numbers in $$$a_1$$$ to $$$a_{i-1}$$$ are multiples of $$$d$$$. Since it has been sorted, this Arrays can be maintained simply by enumerating factors during previous traversals.

    The following work will be better understood through an example. Consider the following sequence $$$2\ 3\ 6\ 7$$$. At this time, $$$i=3$$$, then $$$cnt_1=2$$$ (including $$$2$$$ and $$$3$$$), $$$cnt_2=1$$$ (including $$$2$$$) have been maintained. $$$cnt_3=1$$$ (including $$$3$$$). We enumerate the factors $$$d$$$ of $$$6$$$ from largest to smallest:

    When $$$d=6$$$, $$$cnt_6=0$$$, we do nothing.

    When $$$d=3$$$, $$$cnt_3=1$$$, which means that among the previous numbers, there is a number that is a multiple of $$$3$$$. Although we do not know who it is (in fact, it is $$$3$$$), because we Enumerate $$$d$$$ from large to small. At this time, $$$d=3$$$ must be the $$$\gcd$$$ of that number (in fact, it is $$$3$$$) and $$$a_i$$$ (although this is not actually completely correct, we will ensure the correctness of this soon) , we do $$$ans+=d*cnt_d$$$. And because the numbers in $$$cnt_d$$$ must also be in $$$cnt$$$ of all factors of $$$d$$$, and we only want these numbers to be counted in $$$d$$$, so we need to traverse all the factors of $$$d$$$ $$$d ^{'}$$$, do $$$cnt_{d^{'}}-=cnt_d$$$, thus ensuring correctness. In this example, we did $$$ans+=3\times1$$$, then traversed $$$1$$$ and $$$3$$$, and did $$$cnt_1-=cnt_3$$$ and $$$cnt_3-=cnt_3$$$, thus successfully converting the calculated $$$3 $$$ is removed from $$$cnt_1$$$ and $$$cnt_3$$$.

    When $$$d=2$$$, $$$cnt_2=1$$$, we do $$$ans+=1\times2$$$, $$$cnt_1-=cnt_2$$$ and $$$cnt_2-=cnt_2$$$.

    When $$$d=1$$$, we will find that $$$cnt_1$$$ has been reduced from $$$2$$$ at the beginning to $$$0$$$, so we do nothing

    Finally, we undo all modifications made during this process, i.e. restore $$$cnt$$$ to $$$cnt_1=2$$$, $$$cnt_2=1$$$, $$$cnt_3=1$$$. This can be achieved by creating a $$$\log$$$ sized backup of $$$cnt$$$ before calculating

    Note that the number of divisors of a number in the int range does not exceed $$$1600$$$ at most, and the amortized number should be $$$\log$$$ level, so the complexity is not a problem

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

      Why does cnt[1] also need statistics? From back to front, why do we need to subtract the factor of d? What will be the impact if we don’t subtract it? Do you have any examples?

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

        For your first question, $$$cnt_1$$$ certainly needs to be counted. Because there is a case $$$\gcd(a_j,a_i)=1$$$, they need to be added to the answer. I'm confused by this question and I'm not sure I fully understand your first question.

        For your second question, I can provide further explanation.

        Still based on the premise that $$$a$$$ has been sorted.

        For example, the current situation is $$$i=9$$$, $$$a_9=18$$$, and its factors are $$${18,9,6,3,2,1}$$$.

        Suppose we had $$$a_7=12$$$ before, and its factors are $$${12,6,4,3,2,1}$$$.

        Obviously, $$$\gcd(a_7,a_9)=6$$$, this $$$6$$$ should be part of $$$\sum_{j=1}^{i-1}\gcd(a_j,a_i)$$$ when $$$i=9$$$.

        So now we want $$$\gcd(a_7,a_9)$$$ to be evaluated, and only once, when enumerating up to a factor $$$6$$$ of $$$a_9$$$.

        By the way, we need to make it clear that $$$a_i$$$ is added to $$$cnt$$$ after calculating $$$\sum_{j=1}^{i-1}\gcd(a_j,a_i)$$$. This is very important.

        According to the definition of $$$cnt_d$$$, $$$cnt_6$$$ definitely includes $$$a_7$$$, but does not include $$$a_9$$$. When I traverse the factors of $$$a_9$$$ from large to small, $$$a_7$$$ will enumerate to $$$cnt_6$$$ is considered and calculated correctly, but if we do nothing, $$$a_7$$$ will be calculated again at $$$cnt_3$$$, $$$cnt_2$$$ and $$$cnt_1$$$. That is, if we do nothing, $$$a_j$$$ and $$$a_i$$$ will be evaluated at all divisors of $$$\gcd(a_j,a_i)$$$, which is obviously wrong.

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

        Sorry for still using translation software. Translating from Chinese to English is too easy to cause ambiguity. If you have any questions, please continue to ask :)

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

        The purpose of enumerating the divisors from large to small is to traverse to the "largest" common divisor.

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

      As for the first question I raised, I misread it, and I would like to apologize to you. For the second question: For example, test sample 1 5 2 3 6 12 17

      When gcd is a multiple of 1: there are 10 situations When gcd is a multiple of 2: there are 4 situations When gcd is a multiple of 3: there are 4 situations When gcd is a multiple of 6: there is 1 situation

      Here I use the array f[m] to represent the number of cases when gcd is a multiple of m

      When we count from back to front: First calculate gcd which is a multiple of 6, ans+=i*f[i], ans+=6*1 ans=6 Next, it is calculated that gcd is a multiple of 3, but at this time multiples of 3 include 6, so ans += i*f[i] cannot be used directly. Need f[3]-=f[6] in front and then calculate ans+= i*f[i] again.

      Is my understanding correct?

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

        The effect of the translation software is not good. I can't guarantee that I understand what you said, but based on your final description, I think your basic counting idea is correct. Do you have instant software contact information? For example discord? If you still have questions, you can add me on discord "cap1tal_lol". Faster communication may reduce misunderstandings caused by translation software. If adding social software friends in this way is offensive to you, or is not allowed by codeforces, please forgive me and ignore what I said. If you have any questions about passing, you can continue to contact me :)

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

      I understand the rest of the solution from the tutorial, but I do not understand how to do this "The first step, as in the solution, is to find all divisors of all numbers in the range in mlogm time". I am just a beginner. What I can think of is that we use seive to find all prime factors for each number, but I do not understand how to go from here to finding all factors.

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

Problem B, I try to test 1 1 11 in everybody AC code and all code print 1 1 1, that a wrong answer.

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

In problem C, I was able to identify the need for DFS. I wrote my code in both Python and C++. Tried both. But got TLE on pretest 2 in Python and TLE on pretest 5 in C++.

Python — https://mirror.codeforces.com/contest/1900/submission/234466578

C++ — https://mirror.codeforces.com/contest/1900/submission/234467214

Any kind of help is greatly appreciated.

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

Anyone solved D using Mobius inversion?

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

      Can you please explain your approach too?

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

        Sorting the array gives the same value of the required sum

        The required sum $$$S$$$ is therefore

        $$$\displaystyle\sum\limits_{i=1}^{n} \sum\limits_{j=i+1}^{n} \sum\limits_{k=j+1}^{n} f(a_i, a_j, a_k)$$$
        $$$\displaystyle =\sum\limits_{i=1}^{n} \sum\limits_{j=i+1}^{n} \sum\limits_{k=j+1}^{n} \gcd(a_i, a_j) = \sum\limits_{i=1}^{n} \sum\limits_{j=i+1}^{n} (n - j) \gcd(a_i, a_j)$$$

        Applying the procedure described in this blog:

        $$$\displaystyle S = \sum\limits_{i=1}^{n} \sum\limits_{j=i+1}^{n} (n - j) \gcd(a_i, a_j)$$$
        $$$\displaystyle= \sum\limits_{k=1}^{M} \sum\limits_{i=1}^{n} \sum\limits_{j=i+1}^{n} (n - j) k [\gcd(a_i, a_j) = k]$$$
        $$$\displaystyle=\sum\limits_{k=1}^{M} \sum\limits_{i=1}^{n} \sum\limits_{j=i+1}^{n} (n - j)\, k \, [\gcd(\frac{a_i}{k}, \frac{a_j}{k}) = 1]$$$

        Applying mobius inversion:

        $$$\displaystyle= \sum\limits_{k=1}^{M} k \sum\limits_{d = 1}^{\lfloor \frac{M} {k} \rfloor} \mu (d) \sum\limits_{i=1}^{n} \sum\limits_{j=i+1}^{n} (n - j) \, [kd \mid a_i] \, [kd \mid a_j]$$$

        Let

        $$$\displaystyle \text{magic}(x) = \sum\limits_{i=1}^{n} \sum\limits_{j=i+1}^{n} (n - j) \, [x \mid a_i] \, [x \mid a_j]$$$
        $$$\displaystyle = \sum\limits_{j=2}^{n} (n - j) \, [x \mid a_j] \sum\limits_{i=1}^{j - 1} \, [x \mid a_i]$$$
        $$$\text{If }\, i_1, i_2, \dots i_m \text{ are the indices of multiples of } x \text{ in the sorted array }$$$
        $$$\text{magic}(x) = (n - i_1)(1 - 1) + (n - i_2)(2 - 1) + \dots (n - i_m)(m - 1) $$$

        Which can be calculated "on the fly", by factorizing each arr[i] and updating magic[f] where f is a factor of arr[i].

        Thus the required sum is

        $$$\displaystyle= \sum\limits_{k=1}^{M} \sum\limits_{d = 1}^{\lfloor \frac{M} {k} \rfloor} \mu (d) \, k \, \text{magic}(kd) $$$

        Calculating magic[x] by factoring in O(n * MAX_FACTORS): Submission

        Calculating magic[x] without factoring in O(M log M): Submission

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

infinite water source ftw

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

D could solved in O(MlogM) without factorizing. Just do the normal trick of counting gcd pairs and count in the contribution of c.

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

    could you please explain?

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

      I will first explain the normal method to count gcd pairs. Typical problem is, given an array $$$a$$$ of length $$$N$$$ with value range $$$[1,M]$$$. Count the sum of $$$gcd$$$ of each pair of elements, formally $$$\sum_{i=1}^N\sum_{j=i+1}^Ngcd(a_i,a_j)$$$.

      Instead of iterating each pair of elements, we calculate the contribution from $$$1$$$ to $$$M$$$ as $$$gcd$$$ of a pair of elements. Define two functions:

      $$$g(i) = $$$ the count of pairs that $$$gcd$$$ is $$$i$$$.

      $$$h(i) = $$$ the count of pairs that $$$gcd$$$ could be divided by $$$i$$$.

      Suppose number of elements that could be divided by $$$i$$$ is $$$cnt_i$$$, then $$$h(i)=cnt_i*(cnt_i-1)/2$$$. By inclusion-exclusion principle, $$$g(i)=h(i)-\sum_{j \gt i\ and\ i|j}g(j)$$$.

      Iterate from $$$M$$$ to $$$1$$$, calculate $$$cnt$$$ and $$$h(i)$$$ with the harmonic trick.

      for (int i = M; i >= 1; --i) {
          int cnt = 0;
          long long h = 0;
          for (int j = i; j <= M; j += i) {
              cnt += c[i];
              if (j > i) h += g[j];
          }
          g[i] = 1ll * cnt * (cnt - 1) / 2 - h;
      }
      

      Now come back to the original problem.

      We need to calculate the contribution of each $$$i$$$ where $$$f(a,b,c)=i$$$. WLOG, suppose $$$a\le b\le c$$$. Suppose we are iterating all multiples of $$$i$$$ and reach $$$j$$$. We fix $$$j$$$ as $$$b$$$. All numbers smaller than or equal to $$$j$$$ and is a multiple of $$$i$$$ could be a choice of $$$a$$$. All numbers greater than or equal to $$$j$$$ is a choice of $$$c$$$. Notice here $$$c$$$ is not required to be a multiple of $$$i$$$. Then we have four cases: $$$a \lt b \lt c$$$, $$$a=b \lt c$$$, $$$a \lt b=c$$$, $$$a=b=c$$$.

      By calculating the count of $$$a$$$, and precalculating the count of numbers greater than each $$$b$$$, each case could be calcualted in $$$O(1)$$$. Then by a similar inclusive-exclusive calculation, the whole problem could be solved in $$$O(MlogM)$$$.

      Here is my code. https://mirror.codeforces.com/contest/1900/submission/234450747

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

ABC was too easy this contest.

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

Hi , can smb tell me whats wrong with dfs that returns the answer in task C , because I have TLE in pretest 5.

include <bits/stdc++.h>

using namespace std;

define endl '\n'

int dfs(int x, int kount,string s, vector<pair<int,int>> v){ if(v[x].first==0 && v[x].second==0) return kount; int ans=1000000000; if(v[x].first!=0){ if(s[x-1]=='L') ans=min(ans,dfs(v[x].first,kount,s,v)); else ans=min(ans,dfs(v[x].first,kount+1,s,v));

}
if(v[x].second!=0){
    if(s[x-1]=='R')
        ans=min(ans,dfs(v[x].second,kount,s,v));
    else
        ans=min(ans,dfs(v[x].second,kount+1,s,v));

}
return ans;

}

void solve(){ int n; cin>>n; string s; cin>>s; vector<pair<int,int>> v(n+1); for(int i=1; i<=n; i++) cin>>v[i].first>>v[i].second; cout<<dfs(1,0,s,v)<<endl;; }

int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; cin>>t; while(t--){ solve(); } return 0; }

P.S it works O(n) and n from the text is <=3*10^5 , I can't understand why it's TL

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

can some one please tell why it is giving TLE on test case 5 and please correct it.

https://mirror.codeforces.com/contest/1900/submission/234547330

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

Problem D can be solved by mobius inversion formula.

$$$ \begin{align} \sum_{i = 1}^n (n - i) \sum_{j = 1}^{i - 1} \gcd(a_i, a_j) &= \sum_k k \sum_{i = 1}^n (n - i) \sum_{j = 1}^{i - 1} [\gcd(a_i, a_j) = k]\\ &= \sum_k k \sum_{\substack{i = 1\\k | a_i}}^n (n - i) \sum_{\substack{j = 1\\k | a_j}}^{i - 1} [\gcd(\frac{a_i}{k}, \frac{a_j}{k}) = 1]\\ &= \sum_k k \sum_{\substack{i = 1\\k | a_i}}^n (n - i) \sum_{\substack{j = 1\\k | a_j}}^{i - 1} \sum_{d | \gcd(\frac{a_i}{k}, \frac{a_j}{k})} \mu(d)\\ &= \sum_d \mu(d) \sum_k k \sum_{\substack{i = 1\\kd | a_i}}^n (n - i) \sum_{\substack{j = 1\\kd | a_j}}^{i - 1} 1 \end{align} $$$

Which can be solved in $$$O(n\log n + n \sqrt{m} + m\log m)$$$. Code: 234463754

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

Note that we need to solve

$$$\sum\limits_{i=1}^n(n-i)\sum\limits_{j=1}^{i-1}\text{gcd}(a_i,a_j)$$$

Use the equation

$$$ n=\sum\limits_{d|n}\varphi(d) $$$

We have

$$$\begin{aligned}&\sum\limits_{i=1}^n(n-i)\sum\limits_{j=1}^{i-1}\text{gcd}(a_i,a_j)\\=&\sum\limits_{i=1}^n(n-i)\sum\limits_{j=1}^{i-1}\sum\limits_{d|\text{gcd}(a_i,a_j)}\varphi(d)\\=&\sum\limits_{i=1}^n(n-i)\sum\limits_{d|a_i}\varphi(d)\sum_{j=1}^{i-1}[d|a_j]\end{aligned}$$$

Pretreatment Euler function so that we can solve the equation in time complexity $$$\mathcal{O}(n\sqrt{n})$$$

234478574

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

Thanks for the fast editorial !

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

Are the "Bonus problems" somewhere for us to submit them??

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

Problems C and E are just... weird... Nothing really to solve, just implement standard algorithms.

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

when you dont try E, because of stucking in d :(((

Seems easy for me the E one. (I solved it in one go)

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

I cannot understand this part in tutorial for E : The edge will have a weight equal to the size of the SCC that it is going into.

Why is that ? Shouldnt all the weights of edges be 0 ? and find the longest path by vertex numbers ?

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

In D how to create that divisor array in NlogN. i am doing it in NrootN.

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

Hi, I have a question related to the time limit in problem C (Anji's Binary Tree).

I got many TLE in submissions like 235439131, 235440112, 235441383. The idea was fine, but after reading some of the comments, I realized that removing some things like memset, global variables, and setting reference variables will get me to the AC (235443694).

I would like to know what kind of aspects I have to think when I want to use memset or other kinds of stuff. Why do the last submissions differ in seconds to each other? What are the technical aspects related to the stuff that I have used in the first submissions? Thanks!

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

    Memset is O(array_size) complexity. So using it for each testcase on array of size 300 000 for 50 000 testcases is just too slow. Always reset arrays by iteratting from begin to N as you know elements after it are unaffected. Rarely you might even need to keep elements you changed in a queue.

    Edit: Also, note that locally declared variables are not guaranteed to have all values set to 0, so they are even worse than forgetting to reset global ones.

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

In question 1 , in 5th test case

10

...#..#.

someone pls explain how the output is 2 ?

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

In Problem C, Test case 3: I can just change the string to LRLR by modifying R -> L and U-> R and reach Vertex 2, that takes 2 modification but Why the answer is 3? Can anyone explain that bit to me?

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

In Problem C, I used memorization to calculate the answer. Although being of time complexity of O(n), It is giving Time limit exceeded for n=10^5. Can someone suggest me the problem?

https://mirror.codeforces.com/contest/1900/submission/239971869

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

haha infinite water glitch go brrr

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

Solution for the problem D Link

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

can someone help why i am getting TLE int this submission[submission:284642031]

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

ohkk i might be making a very dumb mistake but in question A solutions says if the string contain substring '...' then answer is 2 otherwise no. of empty cell. ohkk but how will the distant 2 consecutive empty cell and 1 cell separated by blocks can be filled up

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

For problem D, an alternative solution is to aggressively use prefix sums.

Let $$$\text{Max} = \max{ v }$$$, where $$$v$$$ is the given array.

First, we precompute the frequency array $$$\text{freq}$$$. Then we find its prefix sum array, say $$$\text{pref}$$$. Now, to calculate the initial value $$$X$$$ (before deleting the contribution of multiples — refer to hint 1 / solution to hint 1), for each integer $$$i$$$ ($$$1 \le i \le \text{Max}$$$), we have three sources in the summand: the number of triplets where the least two numbers are multiples of $$$i$$$, for every multiple $$$j$$$ of $$$i$$$ in increasing order.

(a) $$$j$$$ is taken 2 or 3 times. The contribution is:

$$$ \binom{\text{freq}[j]}{3} + \binom{\text{freq}[j]}{2} \cdot (n - \text{pref}[j]) $$$

(b) $$$j$$$ is taken exactly 1 time. The contribution is:

$$$ \sum_{\substack{k \gt j \\ k \bmod i = 0}} \left( \binom{\text{freq}[k]}{2} + \text{freq}[k] \cdot (n - \text{pref}[k]) \right) $$$

The first part is straightforward. For the second part, intermediate vectors of $$$\binom{\text{freq}[r]}{2}$$$ and $$$\text{freq}[r] \cdot (n - \text{pref}[r])$$$ can be computed in $$$\log(i)$$$ time (refer to hint 2), where $$$r$$$ runs across all multiples of $$$i$$$. Let these arrays be denoted by $$$\text{fC2}$$$ and $$$\text{f2Sp}$$$, respectively.

These arrays can be prefixed. Then, iterating over all multiples $$$j$$$ of $$$i$$$, we can compute the contribution from all multiples of $$$i$$$ greater than $$$j$$$ in $$$\mathcal{O}(1)$$$ time using:

$$$ X[i] += \text{freq}[j] \cdot \left( (\text{fC2.back()} - \text{fC2}[ct]) + (\text{f2Sp.back()} - \text{f2Sp}[ct]) \right) $$$

where $$$ct = \left\lfloor \frac{j}{i} \right\rfloor$$$. Here, $$$\text{fC2.back()}$$$ refers to the total sum of the array $$$\text{fC2}$$$, effectively giving us a suffix sum. (It might have been simpler to describe this in terms of suffix sums directly, but that would introduce some index complications.)

The rest of the solution proceeds as in the editorial — we remove the contribution of the greater multiples accordingly.

Thus, the overall running time is $$$\mathcal{O}(m \log m)$$$, where $$$m = \max(v)$$$.

Here is my solution using this approach.