Skip to main content

900

Day 1​

Array Reodering​

Case 1: a[i] is EVEN​

If a[i] is even:

  • It contains at least one factor of 2.
  • 2a[j] is always even (because of the extra 2).

So:

gcd(even, even) ≥ 2

ALWAYS greater than 1.

That means:

If the first element in the pair is EVEN, the pair is ALWAYS good.

Case 2: a[i] is ODD​

Now suppose a[i] is odd.

gcd(odd, 2a[j])

Since a[i] has no factor 2, the extra 2 doesn’t help.

So this becomes:

gcd(a[i], a[j])

Now the pair is good only if they share some common factor > 1.

So:

Odd + Odd → sometimes works Odd + Even → might work But NOT guaranteed

Big Insight​

Since:

  • EVEN as first element → ALWAYS good pair
  • ODD as first element → only SOMETIMES good

To maximize pairs:

Put ALL EVEN numbers FIRST Put ALL ODD numbers AFTER

This guarantees:

  • Every even forms good pair with ALL elements after it.

Rorder all items and cout which fulfill the condition.

Bad Boy​

To annoy Anton, Riley decided to throw exactly two yo-yos in cells of the room (they can be in the same cell).

Means:

  • Both yo-yos at Anton’s cell
  • One at Anton’s cell, one somewhere else
  • Two different cells
  • Two same cells somewhere else

Exciting Bets​

Key observation: every move changes both numbers by the same amount.

If you do:

  • +1 move → (a+1, b+1)
  • -1 move → (a-1, b-1)

After k moves, the pair becomes:

(a+k,;b+k)or(a−k,;b−k)(a+k,; b+k) \quad \text{or} \quad (a-k,; b-k)

So effectively we can reach any pair:

(a+x,;b+x)(a+x,; b+x)

for some integer (x) (as long as values stay ≥0).

Step 1 — Key invariant​

The difference stays constant:

(b+x)−(a+x)=b−a(b+x) - (a+x) = b-a

Let

d=∣a−b∣d = |a-b|

This never changes.

Step 2 — Express the GCD​

Let the new numbers be:

A=a+x,B=b+xA = a+x,\quad B = b+x

Then

B=A+dB = A + d

So:

gcd(A,B)=gcd(A,A+d)gcd(A,B) = gcd(A, A+d)

Using the property:

gcd(A,A+d)=gcd(A,d)gcd(A, A+d) = gcd(A, d)

Thus the excitement becomes:

gcd(A,d)gcd(A, d)

Step 3 — Maximum possible GCD​

Since (gcd(A,d) \le d), the maximum possible value is (d).

We can achieve it if:

A≡0(modd)A \equiv 0 \pmod d

So we want:

a+x≡0(modd)a + x \equiv 0 \pmod d

Step 4 — Minimum moves​

Solve:

x≡−a(modd)x \equiv -a \pmod d

The smallest non-negative solution is:

x=(d−a mod d) mod dx = (d - a \bmod d) \bmod d

But we can also move backwards (decrease), so we take the minimum distance to the nearest multiple:

moves=min⁡(a mod d,;d−a mod d)\text{moves} = \min(a \bmod d,; d - a \bmod d)

Step 5 — Special case​

If

a=ba = b

then

d=0d = 0

Now:

gcd(a+x,a+x)=a+xgcd(a+x, a+x) = a+x

By increasing forever we can make the gcd arbitrarily large.

So the answer is:

0 0

(infinite excitement)

Odd Divisor​

Intitution was right, approach gone wrong

even * even = even
odd * even = even
odd * odd = add
  • if n is odd, it definitly have odd divisor

  • if n is even, we need to extract the odd part

    • by dividing we will get another even number
    odd * even = even
    odd * (even *2) = even
    • to get the exact odd value, we have to keep divide it by 2
    • actually the number will be in following format
    odd * 2^k = even
    • so n shouldn't contain 2^k

Mocha and Math​

Important properties of bitwise AND (&):

  • x & y ≤ x and x & y ≤ y
  • AND operation can only turn bits off, never on.
  • Repeating AND operations can only make numbers smaller or equal.

So the operation always reduces or keeps values the same.

What Is Our Goal?

We want to minimize the maximum element in the array after any number of operations.

So we’re trying to make every element as small as possible.

Suppose we select the entire array ([1, n]).

Then:

a1=a1 & ana_1 = a_1 \,\&\, a_n a2=a2 & an−1a_2 = a_2 \,\&\, a_{n-1}

and so on.

If we repeat operations cleverly on different intervals, something important happens:

Every element can eventually become the AND of all elements.

Why?

Because:

  • You can keep shrinking values using AND with other elements.
  • AND is associative and commutative.
  • Repeated mixing spreads bit removals across the array.

So eventually, every element can be reduced to:

a1 & a2 & a3 & … & ana_1 \,\&\, a_2 \,\&\, a_3 \,\&\, \dots \,\&\, a_n

Let’s call this value:

X=a1 & a2 & … & anX = a_1 \,\&\, a_2 \,\&\, \dots \,\&\, a_n
  • AND only removes bits.
  • So no element can ever go below this global AND.
  • But we can make all elements equal to this value.

Therefore:

The minimum possible maximum value = Bitwise AND of all elements