1337xyz1337xyz/codeforces-editorial-full-2026-07-16 huggingface dataset:
Beautiful Fibonacci Problem
Contest: Codeforces Round 604 (Div. 1)
Difficulty rating: 3500
Tags: constructive algorithms, number theory

The well-known Fibonacci sequence $$$F_0, F_1, F_2,\ldots $$$ is defined as follows:

- $$$F_0 = 0, F_1 = 1$$$.
- For each $$$i \geq 2$$$: $$$F_i = F_{i - 1} + F_{i - 2}$$$.

Given an increasing arithmetic sequence of positive integers with $$$n$$$ elements: $$$(a, a + d, a + 2\cdot d,\ldots, a + (n - 1)\cdot d)$$$.

You need to find another increasing arithmetic sequence of positive integers with $$$n$$$ elements $$$(b, b + e, b + 2\cdot e,\ldots, b + (n - 1)\cdot e)$$$ such that:

- $$$0 < b, e < 2^{64}$$$,
- for all $$$0\leq i < n$$$, the decimal representation of $$$a + i \cdot d$$$ appears as substring in the last $$$18$$$ digits of the decimal representation of $$$F_{b + i \cdot e}$$$ (if this number has less than $$$18$$$ digits, then we consider all its digits).

Input format:
The first line contains three positive integers $$$n$$$, $$$a$$$, $$$d$$$ ($$$1 \leq n, a, d, a + (n - 1) \cdot d < 10^6$$$).

Output format:
If no such arithmetic sequence exists, print $$$-1$$$.

Otherwise, print two integers $$$b$$$ and $$$e$$$, separated by space in a single line ($$$0 < b, e < 2^{64}$$$).

If there are many answers, you can output any of them.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, we can choose $$$(b, e) = (2, 1)$$$, because $$$F_2 = 1, F_3 = 2, F_4 = 3$$$.

In the second test case, we can choose $$$(b, e) = (19, 5)$$$ because:

- $$$F_{19} = 4181$$$ contains $$$1$$$;
- $$$F_{24} = 46368$$$ contains $$$3$$$;
- $$$F_{29} = 514229$$$ contains $$$5$$$;
- $$$F_{34} = 5702887$$$ contains $$$7$$$;
- $$$F_{39} = 63245986$$$ contains $$$9$$$.

Example input:
3 1 1

Example output:
2 1

Example input:
5 1 2

Example output:
19 5

Editorial:
Intuitively, we want to a formula for $$$F_{m + n}$$$. This is one we need: $$$F_{m + n} = F_m\cdot F_{n + 1} + F_{m - 1}\cdot F_n$$$.
Denote $$$n = 12\cdot 10^k$$$, one can verify that $$$F_{i\cdot n} \equiv 0$$$ modulo $$$10^k$$$ (directly calculate or use 'Pisano Period').
So we have following properties:
• $$$F_{2\cdot n + 1} = F_{n + 1}^2 + F_n^2$$$ $$$\Rightarrow F_{2\cdot n + 1} = F_{n + 1}^2$$$ modulo $$$10^{2\cdot k}$$$.
• $$$F_{3\cdot n + 1} = F_{2\cdot n + 1}\cdot F_{n + 1} + F_{2\cdot n}\cdot F_n$$$ $$$\Rightarrow F_{3\cdot n + 1} = F_{n + 1}^3$$$ modulo $$$10^{2\cdot k}$$$.
• So on, $$$F_{u\cdot n + 1} = F_{n + 1}^u$$$ modulo $$$10^{2\cdot k}$$$.
Notice that $$$F_{n + 1} = 8\cdot 10^k\cdot t + 1$$$, where $$$gcd(t, 10) = 1$$$.
So $$$F_{u\cdot n + 1} = (8\cdot 10^k\cdot t + 1)^u = 8\cdot u\cdot t\cdot 10^k + 1$$$ modulo $$$10^{2\cdot k}$$$.
Let $$$u = 125.a.t^{-1}$$$ modulo $$$10^k$$$, $$$v = 125.d.t^{-1}$$$ modulo $$$10^k$$$. Then we choose $$$b = u\cdot n + 1, e = v\cdot n$$$.
Soldier and Bananas
Contest: Codeforces Round 304 (Div. 2)
Difficulty rating: 800
Tags: brute force, implementation, math

A soldier wants to buy w bananas in the shop. He has to pay k dollars for the first banana, 2k dollars for the second one and so on (in other words, he has to pay i·k dollars for the i-th banana).

He has n dollars. How many dollars does he have to borrow from his friend soldier to buy w bananas?

Input format:
The first line contains three positive integers k, n, w (1  ≤  k, w  ≤  1000, 0 ≤ n ≤ 109), the cost of the first banana, initial number of dollars the soldier has and number of bananas he wants.

Output format:
Output one integer — the amount of dollars that the soldier must borrow from his friend. If he doesn't have to borrow money, output 0.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Example input:
3 17 4

Example output:
13

Editorial:
We can easily calculate the sum of money that we need to buy all the bananas that we want, let's name it x.
If n >  = x the answer is 0, because we don't need to borrow anything.
Otherwise the answer is x - n.
Soldier and Badges
Contest: Codeforces Round 304 (Div. 2)
Difficulty rating: 1200
Tags: brute force, greedy, implementation, sortings

Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin.

For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors.

Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.

Input format:
First line of input consists of one integer n (1 ≤ n ≤ 3000).

Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge.

Output format:
Output single integer — minimum amount of coins the colonel has to pay.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In first sample test we can increase factor of first badge by 1.

In second sample test we can increase factors of the second and the third badge by 1.

Example input:
4
1 3 1 4

Example output:
1

Example input:
5
1 2 3 2 5

Example output:
2

Editorial:
Let's count the number of badges with coolness factor 1, 2 and so on. Then, let's look at the number of badges with value equal to 1. If it's greater than 1, we have to increase a value of every of them except for one. Then, we look at number of badges with value 2, 3 and so on up to 2n - 2 (because maximum value of badge which we can achieve is 2n - 1). It is easy to see that this is the correct solution. We can implement it in O(n), but solutions that work in complexity O(n2) also passed.
Soldier and Cards
Contest: Codeforces Round 304 (Div. 2)
Difficulty rating: 1400
Tags: brute force, dfs and similar, games

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game.

The rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins.

You have to calculate how many fights will happen and who will win the game, or state that game won't end.

Input format:
First line contains a single integer n (2 ≤ n ≤ 10), the number of cards.

Second line contains integer k1 (1 ≤ k1 ≤ n - 1), the number of the first soldier's cards. Then follow k1 integers that are the values on the first soldier's cards, from top to bottom of his stack.

Third line contains integer k2 (k1 + k2 = n), the number of the second soldier's cards. Then follow k2 integers that are the values on the second soldier's cards, from top to bottom of his stack.

All card values are different.

Output format:
If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won.

If the game won't end and will continue forever output  - 1.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
First sample:

Second sample:

Example input:
4
2 1 3
2 4 2

Example output:
6 2

Example input:
3
1 2
2 1 3

Example output:
-1

Editorial:
It's easy to count who wins and after how many "figts", but it's harder to say, that game won't end. How to do it?
Firstly let's count a number of different states that we can have in the game. Cards can be arranged in any one of n! ways. In every of this combination, we must separate first soldier's cards from the second one's. We can separate it in n + 1 places (because we can count the before and after deck case too).
So war has (n + 1)! states. If we'd do (n + 1)! "fights" and we have not finished the game yes, then we'll be sure that there is a state, that we passed at least twice. That means that we have a cycle, and game won't end.
After checking this game more accurately I can say that the longest path in the state-graph for n = 10 has length 106, so it is enough to do 106 fights, but solutions that did about 40 millions also passed.
Alternative solution is to map states that we already passed. If we know, that we longest time needed to return to state is about 100, then we know that this solution is correct and fast.
Soldier and Number Game
Contest: Codeforces Round 304 (Div. 2)
Difficulty rating: 1700
Tags: constructive algorithms, dp, math, number theory

Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer x > 1, such that n is divisible by x and replacing n with n / x. When n becomes equal to 1 and there is no more possible valid moves the game is over and the score of the second soldier is equal to the number of rounds he performed.

To make the game more interesting, first soldier chooses n of form a! / b! for some positive integer a and b (a ≥ b). Here by k! we denote the factorial of k that is defined as a product of all positive integers not large than k.

What is the maximum possible score of the second soldier?

Input format:
First line of input consists of single integer t (1 ≤ t ≤ 1 000 000) denoting number of games soldiers play.

Then follow t lines, each contains pair of integers a and b (1 ≤ b ≤ a ≤ 5 000 000) defining the value of n for a game.

Output format:
For each game output a maximum score that the second soldier can get.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Example input:
2
3 1
6 3

Example output:
2
5

Editorial:
Firstly we have to note, that second soldier should choose only prime numbers. If he choose a composite number x that is equal to p * q, he can choose first p, then q and get better score. So our task is to find a number of prime factors in factorization of n.
Now we have to note that factorization of number a! / b! is this same as factorization of numbers (b + 1)*(b + 2)*...*(a - 1)*a.
Let's count number of prime factor in factorization of every number from 2 to 5000000.
First, we use Sieve of Eratosthenes to find a prime diviser of each of these numbers. Then we can calculate a number of prime factors in factorization of a using the formula:
primefactors[a] = primefactors[a / primediviser[a]] + 1
When we know all these numbers, we can use a prefix sums, and then answer for sum on interval.
Soldier and Traveling
Contest: Codeforces Round 304 (Div. 2)
Difficulty rating: 2100
Tags: flows, graphs, math

In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th city consists of ai soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road.

Check if is it possible that after roaming there will be exactly bi soldiers in the i-th city.

Input format:
First line of input consists of two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 200).

Next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 100).

Next line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 100).

Then m lines follow, each of them consists of two integers p and q (1 ≤ p, q ≤ n, p ≠ q) denoting that there is an undirected road between cities p and q.

It is guaranteed that there is at most one road between each pair of cities.

Output format:
If the conditions can not be met output single word "NO".

Otherwise output word "YES" and then n lines, each of them consisting of n integers. Number in the i-th line in the j-th column should denote how many soldiers should road from city i to city j (if i ≠ j) or how many soldiers should stay in city i (if i = j).

If there are several possible answers you may output any of them.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Example input:
4 4
1 2 6 3
3 5 3 1
1 2
2 3
3 4
4 2

Example output:
YES
1 0 0 0
2 0 0 0
0 5 1 0
0 0 2 1

Example input:
2 0
1 2
2 1

Example output:
NO

Editorial:
There are few ways to solve this task, but I'll describe the simplest (in my opinion) one.
Let's build a flow network in following way:
Make a source.
Make a first group of vertices consisting of n vertices, each of them for one city.
Connect a source with ith vertex in first group with edge that has capacity ai.
Make a sink and second group of vertices in the same way, but use bi except for ai.
If there is a road between cities i and j or i = j. Make two edges, first should be connecting ith vertex from first group, and jth vertex from second group, and has infinity capacity. Second should be similar, but connect jth from first group and ith from second group.
Then find a maxflow, in any complexity.
If maxflow is equal to sum of ai and is equal to sum of bi, then there exists an answer. How can we get it? We just have to check how many units are we pushing through edge connecting two vertices from different groups.
I told about many solutions, because every solution, which doesn't use greedy strategy, can undo it's previous pushes, and does it in reasonable complexity should pass.
Non-zero
Contest: Codeforces Round 618 (Div. 2)
Difficulty rating: 800
Tags: implementation, math

Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.

If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.

What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\dots$$$ $$$+ a_n \ne 0$$$ and $$$a_1 \cdot a_2 \cdot$$$ $$$\dots$$$ $$$\cdot a_n \ne 0$$$.

Input format:
Each test contains multiple test cases.

The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows.

The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-100 \le a_i \le 100$$$) — elements of the array .

Output format:
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.

In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.

In the third test case, both sum and product are non-zero, we don't need to do anything.

In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$.

Example input:
4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1

Example output:
1
2
0
2

Editorial:
While there are any zeros in the array, the product will be zero, so we should add $$$1$$$ to each zero. Now, if the sum is zero, we should add $$$1$$$ to any positive number, so the sum becomes nonzero. So the answer is the number of zeroes in the array plus $$$1$$$ if the sum of numbers is equal to zero after adding $$$1$$$ to zeros.
Nearest Interesting Number
Contest: Codeforces Round 570 (Div. 3)
Difficulty rating: 800
Tags: implementation

Polycarp knows that if the sum of the digits of a number is divisible by $$$3$$$, then the number itself is divisible by $$$3$$$. He assumes that the numbers, the sum of the digits of which is divisible by $$$4$$$, are also somewhat interesting. Thus, he considers a positive integer $$$n$$$ interesting if its sum of digits is divisible by $$$4$$$.

Help Polycarp find the nearest larger or equal interesting number for the given number $$$a$$$. That is, find the interesting number $$$n$$$ such that $$$n \ge a$$$ and $$$n$$$ is minimal.

Input format:
The only line in the input contains an integer $$$a$$$ ($$$1 \le a \le 1000$$$).

Output format:
Print the nearest greater or equal interesting number for the given number $$$a$$$. In other words, print the interesting number $$$n$$$ such that $$$n \ge a$$$ and $$$n$$$ is minimal.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Example input:
432

Example output:
435

Example input:
99

Example output:
103

Editorial:
Even if we will iterate over all possible numbers starting from $$$a$$$ and check if sum of digits of the current number is divisible by $$$4$$$, we will find the answer very fast. The maximum possible number of iterations is no more than $$$5$$$.
Candy Box (easy version)
Contest: Codeforces Round 570 (Div. 3)
Difficulty rating: 1400
Tags: greedy, sortings

This problem is actually a subproblem of problem G from the same contest.

There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 \le a_i \le n$$$).

You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad).

It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.

Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.

You have to answer $$$q$$$ independent queries.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.

Input format:
The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. Each query is represented by two lines.

The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of candies.

The second line of each query contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy in the box.

It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each query print one integer — the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first query, you can prepare a gift with two candies of type $$$8$$$ and one candy of type $$$5$$$, totalling to $$$3$$$ candies.

Note that this is not the only possible solution — taking two candies of type $$$4$$$ and one candy of type $$$6$$$ is also valid.

Example input:
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7

Example output:
3
10
9

Editorial:
Let's calculate the array $$$cnt$$$ where $$$cnt_i$$$ is the number of candies of the $$$i$$$-th type. Let's sort it in non-ascending order.
Obviously, now we can take $$$cnt_1$$$ because this is the maximum number of candies of some type in the array. Let $$$lst$$$ be the last number of candies we take. Initially it equals $$$cnt_1$$$ (and the answer $$$ans$$$ is initially the same number). Then let's iterate over all values of $$$cnt$$$ in order from left to right. If the current number $$$cnt_i$$$ is greater than or equal to the last taken number of candies $$$lst$$$ then we cannot take more candies than $$$lst - 1$$$ (because we iterating over values of $$$cnt$$$ in non-ascending order), so let's increase answer by $$$lst - 1$$$ and set $$$lst := lst - 1$$$. Otherwise $$$cnt_i < lst$$$ and we can take all candies of this type, increase the answer by $$$cnt_i$$$ and set $$$lst := cnt_i$$$.
Subsequences (easy version)
Contest: Codeforces Round 570 (Div. 3)
Difficulty rating: 2000
Tags: dp, graphs, implementation, shortest paths

The only difference between the easy and the hard versions is constraints.

A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".

You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters.

In one move you can take any subsequence $$$t$$$ of the given string and add it to the set $$$S$$$. The set $$$S$$$ can't contain duplicates. This move costs $$$n - |t|$$$, where $$$|t|$$$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).

Your task is to find out the minimum possible total cost to obtain a set $$$S$$$ of size $$$k$$$ or report that it is impossible to do so.

Input format:
The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 100$$$) — the length of the string and the size of the set, correspondingly.

The second line of the input contains a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters.

Output format:
Print one integer — if it is impossible to obtain the set $$$S$$$ of size $$$k$$$, print -1. Otherwise, print the minimum possible total cost to do it.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example we can generate $$$S$$$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $$$S$$$ is $$$0$$$ and the cost of the others is $$$1$$$. So the total cost of $$$S$$$ is $$$4$$$.

Example input:
4 5
asdf

Example output:
4

Example input:
5 6
aaaaa

Example output:
15

Editorial:
The topic of this problem is BFS. Let strings be the vertices of the graph and there is a directed edge from string $$$s$$$ to string $$$t$$$ if and only if we can obtain $$$t$$$ from $$$s$$$ by removing exactly one character.
In this interpretation we have to find first $$$k$$$ visited vertices if we start our BFS from the initial string. And then the answer will be just $$$nk$$$ minus the sum of length of visited strings. The last thing to mention: instead of standard queue of integers we need to maintain the queue of strings and instead of array of visited vertices we have to maintain the set of visited strings. Don't forget to stop BFS when you obtain exactly $$$k$$$ strings.
If the number of distinct subsequences is less than $$$k$$$ then the answer is -1.