Codeforces Round 1133 (Div. 1, Div. 2) is coming soon! Register here. ×

arpit_kr's blog

By arpit_kr, history, 16 months ago, In English

A. Very Simple Problem

Problem Summary: Check if an integer $$$n$$$ has an odd divisor greater than one.

Key Insight: A number has no odd divisor $$$ \gt 1$$$ if and only if it is a power of two.

Reasoning:

  • Even $$$\times$$$ Even = Even
  • Even $$$\times$$$ Odd = Even
  • Odd $$$\times$$$ Even = Even
  • Odd $$$\times$$$ Odd = Odd

So, only odd numbers can generate odd divisors, and powers of two are formed entirely by 2's.

Implementation Tip: To check if $$$n$$$ is a power of 2:

(n & (n - 1)) == 0

Time Complexity: $$$\mathcal{O}(1)$$$ per test case.


B. Simple Problem

Problem Summary: Given array $$$a$$$, split it into $$$k \gt 1$$$ subsegments and maximize the GCD of the sums of each segment.

Key Insight: It suffices to only try $$$k = 2$$$, because:

$$$ \gcd(b_1, b_2, \dots, b_k) \leq \gcd(b_1 + b_2, b_3, \dots, b_k) $$$

So combining segments doesn't reduce the GCD.

Algorithm:

  1. Compute total sum $$$s = \sum a_i$$$.
  2. For every prefix sum $$$p_i$$$, compute $$$\gcd(p_i, s - p_i)$$$.
  3. Track the maximum.

Time Complexity: $$$\mathcal{O}(n)$$$ per test case.


C. Descent Problem

Problem Summary: Given $$$n$$$, $$$m$$$, and $$$k$$$, find number of valid values of $$$a_{n+1}$$$ such that the array formed by applying $$$a_i = a_{i+1} \bmod i$$$ has exactly $$$k$$$ distinct values.

Case Analysis:

  • $$$k = 1$$$: Always 1 valid way ($$$a_{n+1} = 0$$$).
  • $$$k = 2$$$:
  • If $$$m \leq n$$$: $$$m$$$ ways.

  • Else: $$$n + \left\lfloor \frac{m - n}{n} \right\rfloor$$$.
  • $$$k = 3$$$:
  • If $$$m \leq n$$$: 0 ways.

  • Else: $$$m - n - \left\lfloor \frac{m - n}{n} \right\rfloor$$$.
  • $$$k \gt 3$$$: 0 ways.

Time Complexity: $$$\mathcal{O}(1)$$$ per test case.


D. Hard Problem

Problem Summary: Can all elements of an array be made equal using $$$a_i = a_i + (a_i \bmod 10)$$$?

Key Observations:

  • Numbers divisible by 10 remain fixed.
  • Remainder 5 can only be applied once.
  • Others follow a cyclic behavior mod 20.

Strategy:

  1. If any number is divisible by 5:
  • Apply operation once to all.
  • Check if they are equal.
  1. Else:
  • Normalize all values modulo 20 by applying the operation.
  • Check if all values converge.

Time Complexity: $$$\mathcal{O}(n)$$$ per test case.


E. Very Hard Problem

Problem Summary: Given arrays $$$a$$$ and $$$b$$$, compute:

$$$ \gcd(a_1 + b_j, a_2 + b_j, \dots, a_n + b_j) \text{ for each } j $$$

Use GCD identity:

$$$ \gcd(x, y, z, \dots) = \gcd(x, y - x, z - x, \dots) $$$

Let $$$d = \gcd(a_2 - a_1, \dots, a_n - a_1)$$$. Then,

$$$ \gcd(a_1 + b_j, a_2 + b_j, \dots) = \gcd(a_1 + b_j, d) $$$

Steps:

  1. Compute $$$d$$$ once.
  2. For each $$$b_j$$$, compute $$$\gcd(a_1 + b_j, d)$$$.

Time Complexity: $$$\mathcal{O}(n + m)$$$


F. The Legendary One!!

Problem Summary: Given arrays $$$a$$$ and $$$b$$$, find integer $$$k$$$ such that $$$b_i = a_i \bmod k$$$ (after shuffling).

From:

$$$ \forall i: a_i \equiv b_i \mod k \Rightarrow k \mid (a_i - b_i) $$$

Let $$$\Delta = \sum a_i - \sum b_i$$$

Cases:

  1. If $$$\Delta = 0$$$: Any $$$k \gt \max a_i$$$ works.
  2. Else: Try all divisors of $$$\Delta$$$.

Algorithm:

  • For each divisor $$$d$$$, compute $$$a_i \bmod d$$$.
  • Check if multiset matches $$$b$$$.

Time Complexity: $$$\mathcal{O}(n \sqrt{\Delta})$$$


That concludes the editorial. If you have any doubts or suggestions, feel free to comment below.

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it