E. Divisibility by 25

Codeforces
IDCF988E
Time1000ms
Memory256MB
Difficulty
brute forcegreedy
English · Original
Chinese · Translation
Formal · Original
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, **after each move** the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print _\-1_ if it is impossible to obtain a number that is divisible by $25$. ## Input The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. ## Output If it is impossible to obtain a number that is divisible by $25$, print _\-1_. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. [samples] ## Note In the first example one of the possible sequences of moves is _5071_ $\rightarrow$ _5701_ $\rightarrow$ _7501_ $\rightarrow$ _7510_ $\rightarrow$ _7150_.
给你一个从 $1$ 到 $10^(18)$ 的整数 $n$,且不含前导零。 在一次操作中,你可以交换该数中任意两个相邻的数字,使得得到的数不包含前导零。换句话说,*每次操作后*,你得到的数都不能包含前导零。 求最少需要多少次操作,才能得到一个能被 $25$ 整除的数?如果不可能得到这样的数,请输出 _-1_。 第一行包含一个整数 $n$($1 lt.eq n lt.eq 10^(18)$)。保证数字 $n$ 的首位(左端)不是零。 如果无法得到能被 $25$ 整除的数,输出 _-1_。否则,输出得到这样的数所需的最少操作次数。 注意,你只能交换给定数字中的相邻数字。 在第一个例子中,一种可能的操作序列是 _5071_ $arrow.r$ _5701_ $arrow.r$ _7501_ $arrow.r$ _7510_ $arrow.r$ _7150_。 ## Input 第一行包含一个整数 $n$($1 lt.eq n lt.eq 10^(18)$)。保证数字 $n$ 的首位(左端)不是零。 ## Output 如果无法得到能被 $25$ 整除的数,输出 _-1_。否则,输出得到这样的数所需的最少操作次数。注意,你只能交换给定数字中的相邻数字。 [samples] ## Note 在第一个例子中,一种可能的操作序列是 _5071_ $arrow.r$ _5701_ $arrow.r$ _7501_ $arrow.r$ _7510_ $arrow.r$ _7150_。
Let $ n $ be a positive integer with decimal representation $ d_k d_{k-1} \dots d_1 d_0 $, where $ k+1 = \text{len}(n) $, $ d_k \ne 0 $, and $ d_i \in \{0,1,\dots,9\} $. A number is divisible by 25 if and only if its last two digits form a number in the set $ \{00, 25, 50, 75\} $. We are allowed to perform adjacent swaps, with the constraint that **after every swap**, the resulting number must not have leading zeros (i.e., the first digit must be non-zero). Let $ L = \text{len}(n) $. Define the set of valid target suffixes: $$ T = \{00, 25, 50, 75\} $$ For each target suffix $ s = ab \in T $, we consider all possible ways to assign two digits in $ n $ to be moved to the last two positions (i.e., positions $ L-2 $ and $ L-1 $) such that: - The two digits used are distinct occurrences of $ a $ and $ b $ in $ n $ (possibly the same digit if $ a = b = 0 $), - The remaining digits form a number of length $ L-2 $ with no leading zero (i.e., the leftmost remaining digit must be non-zero). For each such valid assignment: - Compute the minimum number of adjacent swaps required to bring the two chosen digits $ a $ and $ b $ to the last two positions, preserving their relative order $ a $ then $ b $ (i.e., $ a $ must end up at position $ L-2 $, $ b $ at $ L-1 $). - This is computed as the sum of the distances each digit must travel to reach its target position, minus any overlap (i.e., if the left digit moves right past the right digit, their paths cross and the total swaps are reduced by 1). Additionally, after moving the two digits to the end, the prefix (first $ L-2 $ digits) must not start with zero. So we must ensure that **at least one non-zero digit remains in the prefix**, and the leftmost digit of the prefix is non-zero. Let $ S $ be the multiset of digits of $ n $. For each $ s = ab \in T $: 1. Enumerate all pairs of indices $ (i, j) $, $ i < j $, such that $ d_i = a $, $ d_j = b $. 2. For each such pair: - Temporarily remove $ d_i $ and $ d_j $ from the digit sequence. - Check if the remaining $ L-2 $ digits have a non-zero first digit. If not, skip. - Compute the number of adjacent swaps needed to move $ d_i $ to position $ L-2 $ and $ d_j $ to position $ L-1 $, accounting for their relative positions and the fact that moving one affects the other. - The cost is: $$ \text{cost} = (L - 1 - j) + (L - 2 - i) - \delta $$ where $ \delta = 1 $ if $ i < j $ and the movement of $ d_i $ to the left of $ d_j $ causes them to cross (i.e., if $ i $ is to the left of $ j $, and we move $ i $ right past $ j $, then the relative movement is reduced by 1 because $ j $ shifts left by one when $ i $ passes it). - More precisely: Let $ \text{pos}_a = i $, $ \text{pos}_b = j $. After removing both, the number of swaps to bring $ d_i $ to position $ L-2 $ is $ (L - 2 - i) $, but if $ j > i $, then when $ d_i $ moves past $ d_j $, $ d_j $ shifts left by one, so the cost to bring $ d_j $ to $ L-1 $ becomes $ (L - 1 - j - 1) = L - 2 - j $. So total cost: $$ (L - 2 - i) + (L - 1 - j) - \mathbf{1}_{i < j} $$ (since if $ i < j $, moving $ i $ to the right past $ j $ causes $ j $ to move one position left, saving one swap). 3. Among all valid $ s \in T $ and all valid pairs $ (i,j) $, take the minimum cost. If no valid assignment exists (i.e., for all $ s \in T $, no pair of digits can be moved to the end without leaving a leading zero in the prefix), output $ -1 $. --- **Formal Statement:** Let $ n $ be a positive integer with decimal digits $ d_0, d_1, \dots, d_{L-1} $, where $ d_0 \ne 0 $, $ L = \lfloor \log_{10} n \rfloor + 1 $. Define $ T = \{00, 25, 50, 75\} $. For each $ s = ab \in T $, and for each pair of indices $ i < j $ such that $ d_i = a $, $ d_j = b $: - Let $ D' $ be the sequence $ d $ with digits at positions $ i $ and $ j $ removed. - Let $ d'_0 $ be the first digit of $ D' $. If $ d'_0 = 0 $, skip this pair. - Compute cost: $$ c = (L - 2 - i) + (L - 1 - j) - \mathbf{1}_{i < j} $$ (Note: Since $ i < j $ always in our enumeration, this simplifies to $ 2L - 3 - i - j - 1 = 2L - 4 - i - j $.) Let $ C = \min \{ c \mid \text{valid } s \in T, \text{valid pair } (i,j) \} $. If $ C $ is undefined (no valid pair exists), output $ -1 $. Otherwise, output $ C $. --- **Note:** The formula for cost assumes that moving the left digit $ a $ to position $ L-2 $ requires $ (L - 2 - i) $ swaps, and the right digit $ b $ to position $ L-1 $ requires $ (L - 1 - j) $ swaps, but since $ i < j $, when $ a $ moves past $ b $, $ b $ shifts left by one, so we subtract 1. Thus, the cost is: $$ \boxed{2L - 4 - i - j} $$ for each valid pair $ (i,j) $ with $ d_i = a $, $ d_j = b $, $ a,b \in T $, and the remaining digits form a prefix with non-zero leading digit.
Samples
Input #1
5071
Output #1
4
Input #2
705
Output #2
1
Input #3
1241367
Output #3
\-1
API Response (JSON)
{
  "problem": {
    "name": "E. Divisibility by 25",
    "description": {
      "content": "You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contai",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF988E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes.\n\nIn one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contai...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "给你一个从 $1$ 到 $10^(18)$ 的整数 $n$,且不含前导零。\n\n在一次操作中,你可以交换该数中任意两个相邻的数字,使得得到的数不包含前导零。换句话说,*每次操作后*,你得到的数都不能包含前导零。\n\n求最少需要多少次操作,才能得到一个能被 $25$ 整除的数?如果不可能得到这样的数,请输出 _-1_。\n\n第一行包含一个整数 $n$($1 lt.eq n lt.eq 10^(18)$)。...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n $ be a positive integer with decimal representation $ d_k d_{k-1} \\dots d_1 d_0 $, where $ k+1 = \\text{len}(n) $, $ d_k \\ne 0 $, and $ d_i \\in \\{0,1,\\dots,9\\} $.\n\nA number is divisible by 25 i...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments