Vladosiya's blog

By Vladosiya, history, 16 months ago, In English

2114A - Square Year

Idea: myav

Tutorial
Solution
Rate the problem

2114B - Not Quite a Palindromic String

Idea: Vladosiya

Tutorial
Solution
Rate the problem

2114C - Need More Arrays

Idea: Vladosiya

Tutorial
Solution
Rate the problem

2114D - Come a Little Closer

Idea: Vladosiya

Tutorial
Solution
Rate the problem

2114E - Kirei Attacks the Estate

Idea: Gornak40

Tutorial
Solution
Rate the problem

2114F - Small Operations

Idea: Vladosiya

Tutorial
Solution
Rate the problem

2114G - Build an Array

Idea: myav

Tutorial
Solution
Rate the problem
  • Vote: I like it
  • +65
  • Vote: I do not like it

| Write comment?
»
16 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

can someone explain me B problem editorial again, am not able to picture the suggested approach?

  • »
    »
    16 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it +12 Vote: I do not like it

    See first understand, if any binary string is there, swapping any two digits can change the number of palindromic pairs by either 0 or 2. Take few examples and try to wrap your head around this fact. You will understand what they are doing after that.

  • »
    »
    16 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it +15 Vote: I do not like it

    There is an easier way to think about this problem.

    For a string of length $$$n$$$, there are exactly $$$n/2$$$ pairs (since $$$n$$$ is guaranteed to be even). So, since you want exactly $$$k$$$ identical pairs, you want exactly $$$n/2 - k$$$ non-identical pairs. Each non-identical pair uses one $$$0$$$ and one $$$1$$$. So you need at least $$$n/2 - k$$$ zeroes and ones, and you will pair them with each other. Now, for the remaining zeroes and ones, you want all zeroes paired up with zeroes, and all ones with ones. This can happen if and only if the remaining number of zeroes and ones are even. So just check that as well. Here is my code for reference. Just look at the solve function.

»
16 months ago, hide # |
 
Vote: I like it +16 Vote: I do not like it

In editorial for problem F, "dp[i]=max(dp[i],dp[j]+1)", shouldn't it be min instead of max, as we desire for the minimum number of operations. Correct me, if I am wrong.

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

About problem F:

  1. I have seen some solution for F passes in $$$O(xloglogx)$$$ time per query.
  2. You can do F in $$$O(tlogt+xlogx)$$$ using offline processing and sieve (link).
  3. F appeared as one of our training problems (I didn't actually do it, might as well be a coincidence).
  4. This DP looks exactly like shortest path on DAG, I solved it using BFS instead.
Suggestion
»
16 months ago, hide # |
 
Vote: I like it +12 Vote: I do not like it

In the Tutorial of F, it should be min(dp[i], dp[j]+1), not max.

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

D was all about implementation and the editorial code is a bit tough to understand for beginners. So, you can refer the code below.

The main idea here is to find the highest and second highest extremes in all four directions — up, down, right and left. Now, the problem is reduced to removing the extreme most monster in all 4 directions one by one and place inside the rectangle formed by taking second most extreme in that direction and extremes in other 3 directions for eg. rectangle area considering second most right extreme, and up,down,left extreme points.

But here it may be possible that a point is extreme for two adjecent directions, ie. a point on top-right corner is extreme for both right and up directions. So here, the rectangle area is considering the extremes in left and down directions and second most extreme in up and right directions. The two if-elses in AreaC() functions check these 4 corner points.

The Areak() function also takes care of the case where the rectangle is already full ,i.e., the monster removed from one of the extremes cannot be placed inside the proposed smaller rectangle, in this case either the width or the height need to increased by 1 to accomodate the ONE monster that is removed.

Note that each of the variables up, down, right and left are a pair of the highest and second highest extremes in the respective direction.

Feel free to suggest any improvements.

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

    yes this is similar to hat I submitted in Py, and is relatively short: 321482410

    Note that the corner case solves itself if you use a set of points and take the min area after removing each

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

Not able to understand F, please help.

I am trying to do it using prime factorisation, but i say comment that for ~20 we can't do bitmask dp, so i'm getting tle with that approach

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

    Seeing that nobody has reposted this idea anywhere...

    Including in a comment above which described 4 ways of solving F

    I had actually explained my approach here

    Do check it out.. Still have queries, then feel free to ask!

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

    You may try BFS

»
16 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

For problem E, I have no idea why I missed the point that path is towards root during contest, and I solved a delusional version for path starting from each node by rerooting...

»
16 months ago, hide # |
Rev. 3  
Vote: I like it -20 Vote: I do not like it

I'm actually surprised about how weak problem C's tests were. The fact that some people who use vector.erase() still passes the contest testcases is crazy

»
16 months ago, hide # |
 
Vote: I like it +22 Vote: I do not like it

G is a cool problem!

»
16 months ago, hide # |
Rev. 3  
Vote: I like it +2 Vote: I do not like it

In D, correct me if I'm wrong, but it is enough to consider at most 4 monsters, with regard to the relocation. By this I mean, the ones on the extremes in all 4 directions. In the worst scenario, you store at most 4 monsters that you want to move and for each, you brute force normally the min and max coordinates for each axis. No need for a multi-set. For me, way easier to implement.

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

In question E, it can also solved by the dp relation f(v) = max(av,av−a(v-1)+f(v-2)) where f(v) — the maximum value of the threat of the vertex and v-1 and v-2 represent the parent and grandparent of node v. We can keep track of both parent and grandparent in vector as we do bfs or dfs down from vertex.

»
16 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

what is a^2 in problem F?

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

    $$$a$$$ is equal to the number of divisors of the number that we want to decompose into the minimum number of divisors, each of which does not exceed $$$k$$$ For numbers up to $$$10^9$$$, a is approximately $$$a = \sqrt[3]{x} = \sqrt[3]{10^9} = 1000$$$, respectively, $$$a^2 = 1000 ^ 2 = 10^6$$$

    You can quickly find the divisors for a single number by factoring it.

    First, we get the factorization of the number in $$$O(\sqrt{i})$$$

    Next, we will create an array $$$d = [1]$$$ — these will be all the divisors of our number $$$i$$$.

    Let's go through all the prime numbers in the factorization and do the following for each of them: Copy $$$d$$$ to $$$tmp$$$, Then multiply each number from $$$d$$$ by our prime number from the factorization $$$d_i\cdot p_j$$$, where $$$p_j$$$ — is a prime number from the factorization, $$$d_i$$$ — our current divisors. And at the end of each iteration, we will copy our divisors from the previous step, $$$d = d + tmp$$$

    Code Python
»
16 months ago, hide # |
Rev. 6  
Vote: I like it +2 Vote: I do not like it

Problem F with sieve of eratosthenes+dp
Time complexity:$$$O(n\sqrt{n})$$$
Sample code:321647158
Observation:First try to make so simple observation,you can see that if there is a prime number $$$p$$$ exist at $$$x$$$ but not in $$$y$$$ then we need to divide it,similarly for multiply,so first we know is if the $$$p \gt k$$$ we have not solution,so let us define two number $$$I$$$ and $$$J$$$,$$$I$$$ is the number x need to multiply and $$$J$$$ is the number x need to divide,if we can get $$$I$$$ and $$$J$$$ then the problem of us will reduce to how i use the number not greater than k to construct the $$$I$$$ and $$$J$$$.
I believe most of you already know the trick,for a standard sieve of eratosthenes(short form SIE),we can build an array in $$$O(NloglogN)$$$ to check a number is a prime or not,but additional we can extend the SIE to built an array $$$minprime[i]$$$ denoted the minimum prime factor in number i,so after this you can just simply do a while loop to prime factolization a number in $$$O(logN)$$$,so first prime factorize $$$x$$$ and $$$y$$$ then compare their prime factor from low to high,if they share common prime factor then just calculate it should be multiply or divide,if they don't share the prime factor also do the same thing then you can get $$$I$$$ and $$$J$$$
After this the problem of us is how to construct $$$I$$$ and $$$J$$$ will least factor such that $$$\forall factor \le k$$$,I will introduce a dp solution,we define $$$dp[i]$$$ is the minimum number to construct number i by using factor less equal than k,then the transition is $$$dp[i]=min(dp[i],dp[j]+1),\forall j\mid i\;and\;\frac{i}{j}\le k$$$ why?,the logic here is we observe that if we want to get $$$i$$$ first we need to reach the factor of $$$i$$$ then we multiply a number and reach $$$i$$$,so if $$$j$$$ can completely divide $$$i$$$ and $$$\frac{i}{j}\le k$$$ then obviously the minimum number to reach $$$j$$$ + one step to $$$i$$$,so we can just brute force the dp solution,and the reason it work and won't TLE is for a number $$$num\le 10^{6}$$$ there is at most 240 factor,so $$$240^2$$$ of a dp solution won't TLE.

»
16 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
  1. Editorial isn't linked on contest page.

  2. I don't understand this hate for D. It was simple to do in constant time, no? 🤥 it took me 5 minutes to do it. Submission

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

can somebody explain me the problem D? I am not able to understand how we are finding the maximum coordinates

»
16 months ago, hide # |
Rev. 2  
Vote: I like it +12 Vote: I do not like it

I solved problem 2114F - Small Operations using simulated annealing, which is certainly not the best way, but it was interesting. My solution 321712725. I create arrays of prime factors to divide and multiply our number by, and then try to greedily divide them into groups with product less than k, starting from the beginning of the array.

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

how do we prove that the greedy way above of finding the max number of operations for G is correct ? why cant we do it in more operations ?

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

In problem G, we can calculate $$$x$$$ using c&-c in $$$O(1)$$$ time, then the algorithm can be finished in $$$O(n)$$$.

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

Problem F, as some people have already mentioned, can be reduced to this problem. However, I did not see any comment explaining a good way to apply this standard technique to the problem F.

The main simplification is the fact that our bits in bitmasks correspond to prime factors of $$$x$$$, and each mask represents multiplication of these prime numbers, which is some factor of $$$x$$$. This way we can get rid of actual bitmasks and bits, and consider only factors and prime factors of $$$x$$$.

Now, the transitions are the pretty much the same as in the linked problem. For each factor of $$$x$$$ we divide it for each prime factor, and update our $$$dp$$$ state for this factor, which is the total number of subsets, and value of the current subset. This gets us $$$O(d(x) * p(x) + sqrt(x))$$$ solution where $$$d(x)$$$ is the number of factors of $$$x$$$, and $$$p(x)$$$ is the number of distinct prime factors of $$$x$$$. Here is my code, but it's pretty messy. Hope this helps.

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

For problem F, why is this solution giving TLE ??

vector<int> p;

void sieve(){
    int n = 1e6+100;
    vector<bool> prime(n, 1);
    for(int i=2; i*i<n; i++){
        if(prime[i] == 0) continue;
        for(int j=i*i; j<n; j+=i){
            prime[j] = 0;
        }
    }

    for(int i=2; i<n; i++){
        if(prime[i]){
            p.pb(i);
        }
    }
}

void solve() {
    ll x, y, k;
    cin>>x>>y>>k;

    ll g = __gcd(x, y);
    x /= g;
    y /= g;

    ll x1 = x, y1 = y;

    int n = p.size();

    for(int i=0; i<n; i++){
        int f1 = 0, f2 = 0;
        if(x%p[i] == 0){
            f1 = 1;
        }

        if(y%p[i] == 0){
            f2 = 1;
        }

        if(p[i] > k && (f1^f2)){
            cout << -1 << endl;
            return;
        }
    }

    ll a = 1,cnt1 = 0;
    if(x1 > 1){
        for(int i=1; i<30; i++){
            a *= k;
            if(a >= x1){
                cnt1 = i;
                break;
            }
        }
    }


    ll b = 1,cnt2 = 0;
    if(y1 > 1){
        for(int i=1; i<30; i++){
            b *= k;
            if(b >= y1){
                cnt2 = i;
                break;
            }
        }
    }


    cout << cnt1 + cnt2 << endl;



    return;
}



int main() {
    boost
    int t = 1;
    sieve();
    cin >> t;
    for (int i = 1; i <= t; i++) {
        // cout << "Case #" << i << ": \n";
        solve();
    }
    return 0;
}

322437145

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

c can be simply solved with stack and returning the size of stack just implementation question of stack