Pa_sha's blog

By Pa_sha, history, 5 hours ago, In English

2008A - Sakurako's Exam

Tutorial
Solution in C++
Solution in Python
Rate the problem

2008B - Square or Not

Tutorial
Solution in C++
Solution in Python
Rate the problem

2008C - Longest Good Array

Tutorial
Solution in C++
Solution in Python
Rate the problem

2008D - Sakurako's Hobby

Tutorial
Solution in C++
Solution in Python
Rate the problem

2008E - Alternating String

Tutorial
Solution in C++
Solution in Python
Rate the problem

2008F - Sakurako's Box

Tutorial
Solution in C++
Solution in Python
Rate the problem

2008G - Sakurako's Task

Tutorial
Solution in C++
Solution in Python
Rate the problem

2008H - Sakurako's Test

Tutorial
Solution in C++
Solution in Python
Rate the problem
  • Vote: I like it
  • +14
  • Vote: I do not like it

»
4 hours ago, # |
Rev. 9   Vote: I like it +14 Vote: I do not like it

Great problem E. I had a little fun and leaked the solution with a useless if statement (that makes no logical sense and that is wrong): if < 2: print(best + 2) and just a bit above that a if n == 1 so that it would still AC even though there is something useless and wrong. This will make it easy to report cheaters. This is a new thing I think we should all do. Leak solutions with easy to spot but hidden watermark for those who don’t understand the algo. https://pastebin.com/VSP4VCne

we just need to crawl all AC and check those with a if n < 2.

This is an example of a cheater that would have never been caught otherwise: https://mirror.codeforces.com/contest/2008/submission/279191114 Because he or she was clever enough to really rework my solution.

Spoiler
  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Great work lmao

»
4 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

In problem F, I guess it should be "divide" instead of "delete first value by second" :)

»
4 hours ago, # |
  Vote: I like it +8 Vote: I do not like it

Great round, I loved G and H very much.

Anyways, I read E wrongly during the round and I assumed we can do deleting operation as much as we want. Can anyone solve it? (if you can solve that, you can solve E easily.)

Spoiler:
  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I solved the same misread on E initially

    It is straight forward dynamic programming after fixing the alternating string characters

»
4 hours ago, # |
  Vote: I like it 0 Vote: I do not like it
  • »
    »
    3 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    try implementing dfs without funtion

    may be recurion calls is taking time;

»
4 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

In the problem G tutorial there should have been k = k — a2 + a1 + 1 instead of k = k — a2 + a1 — 1

»
4 hours ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

In C , Can be solved in $$$\mathcal{O}(\sqrt{r-l})$$$ and can be optimized by binary search in $$$\mathcal{O}(\log (r-l))$$$ using

$$$\frac{n(n+1)}{2}$$$

, and can be optimized to $$$\mathcal{O}(1)$$$ by quadratic equation , Check 279123925

Overall nice contest Pa_sha orz

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    because no one should be solving A like that

    • »
      »
      »
      3 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Optimization when not needed Bad solutions when not intended

    • »
      »
      »
      2 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      At least I solved C assuming $$$(1 \le l,r \le 10^{18})$$$

  • »
    »
    3 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I don't think that last linked submission of yours is constant time. It uses the sqrt() function.

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hi. Your solution for A cannot pass because it is $$$O(t\cdot (a+b)\cdot 2^{a+b})$$$ which is a lot. On maximum tests it is $$$100\cdot 20\cdot 2^{20}$$$ which is approximetly $$$10^9$$$.

    For C you are right. In fact, $$$O(\sqrt{r-l})$$$ is okey for Div3B, so we didn't take it away.

    Thanks for your feedback

»
4 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

please someone explain to me in problem: 2008G — Sakurako's Task. Why the gcd of all numbers is the key, I cant see the idea behind.

  • »
    »
    3 hours ago, # ^ |
    Rev. 3   Vote: I like it +2 Vote: I do not like it

    So there is a basic concept. If I give you 2 numbers like 2 and 3 and say you that you can use 2 and 3 any number of times and you can either add them or subtract them then what are the possible numbers you can generate.

    Answer :- You can generate any integer from 2 and 3.

    Reason :- GCD of 2 and 3 is 1 therefore you can use 2 and 3 to create 1 (3 — 2)

    Now for any integer x you can generate it by using (3 — 2) x times.

    Now take 2 and 4 for example. Their gcd is 2 so you can generate 2 from them (4 — 2). Now you can generate any even number from its gcd by repeating (4 — 2) any number of times but you cannot make any odd number using it.

    Go in G after taking gcd of all, suppose gcd is x therefore optimal array to form will be [x, 2x, 3x, ... ]

    Sorry if I made you more confused by my explanation but if your doubt is not cleared I will try to explain with a different approach.

»
3 hours ago, # |
  Vote: I like it +16 Vote: I do not like it

Updating the tests instead of the model seems like the wrong decision to me (i am biased though)

The last few cases I remember of a wrong model (but there exists a solution), they fixed the model and rejudged the solution

Can you explain why it was not done like that here? I remember an edu round and a div2 round where we got rejudged midcontest after the model was fixed

Vladosiya Pa_sha

Relevant meme :

Situation : Unit tests are failing

Vladosiya and Pasha : delete the unit tests

  • »
    »
    3 hours ago, # ^ |
    Rev. 2   Vote: I like it -7 Vote: I do not like it

    Also, Why isnt there more discussion on this?

    Authors fucking changed constraints midway but a slightly too hard div2C gets more hate???

    • »
      »
      »
      3 hours ago, # ^ |
        Vote: I like it -6 Vote: I do not like it

      You don't need to curse or be rude every time. You're not umnik, and will never be.

      • »
        »
        »
        »
        3 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        you would be pissed off too if you spent 30minutes wondering where your code might go wrong and then it turns out authors cannot write a correct model.

        I do not want to be a umnik, i want to be myself. Cursing comes naturally to me and I dont see the need to restrain from it.

      • »
        »
        »
        »
        64 minutes ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        I think he just accidentally got a little carried away due to his recent rating boosts.

    • »
      »
      »
      3 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      is the div2C ur talking about from recent div 2? cuz i haven't been active lately

      • »
        »
        »
        »
        3 hours ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        just in general, you will find tons of comments complaining about a hard div2C or something.

        But, there are almost no comments complaining about how constraints were changed mid round...

        • »
          »
          »
          »
          »
          2 hours ago, # ^ |
            Vote: I like it +1 Vote: I do not like it

          i think the reason no one complain about the constraint change in this round's G is because the constraint change does not affect the initial solution for the problem pre-change, and all the change did is probably made some coders mildly annoyed because they had written some edge cases for the problem before the change.

          i do agree tho that constraints change midway through contest can be fatal

          • »
            »
            »
            »
            »
            »
            2 hours ago, # ^ |
              Vote: I like it +5 Vote: I do not like it

            You are right that the constraint change isnt the issue

            The issue was that the authors wrote the wrong solution, and thus my correct solution was marked as incorrect before rejudging with the changed test data.

            So it was me who was extra careful, while mostly everyone else wasnt

    • »
      »
      »
      2 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Its probably because its a G, so many didnt even get to it. I personally got to it only after constraints changed, and only realised after contest that anything actually happened. Im not saying this justifies it or anything, just giving an explanation :D

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    Hi, first of all, I want to say sorry to you for this. For me, it is sad to look at all of this, because, I put the most afford in G among all tasts (I think because it has been changed little before contest).

    For me, two solutions (your and what we have done) seem like equally good. I mean, if you know how to solve this problem, you should know how to do with that tests. But if we would notice this test before contest, it would be put in the samples, since in other case there would be wa2 everywhere. But we cannot change samples during the contest. So, I think this decision was better in this case.

    One more time, sorry for inconvinience. We didn't want such think to happend.

    • »
      »
      »
      2 hours ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      it was a great contest, and i found G really enjoyable even though i upsolved it 15 mins after the round, thank you very much!

»
3 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Thank you for fast editorial

»
3 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Hi I am a begginer and I really liked your effort on this round thank you for the answers so I can get the clues of coding

»
3 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Very good problem H!

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Could someone tell me ,for problem E , why this submission is giving wrong answer for test case 97 of test 2 . https://mirror.codeforces.com/contest/2008/submission/279256847

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

I solved problem E in O(nlog(26)) using two sets.

For even ns my solution is basically the same.

For odd ns, I brute forced the index of the character to delete. First I deleted the first character from the string and inserted frequencies of other letters on odd and even indexes in each of these sets. Then for other characters, I updated the set of frequencies by erasing, updating and inserting a frequency of character.

you can also check my code if you're interested: 279194144

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

very nice idea for problem H. also, the sentence "we need to find the smallest element which has at least $$$\big\lfloor \frac{n}{2} \big\rfloor$$$ elements in the array that is less or equal to it" in the editorial for H is kinda confusing since some people might interpret it as "find the smallest element $$$h$$$ which has at least $$$\big\lfloor \frac{n}{2} \big\rfloor$$$ elements in the array that is less or equal to $$$h$$$ including $$$h$$$".

my suggestion is to change it into "we need to find the smallest element such that it has at least $$$\big\lfloor \frac{n}{2} \big\rfloor$$$ other elements in the array that is less or equal to it" or smth along that line. also, i can see that you use translator (probably) to write this edi.

»
99 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

In the editorial solution for F, why do we do

sum=(sum-sumsq+mod)%mod;

instead of

sum=(sum%mod -sumsq%mod);

to the best of my knowledge it is done when we want to take modulo of negative numbers however this doesn't make sense to me here since difference of sum — sumsq can't be negative mathematically , or i may be wrong some where plz help.

  • »
    »
    96 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The latter is also correct

    • »
      »
      »
      89 minutes ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      This code gives WA on 4 but if i change line

      int num = (sum%MOD - pairsum%MOD);

      to

      int num = (sum - pairsum + MOD)%MOD;

      it gets AC'ed why so ?

      #include <bits/stdc++.h>
      #define pb push_back
      #define int long long
      #define all(x) x.begin(), x.end()
      #define rall(x) x.rbegin(), x.rend()
      #define lb lower_bound
      #define ub upper_bound
      #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
      using namespace std;
      #define MOD 1000000007
      
      using namespace std;
      
      int modmul(int a , int b , int mod){
          return (a % mod * b % mod) % mod;
      }
      
      int fastpow(int a, int b, int mod) {
          int ans = 1;
          a = a%mod;
          while (b > 0) {
              if (b & 1) {
                  ans = modmul(ans,a,mod);
              }
              a = modmul(a,a,mod);
              b >>= 1;
          }
          return ans%mod;
      }
      
      
      int inv( int a , int p){
          return fastpow(a , p-2 , p);
      }
      
      
      signed main() {
          ios::sync_with_stdio(false);
          cin.tie(0);
          
          int t;
          cin >> t;
          while (t--) {
              int n;
              cin >> n;
              vector<int> a(n);
              int sum = 0;
              for (int i = 0; i < n; i++) {
                  cin >> a[i];
                  sum = (sum + a[i])%MOD;
              }
              sum = (sum%MOD * sum%MOD)%MOD;
              int pairsum = 0;
              for(int i=0;i<n;i++){
                  pairsum = (pairsum + ((a[i]%MOD*a[i]%MOD)%MOD))%MOD;
              }
              int paircnt = (n*(n-1))%MOD;
              int num = (sum%MOD - pairsum%MOD);
              int paircnt_inv = inv(paircnt,MOD)%MOD;
              num = (num%MOD*paircnt_inv%MOD)%MOD;
              cout << num << endl;
          }
          
          return 0;
      }
      
      
  • »
    »
    78 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Try 1 1 1 1 1 1 1 1000000000

    • »
      »
      »
      67 minutes ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks!

      If i understood it correctly then ->

      sum - sumsq >= 0 but (sum%MOD - sumsq%MOD) might be negative hence will not work.

      • »
        »
        »
        »
        30 minutes ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        My bad, you are correct it's due to negative, because if it was a + sign between the two,your formula would have worked

»
72 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

G — Sakurako's Task gives TLE when using Binary Search but succeeds when I use linear search. Can anyone explain why? Shouldn't the binary search ideally take log(n) time?

Here are the two solutions -

Binary Search — https://mirror.codeforces.com/contest/2008/submission/279260235 Linear Search (as given in tutorial) — https://mirror.codeforces.com/contest/2008/submission/279260712

The diff is in last few lines, where I use either binary search or linear search.

  • »
    »
    57 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    if (k > (n - 1) * (gcd - 1))
    Potential int overflow here.

»
62 minutes ago, # |
  Vote: I like it +1 Vote: I do not like it

imo you should have made l and r constraints on C 10^18, so that linear search doesnt pass, and it would have to be binary search, but great contest nonethelesz