Gellyfish's blog

By Gellyfish, 11 months ago, In English

You could tell people didn't seem to like the match very much. I'm sorry I screwed up again ¯\_(ツ)_/¯

If you're interested, I'd like to share some thoughts I have about this contest.

sad story

2116A - Gellyfish and Tricolor Pansy

Idea: Gellyfish Solution: Gellyfish Prepared by: Gellyfish

Hint 1
Solution
Code

2116B - Gellyfish and Baby's Breath

Idea: Gellyfish Solution: Gellyfish Prepared by: Gellyfish

Hint 1
Hint 2
Solution
Code

2115A - Gellyfish and Flaming Peony

Idea: Gellyfish Solution: Gellyfish Prepared by: Gellyfish

Hint 1
Hint 2
Solution
Bonus
Code

2115B - Gellyfish and Camellia Japonica

Idea: Gellyfish Solution: Gellyfish Prepared by: Gellyfish

Hint 1
Hint 2
Solution
Code

2115C - Gellyfish and Eternal Violet

Idea: Gellyfish Solution: Gellyfish Prepared by: Gellyfish

Hint 1
Hint 2
Solution
Code

2115D - Gellyfish and Forget-Me-Not

Idea: MagicalFlower Solution: MagicalFlower Prepared by: MagicalFlower

Hint 1
Hint 2
Solution
Code

2115E - Gellyfish and Mayflower

Idea: Gellyfish Solution: Gellyfish Prepared by: Gellyfish

Hint 1
Hint 2
Solution
Code

2115F1 - Gellyfish and Lycoris Radiata (Easy Version)

Idea: Gellyfish User Solution: JoesSR, zhaohaikun Prepared by: MagicalFlower

Solution
Code (by zhaohaikun)

2115F2 - Gellyfish and Lycoris Radiata (Hard Version)

Idea: Gellyfish Full Solution: errorgorn Prepared by: MagicalFlower

Solution
Code
  • Vote: I like it
  • +206
  • Vote: I do not like it

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

Cool! The problems are interesting.

»
11 months ago, hide # |
 
Vote: I like it -51 Vote: I do not like it

I think you should swap C and D because C is extremely difficult while D is relatively easy. If there were no C, I believe I could solve problems D and E. Additionally, if you had ever solved this problem, you could probably solve E in just a few minutes.

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

Can anyone tell me what does this pipe sign indicate "x | ai" does it mean x is multiple of ai? or it has other meaning

edited: Thanks phongnc

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

    quite the opposite: $$$a_i$$$ is a multiple of $$$x$$$, or $$$x$$$ divides $$$a_i$$$

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

I solved only 1 problem in Div.1 T_T

It's too hard

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

In Div1-C, if I declare the dp array dp[m][h][n] like this, it gets TLE.

But if the array is declared dp[m][n][h] in this format, it passes in 750 ms.

Can someone explain this?

  • »
    »
    11 months ago, hide # ^ |
     
    Vote: I like it +30 Vote: I do not like it

    It's cache. When you are consecutively using memories in the cache, it kind of speeds up your code by decreasing the constant of your code. I'm not quite an expert on this, I hope my ''blurred'' explanation does help a bit(

    • »
      »
      »
      11 months ago, hide # ^ |
       
      Vote: I like it +7 Vote: I do not like it

      As far as I know multi-dimensional array are represented as linear array in memory. So both array should use contiguous blocks of memory.

      • »
        »
        »
        »
        11 months ago, hide # ^ |
         
        Vote: I like it -14 Vote: I do not like it

        computer archi er course koren, oitay ei jinisher answer ase:)

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

        yes, but in your second case, dp[m][n][h] and dp[m][n][h+1] are adjacent in memory, meanwhile the same states in your original solution were not (dp[m][h][n] and dp[m][h+1][n]). This can cause speedups because when you access a memory address, the contents of addresses that are nearby are also brought to cache.

        • »
          »
          »
          »
          »
          11 months ago, hide # ^ |
          Rev. 4  
          Vote: I like it 0 Vote: I do not like it

          While I was using the first array, I accessed dp[m][h][n + 1] right after dp[m][h][n].The actual reason is something else. Actually most of the time both array gives almost equal performance. But when n is 1 only then the second array gives better performance, as n is constrained over all testcase but m and h is not. Let's explain why second one gives better performance when n is 1.

          Let's denote first dp array as dp1, second one as dp2, the iterator over m is i, the iterator over h is j and the iterator over n is k

          dp1[4000][400][20]

          dp2[4000][20][400]

          The distance between dp1[i][j][k] and dp1[i][j][k + 1] is 1, but this transition will never occur when n = 1. Every time the transition will be from dp[i][j][1] to dp[i][j + 1][1] the distance between dp1[i][j][1] and dp1[i][j + 1][1] is 20.

          But, in case of second dp array the distance between dp2[i][1][j] and dp2[i][1][j + 1] is 1. So only in this case second dp array will give better performance because of cache speedup.

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

Solution codes would be very appreciated (mostly for Div2D)

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

but sadly 2115C - Gellyfish and Eternal Violet was much harder than expected.

I don't think it's very hard, the idea is like 2100-2200 level, but the hardest part of the problem by far is to figure out all the details and implement correctly, which isn't very easy given weak samples (for example all samples had minimum element $$$\le 2$$$, so dp bugs were unlikely to be caught) and only 2 hours. Also edge cases like $$$p = 0, p = 100, n = 1$$$ didn't help, along with $$$O(tm^2)$$$ being disallowed for calculating coefficients. I think it would've been better to just replace this problem or somehow make it less implementation heavy, though I'm not sure how to do it.

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

can someone explain the second test case of D1 C.why is it 1-(0.8)^5

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

Div 2C / Div 1A could also be done via brute force for finding minimum operations for getting an element = gcd.

AC Submission: 322311151

This works because ai <= 5000 and this guarantees atmost 5 operations required (2 * 3 * 5 * 7 * 11 * 13 > 5000). Also the number of unique elements possible in a would also be constrained as minimum number of operations increases. Have not proved rigorous bounds for this, so someone can attempt hacking the solution if u feel it will TLE.

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

I do not think there is any problem of 2116C - Gellyfish and Flaming Peony, I don't preprocess gcd, but my submission pass within about 1s, which is the half of TL.

322347081

So I think TL is enough.

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

I'm a little confused about the analysis of the F2 solution. I don't understand why the query time is small. In the get_val function, when we successfully return a value, is there an upper bound on the recursion depth? Can we build a long chain where $$$T_x$$$ is always empty but we always have to keep recursing into $$$S_{v_{x,1}}$$$?

int get_val(int u) {
	if(a[u].exit == 0) return 0;
	if(a[u].val != 0) return a[u].val;
	if(a[u].fa.empty()) return 0;
	int ans = 0;
	while(1) {
		ans = get_val(a[u].fa.front());
...
»
11 months ago, hide # |
Rev. 2  
Vote: I like it +22 Vote: I do not like it

Can someone please tell me why the first code for D1 A gets TLE and the other one passes in < 500ms? They are both $$$O(nA)$$$.

322302212

322302440

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

In fact it was still possible to get AC in Div2C with a $$$O(n^2 log(max(a)))$$$ solution.

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

How to solve bonus Div2C/ Div1A $$$?$$$

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

    I guess we just need to replace the bruteforce O(N*V) DP with CF1043F(the exactly identical subproblem of 1A which requires nlogv complexity)

»
11 months ago, hide # |
 
Vote: I like it -6 Vote: I do not like it

1C is great, it's quite an interesting question. There's no need to blame yourself. This match is already excellent enough.

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

How to solve the 1A enhanced version mentioned in the solution?

Try to solve $$$n, a_i \leq 2 \times 10^5$$$ with only one test case.

  • »
    »
    11 months ago, hide # ^ |
     
    Vote: I like it +25 Vote: I do not like it

    try to find the set of numbers with dp[x] = i + 1 from the set of numbers with dp[x] <= i. You can do it in $$$O(A log A)$$$ with some PIE. Repeat $$$O(log A)$$$ times. 322329305

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

The problem with the original constraints on C is not because solutions with worse complexity passs with "extreme constant optimization", it's because solutions with worse complexity pass way too easily. While testing I wrote the following very simple $$$O(hm^2)$$$ code that was as fast as the model solution on the original constraints.

code

It is obvious that this solution requires much less effort to come up with and write compared the the intended one. You don't need crazy constant optimization techniques to make it pass. Therefore I think it is reasonable to change the constraints and TL to separate these two.

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

Here's a way to do D1D that seems different and easier (I'm not sure if I fully understand the editorial though so it might be the same solution under the hood):

Transform $$$a_i,b_i$$$ into $$$c_i=a_i\oplus b_i$$$ and initialize $$$x$$$ as $$$\sum a_i$$$ as in the editorial. Solve for the 60th bit first, say $$$i_1,\ldots,i_k$$$ are the indices such that $$$c_{i_1},\ldots,c_{i_k}$$$ have the 60th bit set. Then, depending on whether $$$x$$$ is set on the 60th bit and whether Flower or Gellyfish controls $$$c_{i_k}$$$, we have a constraint of the form "we should include an even/odd number of elements among $$$c_{i_1},\ldots,c_{i_k}$$$". The point of this constraint is that the choice of including each of $$$c_{i_1},\ldots,c_{i_{k-1}}$$$ will toggle whether we decide to include $$$c_{i_k}$$$. If we should include an even number of elements among $$$c_{i_1},\ldots,c_{i_k}$$$, we can enforce this by adding $$$c_{i_k}$$$ to $$$c_{i_1},\ldots,c_{i_{k-1}}$$$, and then deleting $$$c_{i_k}$$$ (set it to zero). If we should include an odd number of elements among $$$c_{i_1},\ldots,c_{i_k}$$$, then do the same thing but first set $$$x$$$ to $$$x\oplus c_{i_k}$$$. Now solve for the 59th bit on the modified $$$c$$$ array and so on.

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

Div 2. Problem C: Gellyfish and Flaming Peony

For each prime p_k, define a set S_k, which is a subset of the array a. First, divide each element a[i] by their greatest common divisor g, i.e., set a[i] = a[i] / g. Then perform prime factorization on each a[i], expressed as a product of prime powers p_ij^b_ij. If for some j, the exponent b_ij = 0, then a[i] belongs to the set S_j.

This reduces the problem to a Hitting Set problem: the universe is the array a, and each S_k is a subset.

Solving this problem is somewhat complicated. Considering a[i] ≤ 5000 and the number of prime factors j ≤ 5, one can use a bitset c_i to represent the subset status corresponding to a[i]. Then perform one-dimensional dynamic programming (DP) in descending order of c_i. Here, the DP array dp[c_i] represents the minimum number of elements needed to hit the subsets represented by c_i. Alternatively, Integer Linear Programming (ILP) can be used to solve the problem.

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

can some one please explain me the div2 D problem statement-

for this input

30 30

585394845 663181874 674985388 534304106 534304106 280872037 534304106 118807993 280872037 280872037 460630149 405335374 23982539 584709287 534304106 280872037 534304106 534304106 521918620 884592393 772438410 378525108 663181874 280872037 534304106 585394845 378525108 280872037 180180388 180180388

16 30 21

13 30 3

7 30 28

3 30 23

12 30 7

25 30 1

23 30 22

21 30 15

9 30 7

8 30 4

22 30 24

9 30 15

21 30 2

23 30 15

28 30 3

7 30 10

21 30 16

25 30 20

26 30 27

18 30 17

14 30 29

16 30 30

8 30 10

20 30 4

15 30 4

10 30 10

11 30 6

18 30 1

25 30 24

17 30 28

it says it has the solution 585394845 663181874 674985388 405335374 1 1 405335374 1 1 280872037 460630149 405335374 23982539 584709287 1 534304106 1 1 1 884592393 772438410 180180388 521918620 405335374 585394845 1 378525108 118807993 1 1

but when i apply the operation on result i dont get the input

shouldnt its ans be -1?

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

    Where did u get this testcase? My accepted code gives -1 as answer. Maybe u can hack that(or possibly my XD) code.

    btw learn to insert code chart like this

    30 30
    585394845 663181874 674985388 534304106 534304106 280872037 534304106 118807993 280872037 280872037 460630149 405335374 23982539 584709287 534304106 280872037 534304106 534304106 521918620 884592393 772438410 378525108 663181874 280872037 534304106 585394845 378525108 280872037 180180388 180180388
    16 30 21
    13 30 3
    ...(no need to write so long)
    
    • »
      »
      »
      11 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Well i tried to hack myself with this testcase and failed, so the ansewr is -1 after all.

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

      sorry brother pasted the wrong testcase orignal testcase is this 30 30

      585394845 663181874 674985388 534304106 534304106 280872037 534304106 118807993 280872037 280872037 460630149 405335374 23982539 584709287 534304106 280872037 534304106 534304106 521918620 884592393 772438410 378525108 663181874 280872037 534304106 585394845 378525108 280872037 180180388 180180388

      16 21 15

      13 3 19

      7 28 6

      3 23 29

      12 7 8

      25 1 26

      23 22 30

      21 15 18

      9 7 9

      8 4 22

      22 24 17

      9 15 23

      21 2 23

      23 15 25

      28 3 9

      7 10 16

      21 16 28

      25 20 4

      26 27 22

      18 17 12

      14 29 19

      16 30 29

      8 10 16

      20 4 7

      15 4 17

      10 10 24

      11 6 8

      18 1 5

      25 24 9

      17 28 6 (it is the 1st testcase for 3rd set) expected output 585394845 663181874 674985388 405335374 1 1 405335374 1 1 280872037 460630149 405335374 23982539 584709287 1 534304106 1 1 1 884592393 772438410 180180388 521918620 405335374 585394845 1 378525108 118807993 1 1

      my solution gives -1 (:

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

I have a solution for div2C bonus. In short the solution's 322468958 (just change the constants).

Basically I try to use bfs and shortest path. Let $$$\gcd(a_i, a_j) = g$$$, then $$$g$$$ can only be a divisor of $$$a_i$$$ and $$$a_j$$$ there aren't that many divisors of a number. So I make a directed graph of edges $$$(x, y)$$$ if there exist $$$i$$$, so $$$\gcd(a_i, x) = y$$$.

We want to find the least subset with $$$\gcd$$$ of $$$1$$$. Then we need to find subset, so intersection of elements' primes is empty. That means if $$$a_i = p_1^{a_1}...{p_k}^{a_k}$$$ we can make it $$$a_i:=p_1...p_k$$$, as we do not care about primes powers.

So the problem is for each $$$x$$$ find all $$$y$$$, so for some $$$i$$$ we have $$$\gcd(a_i, x) = y$$$. I bruteforce $$$x$$$, and consider its divisors in an decreasing order. Let $$$dp_i$$$ is the number of $$$j$$$, so $$$\gcd(a_j, x) = i$$$. Then to find $$$dp_i$$$ we add the number of $$$a_j$$$ that $$$i$$$ divide and substract extra, which is $$$dp_k$$$ for all divisors $$$k$$$ of $$$x$$$ that $$$i$$$ divide. Math formula $$$dp_i = p[i] - \sum\limits_{\forall \,k | x \,\text{and} \,i | k} dp_k$$$.

Let's calculate the complexity. For each $$$x$$$ we look for all its divisors $$$div$$$, and for them for number of theirs divisors. The number smaller than $$$2 \times 10^5$$$ has atmost $$$6$$$ primes. Then the sum of number of divisors for each divisor of a number is atmost $$$\sum\limits_{i = 0}^k (\mathrm{C}_{k}^{i})^2 = \mathrm{C}_{2k}^{k} \le 1000$$$. So at most $$$2 \times 10^8$$$ operations, which should somewhat work in $$$2$$$ seconds. I tried it out at codeforces custom runner, it ran fast, but I used random tests.

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

Can anyone tell me why does this submmission 322398512 for div2C passes as i didn't precompute the gcd values as expected by the author

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

Can anyone help me spot what's wrong with my solution for https://mirror.codeforces.com/contest/2116/problem/D?

One can see that if there exists an array a satisfying the properties in the problem, then one can also find an array a' satisfying those properties, but also satisfying set(a') = set(b) (1).

We visualize the problem as a graph of variables where we put an arrow x -> z and y - > z if ever there was an operation z = min(x, y). This graph will have n + q nodes. Our goal is to "recover" those variables using greedy "backprop": find the largest value among b[i]-s and simply fill variables that b[i] depends on (those variables can't be larger than that b[i] because of (1).

https://mirror.codeforces.com/contest/2116/submission/322482479

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

i think the C is a good problem, but the time limit is too strict

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

Hi, In div2 b I am not writing if else statement and using: cout<< max(power(2,p[i]) + power(2,q[k-i]), power(2,p[k-j])+ power(2,q[j]))%Mod<< " ";

But it is failing and if else below code is working. Could someone please help me?

for (int i = 0, j = 0, k = 0; k < n; k++) {
        if (p[k] > p[i]) i = k;
        if (q[k] > q[j]) j = k;

        if (p[i] != q[j]) {
            if (p[i] > q[j])
                cout << (power(2, p[i]) + power(2, q[k - i])) % Mod << " ";
            else
                cout << (power(2, q[j]) + power(2, p[k - j])) % Mod << " ";
        } else {
            cout << (power(2, p[i]) + power(2, max(q[k - i], p[k - j]))) % Mod << " ";
        }
       // My code:  
     // cout<< max(power(2,p[i]) + power(2,q[k-i]), power(2,p[k-j])+ power(2,q[j]))%Mod<< " ";
    }
»
11 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

I solves Div2C without using dp, i found the gcd of all element first, and then find the minimum gcd of 2 numbers using O(n^2log(max(ai))), and then I use a greedy algorithm to find the minimum value of current gcd and every number in a, Until it reaches the gcd of all element, then calculate the minimum number using the number of times it needed to operate.

Here is my submission (it passes with sightly more than a second :D ) 322270354

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

I have a solution for div2C/div1A bonus in $$$O(maxA * logN * logmaxA)$$$ time: 322504352.

Instead of finding minimum number of operations, I find the smallest subset where the $$$gcd$$$ equals $$$g$$$. To achieve this, I used binary search combined with Inclusion — Exclusion principle.

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

    this will actually fail for cases where the count is a multiple of mod 1e9+7 then your check may return false , but actually its true .There are maybe weak test cases in the problem

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

Why editorials('tutorials') are not showing up in problem's 'contest material' sections?

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

In problem C (Div 1), these lines:

double r = 0;
for(int x = 0 ; x <= min(i - s, m) ; x ++) r = max(r, g[k - i][0][m - x]);

should be: r = g[k — 1][0][m — min(i — s, m)] Instead of iterating to find the maximum, it's more efficient to directly use this.

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

div1 A can be solved using gcd-convolution: submission

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

In Div1D, the editorial claims that $$$f(x + y) = f(x) + f(y)$$$. But this implies that $$$f(x) = f(0) + f(x) \implies f(0) = 0$$$; this isn't true, right? Suppose that there is a single nonzero vector in $$$c_i$$$, and that it is controlled by Flower. Then Flower will use that vector and the answer will not be $$$0$$$, contradicting that $$$f(0) = 0$$$.

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

I see that some people submitted solutions to F2 that don't appear equivalent to the editorial at first glance, mind giving a brief explanation of what you did? Nachia turmax maspy

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

    My solution appears to follow the same fundamental approach as the F1 editorial. I use a balanced binary search tree and a bitset<B> for block-level simulation. For value-finding queries, I use bitset::_Find_First(), which results in a time complexity of $$$O(n^2/w)$$$.

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

    I have the same as F1 too with bitsets with complexity $$$O(n\sqrt{\frac{n}{w}})$$$ (but tbh i implemented it worse and with complexity $$$O(n\sqrt{\frac{nlog(n)}{w}})$$$).

    Upd: No, it is just $$$O((n+q)\sqrt{n+q})$$$.

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

      If I'm understanding correctly, the time complexity of your solution still looks proportional to $$$Q\sqrt Q$$$? In each loop iteration, you have the following two parts, neither of which is reduced by word size:

      • makeotm which takes $$$B$$$ time
      • for(int i1=0;i1<i/B;++i1) which takes $$$i/B$$$ time.

      322376847

  • »
    »
    11 months ago, hide # ^ |
     
    Vote: I like it +20 Vote: I do not like it

    Mine is $$$O(q (\log q)^2)$$$ .

    Consider a segment tree of the query sequence. Each node is evaluated as soon as its children are filled. Each node stores a permutation of intervals (of the original sequences) and is-reversed flag for each interval. A node of length $$$k$$$ is evaluated in $$$O(k \log k)$$$ time.

    Also, an exist flag is assigned to an interval in a leaf node corresponding to an insert query. These flags are aggregated over the segment tree. An exist flag in a middle node is removed when both the corresponding intervals in the children become to have no exist flags. Removing is irreversible so the cost of delete queries sums up to (number of intervals) = $$$O(q\log q)$$$ .

    Querying is a straightforward thanks to exist flag, $$$O((\log q)^2)$$$ time because of the segment tree traversal and the binary search of intervals.

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

My solution to 1D somehow avoids requiring knowledge of XOR basis:

Let's look at the highest bit $$$\text{bit1}$$$, and consider the largest $$$i$$$ such that $$$a[i] \land \text{bit1} \neq b[i] \land \text{bit1}$$$ ($$$\land$$$ means AND because I couldn't figure out how to type an & in math mode). Whoever can make a move on this turn gets to choose whether $$$\text{bit}$$$ is on in the final answer or not.

Now let's do the same for the next highest bit $$$\text{bit2}$$$. Unfortunately, there's an issue if the largest index matches the previous one — we already know whether it flips or not depends on if $$$\text{bit}$$$ is on in $$$x$$$. Therefore, the person playing on this turn will either want $$$(x \land \text{bit1}) \oplus (x \land \text{bit2})$$$ to be $$$0$$$ or $$$1$$$. To "set this up" using a previous column, we'll now care about the largest $$$j \lt i$$$ such that $$$(a[j] \land \text{bit1}) \oplus (a[j] \land \text{bit2}) \neq (b[j] \land \text{bit1}) \oplus (b[j] \land \text{bit2})$$$.

Now let's do the same for the next highest bit $$$\text{bit3}$$$. Unfortunately, now it gets really confusing since the index can collide with the previous two indices...

After some thinking, we can handle everything as follows: let $$$f(x, m)$$$ be (__builtin_popcountll(x & m) & 1). We'll iterate through the bits in descending order and keep track of the set $$$S$$$ of indices that we've picked. For every $$$i \in S$$$, we maintain two values $$$\text{mask}(i)$$$ and $$$\text{target}(i)$$$. This means that on turn $$$i$$$, the player will choose between $$$a[i]$$$ and $$$b[i]$$$ depending on whether or not $$$f(x, \text{mask}(i)) = \text{target}(i)$$$.

For each bit $$$\text{bit}$$$, we'll scan from right to left in the array, and look for the first index $$$i$$$ that satisfies $$$f(a[i], m) \neq f(b[i], m)$$$, where $$$m$$$ is initially $$$\text{bit}$$$. If we ever run into a collision with some index $$$j$$$, we can replace $$$m$$$ with $$$m \oplus \text{mask}(j)$$$, and look for the next largest index $$$ \lt j$$$ that satisfies the new condition and so on. Suppose that we've finally found some $$$i \notin S$$$ that fulfills the condition. The value of $$$\text{mask}(i)$$$ should just be $$$m$$$, but what about $$$\text{target}(i)$$$? This is admittedly really confusing to compute, but thankfully, we can try setting it to both values and simulating the game with the rules we've found in $$$S$$$ so far. We can decide $$$\text{target}(i)$$$ simply based on whose turn it is and which simulation produced a number with $$$\text{bit}$$$ on.

The implementation is surprisingly short: https://mirror.codeforces.com/contest/2115/submission/322602866

Admittedly, at least for me, this seems unusually difficult to come up with in 2 hours, so I'm guessing the XOR basis-dependent solution is the more "proper" one to think of (guessing because I haven't thought about that one yet).

  • »
    »
    11 months ago, hide # ^ |
    Rev. 3  
    Vote: I like it +8 Vote: I do not like it

    Here's another solution that I don't think requires thinking explicitly about XOR basis (though it ends up being quite related), and I think is easier to understand:

    322674146

    Basically, let $$$Z_i=A_i\oplus B_i$$$ for all $$$i$$$, and $$$x=A_1\oplus A_2\oplus \dots \oplus A_N$$$. Then iterate over the $$$Z_i$$$ in reverse order and update $$$x$$$ / $$$Z_i$$$. For example,

    • If $$$Z_N=0$$$, then nothing happens.
    • Otherwise, if $$$C_N=0$$$, then we will always choose to XOR $$$x$$$ by $$$Z_N$$$ if it makes the result smaller. This is equivalent to setting $$$x=\min(x, x\oplus Z_N)$$$ and $$$Z_i=\min(Z_i,Z_i\oplus Z_N)$$$ for all $$$i \lt N$$$.
      • Note: This is equivalent to removing the MSB of $$$Z_N$$$ from all numbers.
    • If $$$C_N=1$$$: Same but we set $$$x=\max(x, x\oplus Z_N)$$$
      • Note: This is equivalent to removing the MSB of $$$Z_N$$$ from all numbers, but ensuring it is set in $$$x$$$.

    The number of nonzero $$$Z_i$$$ is bounded by the number of bits.

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

      You can also think of the min/max portion as setting the bit in $$$x$$$ to whatever the bit in $$$C_N$$$ is

    • »
      »
      »
      11 months ago, hide # ^ |
       
      Vote: I like it +10 Vote: I do not like it

      Seems like 322284403 by ikaurov kind of combines both of these ideas. (Basically instead of iterating over the $$$Z_i$$$ in reverse order, do them in order of highest bit, prioritizing the rightmost one for each bit. And then to force the parity of $$$Z_i$$$ to be correct, do the same trick of setting $$$x = \min(x, x \oplus Z_i)$$$, and $$$Z_j = \min(Z_j, Z_j \oplus Z_i)$$$ for all $$$j \neq i$$$. Finally, remove $$$Z_i$$$.)

      (Congrats on IGM btw! And yes, I stalk your submissions quite often LOL)

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

can anyone tell me where my code in DIv2 B going wrong? Initially even though loigc were exactly same but official solution implemented it a bit differently. Even changing to that is giving me WA for some reason. this is my solution: https://mirror.codeforces.com/contest/2116/submission/322560858

Thanks in advance.

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

Why my $$$O(nmh)$$$ solution TLE?

submission

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

The problems were very good (I especially enjoyed B). C was just more difficult than usual, but don't beat yourself over this ! The problems themselves were quite high quality

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

Hi, For C Problem, any idea why below code is giving TLE

APPROACH:

Step 1: Calculate GCD of full array.

Step2: Checking if fullGCD is alredy present in array if present print ans in O(1) and return.

Step3: Try multisource BFS over all elements to calculate number of steps for each possible GCD with array elements. Then print ans in O(1).

Link: https://mirror.codeforces.com/contest/2116/submission/322759756

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

I really liked the solution for Div 1B/Div 2D. Is there a special name for this technique?

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

can somebody help me what is wrong with my program for the problem — B , It is giving wrong ans in test case 3, I am not able to understand what is the problem, I am sharing the submission link, — https://mirror.codeforces.com/contest/2116/submission/322884021. Hoping for help

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

    like this case

    32 22 13 20
    21 32 8  29
    

    In this case, most probably your ans would come wrong.You will understand when you work out the example on your own.

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

      Ok, I will go through it and try to understand, btw thankyou

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

        You have implemented in a correct way..but there is a mistake you are comparing numbers after taking MOD so it maybe wrong.The example given may be correct as you're checking it correctly...

        Hint

        Sorry for not checking properly.

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

In problem 2115 C, what's the meaning of the $$$g[i][j][k]$$$ ? I can't understand.

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

Is there anyone available to help me to solve Problem B? I am getting wrong answer on tast case 3. My link is: https://mirror.codeforces.com/contest/2116/submission/323347026

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

This is some text. HEY , FIRST OF ALL I AM SEEKING SORRY IF I MADE ANY ANY MISTKAE , BUT IN MY LITTLE BRAIN I GET A POINTS WHERE THE SOLTUION OF MANY PEIOPLE FOR PROBLEM HAS FAIL //here is the code

// this is code
LIKE #include <bits/stdc++.h>
using namespace std;
int N=(int)1e5;
vector<int>power(1e5+1);
const int mod=998244353;
// it shopuld be faile in this test case
//1
// 2
// 2 2
// 0 1
// 5 5 
// PS C
// but correct output for r1=2^2+2^1==6
//hower this code passed how i do not know lol waek test
#define int long long
void paw(){
    power[0]=1;
    for(int i=1;i<=N;++i){
        (power[i]=power[i-1]*2)%=mod;
    }
    return ;
}
void solve(){
    int n;
    cin>>n;
    vector<int>vec(n),vec2(n);
    for(auto &i:vec){
        cin>>i;
    }
    for(auto &i:vec2){
        cin>>i;
    }
    int mx1=-1,  mx2=-1, ind1=0, ind2=0;
    for(int i=0;i<n;++i){
        // if(vec[i]==mx1){
        //     (vec2[i-ind1]<=vec2[0]?ind1=i:ind1=ind1);
        // }
        // if(vec2[i]==mx2){
        //      (vec[i-ind2]<=vec[0]?ind2=i:ind2=ind2);
        // }
        if(vec[i]>=mx1){
            mx1=vec[i];
            ind1=i;
        }
        if(vec2[i]>=mx2){
            mx2=vec2[i];
            ind2=i;
        }
        if(mx1==mx2){
            int ans=-1;
            if(vec2[i-ind1]>=vec[i-ind2]){
                ans=(power[mx1]+power[vec2[i-ind1]]);
    
            }
            else{
                ans=(power[mx2]+power[vec[i-ind2]]);

            }
            cout<<(ans%=mod)<<" ";
        }
        else if(mx1>mx2){
            int ans=((power[vec[ind1]]+power[vec2[i-ind1]]));
            cout<<(ans%=mod)<<" ";
        }
        else{
            int ans=((power[vec2[ind2]]+power[vec[i-ind2]]));
            cout<<(ans%=mod)<<" ";
        }
        
    }
    cout<<"\n";

    return ;
}
int32_t main(){
    paw();
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

The rest of the text.

if i excute the coomment parth output is r1=6 else 5 both code got Ac pls any may clear my litlle;s brain weak intuition pls ; thanks in advance cutie :) close.

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

It might be helpful to look at https://atcoder.jp/contests/agc045/tasks/agc045_a before looking at D. Very similar problem but an easier version. Nice problem!

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

can anyone help me with my code of B and why is failing on test 3. I am pretty sure its because of Mod but I have tried enough I couldn't fix it. submission: https://mirror.codeforces.com/contest/2116/submission/324570618

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

In the Problem C : Gellyfish and Flaming Peony I am getting TLE on using long long instead of int.

Accepted: 324649684

TLE: 324649318

The only difference is: Screenshot

Can anyone explain why?

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

Can anyone help me with problem B.

auto f = [&](int x, int y)
    {
        return (binpow(2, x, mod) + binpow(2, y, mod)) % mod;
    };

    cout << f(p[0], q[0]) << " ";
    mpp[p[0]] = 0;
    mpq[q[0]] = 0;

    rep(i, 1, n - 1)
    {
        mpp[p[i]] = i;
        mpq[q[i]] = i;

        auto [mxp, ip] = (*mpp.rbegin());
        auto [mxq, iq] = (*mpq.rbegin());

        // THIS WORKS
        if (mxp > mxq)
            cout << f(p[ip], q[i - ip]) << " ";
        else if (mxq > mxp)
            cout << f(q[iq], p[i - iq]) << " ";
        else
            cout << f(p[ip], max(q[i - ip], p[i - iq])) << " ";

        // // THIS DOES NOT WORK WHY
        // int vp = f(p[ip], q[i - ip]);
        // int vq = f(q[iq], p[i - iq]);

        // cout << max(vp, vq) << " ";
    }
    cout << nl;

why the upper code works and the commented one does not? What I am thinking is that ultimately in the commented code I am getting the max of the two conditions right that is what we want "the max". then why it fails?

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

Gellyfish and Forget-Me-Not I don't undestend why ****""it is easy to observe that the new function g(x) (before this decision) remains a affine transformation, satisfying g(x)=g(x+ci)"" . what is g(x)?

why affine transformation while x is a number not a vector

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
Choosing ci
result is f(x+ci)=f(x)+f(ci)+b . -why b is constant?
»
10 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

In div 2 C, why it's safe to perform this:

"The transition is simple, just enumerate $$$x$$$ from largest to smallest, and then for all $$$i$$$, use $$$f_{x}+1$$$ to update $$$f_{gcd(x,a_{i})}$$$."

This transition is assuming that all initial $$$a_i$$$ will be unchanged and available, but on each action, we are altering $$$x$$$ to $$$gcd(x,a_{i})$$$ and "destroying" $$$a_i$$$?

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

    That is a great valid question. It is because the answer of different pairs is same as gcd of all those combined elements. Your concern is :- {a1,a2,a3,a4,a5,a6} Let us say best answer follows this way :- Step 1 :-> a4 = gcd(a3,a4) Step 2 :-> a2 = gcd(a1,a2) Step 3 :-> a2 = gcd(a2,a4) -> Total 3 steps gives the best minimum answer -> assume it for this testcase. Now the worry arises because we are not comparing the middle values with each-other in the algorithm -> basically gcd(a3,a4) vs gcd(a1,a2) are not mixing up with each other -> because our current algorithm only compares x with all other numbers in the array -> x does not compare with other intermediate y which would have been achieved by some other operations. But all this still works because you can get job done in 3 operations by not changing any of the other elements except the current element i -> Step 1 :-> a4 = gcd(a3,a4) Step 2 :-> a4 = gcd(a1,a4) Step 3 :-> a4 = gcd(a4,a2) -> we achieve our goal in 3 steps and see we had to change no element in the entire process except the 4th element :) And yes if 4th element is changing its fine because if any future operation contains it — as gcd(small,big) == small -> big is the earlier number at index 4 and small is the new number at index 4 after some operation/operations is/are done. Even thinking this is not needed. You can just assume that you can reach any number "u" by changing just 1 index and making no change ever to any other indices. Assuming creating u was possible in first place for it

    Hope it helps :)

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

gays pleas pleas some one tell me where is the pug in my coode problem is B. Gellyfish and Baby's Breath this my code
~~~~~ Your code here... ~~~~~

include

include <bits/stdc++.h>

include <memory.h>

include

define ll long long

define yes cout<<"YES"<<endl;

define no cout<<"NO"<<endl;

define br cout<<endl;

define he cout<<"here"<<endl;

define pb push_back

include

include

define mod 1000000000+7//if the answer negative +mod

define starto ios_base::sync_with_stdio(0) ,cin.tie(0);;

using namespace std; bool flag=0; //A B C D E F G H I J K L M N O P Q R S T U V W X Y Z //( a + b) % c = ( ( a % c ) + ( b % c ) ) % c // ll k=sizeof(s)/sizeof(s[0]); //->>>tree //vector<vector>tree(n); //bool s[n][n]; // pair<int, string> p = {1, "Hello"}; // cout << "First element: " << p.first << "\n"; //(size ,value) /* for(aout i :Maylist) { cout<<i; } /// make all the arry o fill(x + 1,x + 1 + n, 0);

*/

ll mul(ll a,ll b,ll m) { return ((a%m)*(b%m))%m; }

ll add(ll a,ll b,ll m) { return ((a%m)+(b%m))%m; } int main() { starto; ll T=2;

cin>>T; ll k=998244353; while(T--) { ll n; cin>>n; ll p[n],q[n],super[n]; for(int i=0;i<n;i++) { cin>>p[i]; if(i==0) { super[i]=1; } else { super[i]=super[i-1]*2%k; } }

for(int i=0;i<n;i++) { cin>>q[i]; } ll x1=0,x2=0,ans1=0,ans2=0; for(int i=0;i<n;i++) {

if(p[x1]<p[i]) {

x1=i;

} if(q[x2]<q[i]) {

x2=i;
      }

ans1=super[p[x1]]+super [q[i-x1]]%k; ans2=super[q[x2]]+super [p[i-x2]]%k;

cout<<max(ans1,ans2)<<" "; }

//br; br; }

return 0;

}

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

Does anyone know why this does not work for B?

But it works when I uncomment the two lines. Are both not the same logic?

powersOfTwo = [1]  # Precompute powers of 2 modulo 998244353
for j in range(10**5):
    powersOfTwo.append((powersOfTwo[-1] << 1) % 998244353)  # Left shift to multiply by 2, then take mod
P = powersOfTwo

def solve(idx):
    n = int(input())
    A =  list(map(int,input().split()))
    B =  list(map(int,input().split()))

    ans = []
    MOD = 998244353

    max_a_idx  = 0
    max_b_idx  = 0
    for i, (a, b) in enumerate(zip(A, B)):
        if a >= A[max_a_idx]:
            max_a_idx = i
        if b >= B[max_b_idx]:
            max_b_idx = i

        if A[max_a_idx] > B[max_b_idx]:
            res = (P[A[max_a_idx]]) + P[B[i-max_a_idx]]
        elif B[max_b_idx] > A[max_a_idx]:
            res = P[B[max_b_idx]] + P[A[i-max_b_idx]]
        else:
            min_a, min_b = B[i-max_a_idx], A[i-max_b_idx]

            # THIS DOES NOT WORK
            res1 = P[A[max_a_idx]] + P[min_a]
            res2 = P[A[max_a_idx]] + P[min_b]
            res = max(res1, res2)

            # # THIS WORKS
            # mx = max(min_a, min_b)
            # res = P[A[max_a_idx]] + P[mx]

        res %= MOD
        ans.append(res)

    print(*ans)

for i in range(int(input())):
    solve(i) 
»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hey mate, I gave a good time solving the bonus problem associated with 2115A. I am stuck and need a hint? I am assuming g = gcd(all elements) does not belong to array, and I operate a[i] = a[i]/g for simplicity. Now, say an element requires minimum 'k' operations to reach 1, then our final answer is n+K-1, where K is minimum of all such k's. Also, each k is bounded by log(n), as at each gcd call, we at least reduce a[i] by two, can be easily proven by contradiction. Now, trying to construct using this bound some n*log(n) algorithm, but facing crisis, thanks!