Educational Codeforces Round 86 (Rated for Div. 2)
Enter
A. Road To Zero
You are given two integers and . You can perform two types of operations:
Pay a dollars and increase or decrease any of these integers by . For example, if and there are four possible outcomes after this operation:;;;
Pay b dollars and increase or decrease both integers by . For example, if and there are two possible outcomes after this operation: ;
Your goal is to make both given integers equal zero simultaneously, i.e. . There are no other requirements. In particular, it is possible to move from , to .
Calculate the minimum amount of dollars you have to spend on it.
Input
The first line contains one integer — the number of testcases.
The first line of each test case contains two integers and .
The second line of each test case contains two integers and .
Output
For each test case print one integer — the minimum amount of dollars you have to spend.
Example
input
2
1 3
391 555
0 0
9 4
output
1337
0
Note
In the first test case you can perform the following sequence of operations: first, second, first. This way you spend dollars.
In the second test case both integers are equal to zero initially, so you don’t have to spend money.
Translation
给两个数 。花费 的代价可以进行操作 :选择 其中的一个数将其增加或者减少 。花费 的代价可以进行操作 $2 $:将 都增加或者减少 。求将 都置为 的最底花费。
Idea
由于 ,所以先进行几次操作 ,直到 为 ,接着再执行几次操作 ,直到 为 ,那么就能用最少的操作次数达到目的。但是最高效率并不意味着最低花费,我们可以将操作 视为执行了两次操作 ,那么如果两次操作 的花费低于操作 ,那么执行两次操作 才能达到最低花费的目的。也就是说,如果两次操作 的花费低于操作 ,那么就用两次操作 的代价代替一次操作 的代价。
Code
1 |
|
B. Binary Period
Let’s say string has period if for all from to ( means length of string ) and is the minimum positive integer with this property.
Some examples of a period: for the period is , for the period is , for the period is , for the period is .
You are given string consisting only of ’s and ’s and you need to find such string that:
String consists only of ’s and ’s;
The length of doesn’t exceed ;
String is a subsequence of string ;
String has smallest possible period among all strings that meet conditions .
Let us recall that is a subsequence of if can be derived from by deleting zero or more elements (any) without changing the order of the remaining elements. For example, is a subsequence of .
Input
The first line contains single integer $ (1≤T≤100)$ — the number of test cases.
Next T lines contain test cases — one per line. Each line contains string $ (1≤|t|≤100)$ consisting only of ’s and ’s.
Output
Print one string for each test case — string you needed to find. If there are multiple solutions print any one of them.
Example
input
4
00
01
111
110
output
00
01
11111
1010
Note
In the first and second test cases, since it’s already one of the optimal solutions. Answers have periods equal to and , respectively.
In the third test case, there are shorter optimal solutions, but it’s okay since we don’t need to minimize the string . String has period equal to .
Translation
给一个 串 ,输出一个 串 。要求 , 是 的子序列, 且要求 最小。
Idea
显然,如果 $t=1111111\cdots $ 或 ,那么 $t $ 本身就符合条件 ,且此时的 ,是最小的。若 中出现了 和 ,我们可以在 中添加一些字符得到 ,这样做的好处是 自然会成为 的子序列;我们让 成为 和 交替的串,即 或 ,此时的 必然满足条件,且此时的 ,是最小的。
Code
1 |
|
C. Yet Another Counting Problem
You are given two integers and , and queries. The -th query consists of two numbers and , and the answer to it is the number of integers such that , and . Calculate the answer for each query.
Recall that is the remainder of the division of by . For example, , $, $$9$$\bmod 4$$=$$1$, .
Input
The first line contains one integer — the number of test cases. Then the test cases follow.
The first line of each test case contains three integers , and $ 1$$≤$$q$$≤$$500$$)$.
Then lines follow, each containing two integers and for the corresponding query.
Output
For each test case, print integers — the answers to the queries of this test case in the order they appear.
Example
input
2
4 6 5
1 1
1 3
1 5
1 7
1 9
7 10 2
7 8
100 200
output
0 0 0 2 4
0 91
Translation
给定两个数 ,在 次询问中,每次给一个区间 ,求集合 $\{$$x\in W|($$x$$\bmod a$$)$$\bmod b$$≠$$($$x$$\bmod b$$)$$\bmod a$$\}$ 中的元素个数。
Idea
1 |
|
从第三行起,输出的左边是 ,右边代表 的真假。
如图, 的分布是按照 长度循环的。也就是说,我们只需要知道 内的分布情况,就能得到任意区间的分布情况。因此查询是 的。
考虑一个前缀和函数 代表 内符合条件 的 的个数。对于询问 ,只需要输出 。直接用数组存这个前缀和显然是不现实的,我们要利用前面找到的规律。由于分布基于 循环,那么就可以求出 内的前缀和 ;当查询 的前缀和时,由于每段长度为 的区间分布相同, 内首先就有贡献 ,剩余的部分中,有 ,因此 内的总贡献为
代码
1 |
|
D. Multiple Testcases
So you decided to hold a contest on Codeforces. You prepared the problems: statements, solutions, checkers, validators, tests… Suddenly, your coordinator asks you to change all your tests to multiple testcases in the easiest problem!
Initially, each test in that problem is just an array. The maximum size of an array is . For simplicity, the contents of arrays don’t matter. You have tests — the i-th test is an array of size .
Your coordinator asks you to distribute all of your arrays into multiple testcases. Each testcase can include multiple arrays. However, each testcase should include no more than arrays of size greater than or equal to , no more than arrays of size greater than or equal to , …, no more than arrays of size greater than or equal to . Also, .
So now your goal is to create the new testcases in such a way that:
- each of the initial arrays appears in exactly one testcase;
- for each testcase the given conditions hold;
- the number of testcases is minimum possible.
Print the minimum possible number of testcases you can achieve and the sizes of arrays included in each testcase.
Input
The first line contains two integers and — the number of initial tests and the limit for the size of each array.
The second line contains integers — the sizes of the arrays in the original tests.
The third line contains integers ; is the maximum number of arrays of size greater than or equal to you can have in a single testcase.
Output
In the first line print a single integer — the minimum number of testcases you can achieve.
Each of the next ans lines should contain the description of a testcase in the following format:
— the testcase includes arrays, is the size of the -th array in that testcase.
Each of the initial arrays should appear in exactly one testcase. In particular, it implies that the sum of t over all ans testcases should be equal to .
Note that the answer always exists due to (and therefore ).
If there are multiple answers, you can output any one of them.
Examples
input
4 3
1 2 2 3
4 1 1
output
3
1 2
2 1 3
1 2
input
6 10
5 8 1 10 8 7
6 6 4 4 3 2 2 2 1 1
output
2
3 8 5 7
3 10 8 1
input
5 1
1 1 1 1 1
5
output
1
5 1 1 1 1 1
input
5 1
1 1 1 1 1
1
output
5
1 1
1 1
1 1
1 1
1 1
Note
In the first example there is no way to distribute the tests into less than testcases. The given answer satisfies the conditions: each of the testcases includes no more than arrays of size greater than or equal to and no more than array of sizes greater than or equal to and .
Note that there are multiple valid answers for this test. For example, testcases with sizes would also be correct.
However, testcases with sizes would be incorrect because there are arrays of size greater than or equal to in the second testcase.
Note the difference between the third and the fourth examples. You can include up to arrays of size greater than or equal to in the third example, so you can put all arrays into a single testcase. And you can have only up to array in the fourth example. Thus, every array should be included in a separate testcase.
Translation
数列 $\{m_n\}$ 中的最大元素为 。将数列 $\{m_n\}$ 分组,要求每组中大于等于 的数字个数不超过 。
要求分的组的数量尽可能小,并输出分组情况。
Idea
设数列 $\{m_n\}$ 中大于等于 的数字个数为 。
于是,大于等于 的数字有 个,每组不能超过 个,需要分成 组。所以,当我们考察整个数列中的所有数字,至少需要分成 $ans=\max\limits_{1\le i\le k}\left\{\left\lceil \frac{cnt_i}{c_i}\right\rceil\right\}$ 组。
因此我们只需要将 从大到小依次循环填入第 组即可,这样的分布是最优的,一定可以满足条件。
Code
1 |
|
E. Placing Rooks
Calculate the number of ways to place n rooks on n×n chessboard so that both following conditions are met:
- each empty cell is under attack;
- exactly pairs of rooks attack each other.
An empty cell is under attack if there is at least one rook in the same row or at least one rook in the same column. Two rooks attack each other if they share the same row or column, and there are no other rooks between them. For example, there are only two pairs of rooks that attack each other in the following picture:
One of the ways to place the rooks for and
Two ways to place the rooks are considered different if there exists at least one cell which is empty in one of the ways but contains a rook in another way.
The answer might be large, so print it modulo .
Input
The only line of the input contains two integers and .
Output
Print one integer — the number of ways to place the rooks, taken modulo 998244353.
Examples
input
3 2
output
6
input
3 3
output
0
input
4 0
output
24
input
1337 42
output
807905441
Translation
在一个 的国际象棋棋盘上放置 个车,满足以下两个条件:①棋盘上的每一个空格子都能被攻击到,②恰好存在 对车可以相互攻击。
Idea
要在 的棋盘上放 个车,使得所有空格都能被攻击到,那么每行/列都需要有一个车。我们先假设每行都有一个车,由棋盘的对称性,列的情况与之相同。以下的讨论只都基于每行都有一个车的情况。
设想现在每行都有一个车,如果考察行,每一辆车是互不干扰的,要使有 个车能互相攻击,需要在列上做文章。初始情况是所有车都在棋盘的对角线上,这时没有车能互相攻击。我们选出 列置为空,将 个车放入其他 列中;设想如果一列上有 个车,那么这列上能互相攻击的车的数目为 ;对于这 列,能互相攻击的车的数目为 。所以该问题等价于将 个车放入 列中。
立刻联想到了第二类斯特林数()。把 个不同的球放到 个相同的盒子里,假设没有空盒,则放球方案数记做 ,称为第二类斯特林数。
由于棋盘上每一列是不同的,所以系数 是多余的;此外,要从 列中选出 列放置 个车,需要乘上系数 。
当 时组合数无意义,答案为 。否则, 当 时行和列的情况重复,只有在 时,答案需要乘 来加上每一列上都有车的情况。
Code
1 |
|
后记
乘 之后记得取余。