Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Problemset 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

ISAF 2024 CONTEST

Contest Jul 27, 2024

Problem A. Subtract Operation


Time limit 1000 ms
Mem limit 262144 kB

You are given a list of n integers. You can perform the following operation: you choose an
element x from the list, erase x from the list, and subtract the value of x from all the
remaining elements. Thus, in one operation, the length of the list is decreased by exactly 1.

Given an integer k (k > 0), find if there is some sequence of n − 1 operations such that,
after applying the operations, the only remaining element of the list is equal to k.

Input

The input consists of multiple test cases. The first line contains a single integer t (
) — the number of test cases. Description of the test cases follows.
4
1 ≤ t ≤ 10

The first line of each test case contains two integers n and k (2 ,
5
≤ n ≤ 2 ⋅ 10

), the number of integers in the list, and the target value, respectively.
9
1 ≤ k ≤ 10

The second line of each test case contains the n integers of the list a1 , a2 , … , an (
).
9 9
−10 ≤ ai ≤ 10

It is guaranteed that the sum of n over all test cases is not greater that 2 ⋅ 10 .
5

Output

For each test case, print YES if you can achieve k with a sequence of n − 1 operations.
Otherwise, print NO.

You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be
recognized as a positive answer).

Examples

1
-
Contest Jul 27, 2024

Input Output

4 YES
4 5 NO
4 2 2 7 YES
5 4 NO
1 9 1 3 4
2 17
17 0
2 17
18 18

Note

In the first example we have the list {4, 2, 2, 7}, and we have the target k = 5. One way to
achieve it is the following: first we choose the third element, obtaining the list {2, 0, 5}.
Next we choose the first element, obtaining the list {−2, 3}. Finally, we choose the first
element, obtaining the list {5}.

2
-
Contest Jul 27, 2024

Problem B. Nice Grid


Time limit 2000 ms
Mem limit 1048576 kB

Problem Statement

Print the color of the cell at the R-th row from the top and C -th column from the left in the
following grid with 15 vertical rows and 15 horizontal columns.

Constraints

1 ≤ R, C ≤ 15
R and C are integers.

Input

Input is given from Standard Input in the following format:

R C

Output

- 3
Contest Jul 27, 2024

In the grid above, if the color of the cell at the R-th row from the top and C -th column
from the left is black, then print black ; if the cell is white, then print white . Note that the
judge is case-sensitive.

Sample 1
Input Output

3 5 black

In the grid above, the cell at the 3-rd row from the top and 5-th column from the left is
black. Thus, black should be printed.

Sample 2
Input Output

4 5 white

In the grid above, the cell at the 4-th row from the top and 5-th column from the left is
white. Thus, white should be printed.

-
4
Contest Jul 27, 2024

Problem C. Perfect Square


Time limit 2000 ms
Mem limit 262144 kB

Kristina has a matrix of size n by n , filled with lowercase Latin letters. The value of n is
even.

She wants to change some characters so that her matrix becomes a perfect square. A matrix
is called a perfect square if it remains unchanged when rotated 90 clockwise once.

Here is an example of rotating a matrix by 90 :


In one operation, Kristina can choose any cell and replace its value with the next character
in the alphabet. If the character is equal to "z", its value does not change.

Find the minimum number of operations required to make the matrix a perfect square.

For example, if the 4 by 4 matrix looks like this:

a b b a

b c b b

b c c b

a b b a

then it is enough to apply 1 operation to the letter b, highlighted in bold.

Input

The first line of the input contains a single integer t (1 ) — the number of test
2
≤ t ≤ 10

cases.

Then follows the description of each test case.

The first line of each test case contains a single even integer n (2 ) — the
3
≤ n ≤ 10

number of rows and columns in the matrix.

Then follows n lines, each containing exactly n lowercase Latin letters.

-
5
Contest Jul 27, 2024

It is guaranteed that the sum of n over all test cases does not exceed 10 .
3

Output

For each test case, output a single number on a separate line: the minimum number of
operations required for Kristina to obtain a perfect square.

Examples
Input Output

5 1
4 2
abba 181
bcbb 5
bccb 9
abba
2
ab
ba
6
codefo
rcesco
deforc
escode
forces
codefo
4
baaa
abba
baba
baab
4
bbaa
abba
aaba
abba

Note

The first test case is explained in the problem statement.

- 6
Contest Jul 27, 2024

Problem D. LOOKUP
Time limit 2000 ms
Mem limit 1048576 kB

Problem Statement

You are given strings S and T consisting of lowercase English letters. Determine whether T
is a (contiguous) substring of S .

A string Y is said to be a (contiguous) substring of X if and only if Y can be obtained by


performing the operation below on X zero or more times.

Do one of the following.


Delete the first character in X .
Delete the last character in X .

For instance, tag is a (contiguous) substring of voltage , while ace is not a (contiguous)
substring of atcoder .

Constraints

S and T consist of lowercase English letters.


1 ≤ ∣S∣, ∣T ∣ ≤ 100 (∣X∣ denotes the length of a string X .)

Input

The input is given from Standard Input in the following format:

S
T

Output

If T is a (contiguous) substring of S , print Yes ; otherwise, print No .

Sample 1

-
7
Contest Jul 27, 2024

Input Output

voltage Yes
tag

tag is a (contiguous) substring of voltage .

Sample 2
Input Output

atcoder No
ace

ace is not a (contiguous) substring of atcoder .

Sample 3
Input Output

gorilla No
gorillagorillagorilla

Sample 4
Input Output

toyotasystems Yes
toyotasystems

It is possible that S = T.

-
8
Contest Jul 27, 2024

Problem E. Static Range Minimum Queries


Time limit 1000 ms
Mem limit 524288 kB

Given an array of n integers, your task is to process q queries of the form: what is the
minimum value in range [a, b]?

Input

The first input line has two integers n and q : the number of values and queries.

The second line has n integers x1 , x2 , … , xn : the array values.


​ ​ ​

Finally, there are q lines describing the queries. Each line has two integers a and b: what is
the minimum value in range [a, b]?

Output

Print the result of each query.

Constraints

1 ≤ n, q ≤ 2 ⋅ 105
1 ≤ xi ≤ 109

1≤a≤b≤n

Example

Input Output

8 4 2
3 2 4 5 1 1 5 3 1
2 4 1
5 6 4
1 8
3 3

-
9
Contest Jul 27, 2024

Problem F. Buttons
Time limit 1000 ms
Mem limit 262144 kB

Anna and Katie ended up in a secret laboratory.

There are a + b + c buttons in the laboratory. It turned out that a buttons can only be
pressed by Anna, b buttons can only be pressed by Katie, and c buttons can be pressed by
either of them. Anna and Katie decided to play a game, taking turns pressing these buttons.
Anna makes the first turn. Each button can be pressed at most once, so at some point, one of
the girls will not be able to make her turn.

The girl who cannot press a button loses. Determine who will win if both girls play
optimally.

Input

The first line contains a single integer t (1 ) — the number of test cases.
4
≤ t ≤ 10

Each test case consists of three integers a, b, and c (1 ) — the number of


9
≤ a, b, c ≤ 10

buttons that can only be pressed by Anna, the number of buttons that can only be pressed by
Katie, and the number of buttons that can be pressed by either of them, respectively.

Output

For each test case, output First if Anna wins, or Second if Katie wins.

Examples
Input Output

5 First
1 1 1 First
9 3 3 Second
1 2 3 First
6 6 9 Second
2 2 8

Note

-
10
Contest Jul 27, 2024

For the simplicity of the explanation, we will numerate the buttons by the numbers from 1
to a + b + c: the first a buttons can only be pressed by Anna, the next b buttons can only be
pressed by Katie, and the last c buttons can be pressed by either of them.

In the first test case, Anna can press the 3-rd button on the first turn. Then Katie will press
the 2-nd button (since it is the only possible turn for her). Then Anna will press the 1-st
button. Katie won't have a button to press, so Anna will win.

In the second test case, Anna can press the first nine buttons in some order on her turns. No
matter what buttons Katie will press, all the buttons from the 10-th to the 15-th will be
pressed after 12 turns. On the 13-th turn, Anna will press one of the first nine buttons and
Katie will not have a button to press on her turn. Thus, Anna will win.

In the third test case, the game can proceed as follows:

On the 1-st turn Anna presses the 5-th button.


On the 2-st turn Katie presses the 4-th button.
On the 3-st turn Anna presses the 6-th button.
On the 4-st turn Katie presses the 3-th button.
On the 5-st turn Anna presses the 1-th button.
On the 6-st turn Katie presses the 2-th button.
Anna cannot make the turn, so Katie wins.

It can be shown that Katie can win no matter what moves Anna takes.

-
11
Contest Jul 27, 2024

Problem G. Book Shop


Time limit 1000 ms
Mem limit 524288 kB

You are in a book shop which sells n different books. You know the price and number of
pages of each book.

You have decided that the total price of your purchases will be at most x. What is the
maximum number of pages you can buy? You can buy each book at most once.

Input

The first input line contains two integers n and x: the number of books and the maximum
total price.

The next line contains n integers h1 , h2 , … , hn : the price of each book.


​ ​ ​

The last line contains n integers s1 , s2 , … , sn : the number of pages of each book.
​ ​ ​

Output

Print one integer: the maximum number of pages.

Constraints

1 ≤ n ≤ 1000
1 ≤ x ≤ 105
1 ≤ hi , si ≤ 1000
​ ​

Example

Input Output

4 10 13
4 8 5 3
5 12 8 1

Explanation: You can buy books 1 and 3. Their price is 4 + 5 = 9 and the number of pages is
5 + 8 = 13.

- 12
Contest Jul 27, 2024

Problem H. T-primes
Time limit 2000 ms
Mem limit 262144 kB
Input file stdin
Output file stdout

We know that prime numbers are positive integers that have exactly two distinct positive
divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct
positive divisors.

You are given an array of n positive integers. For each of them determine whether it is Т-
prime or not.

Input

5
The first line contains a single positive integer, n (1 ≤ n ≤ 10 ), showing how many numbers

are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012).

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to
use the cin, cout streams or the %I64d specifier.

Output

Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-

prime, and "NO" (without the quotes), if it isn't.

Examples
Input Output

3 YES
4 5 6 NO
NO

Note

-
13
Contest Jul 27, 2024

The given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and
4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and
5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".

-
14
Contest Jul 27, 2024

Problem I. Tour
Time limit 2000 ms
Mem limit 1048576 kB

Problem Statement

The republic of AtCoder has N cities numbered 1 through N and M roads numbered 1
through M .

Road i leads from City Ai to City Bi , but you cannot use it to get from City Bi to City Ai .
​ ​ ​ ​

Puma is planning her journey where she starts at some city, travels along zero or more
roads, and finishes at some city.

How many pairs of cities can be the origin and destination of Puma's journey? We
distinguish pairs with the same set of cities in different orders.

Constraints

2 ≤ N ≤ 2000
0 ≤ M ≤ min(2000, N (N − 1))
1 ≤ Ai , Bi ≤ N ​ ​

Ai 
= Bi
​ ​

(Ai , Bi ) are distinct.


​ ​

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A1 B1
​ ​


AM BM ​ ​

Output

15
Contest Jul 27, 2024

Print the answer.

Sample 1
Input Output

3 3 7
1 2
2 3
3 2

We have seven pairs of cities that can be the origin and destination:
(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 2), (3, 3).

Sample 2
Input Output

3 0 3

We have three pairs of cities that can be the origin and destination: (1, 1), (2, 2), (3, 3).

Sample 3
Input Output

4 4 16
1 2
2 3
3 4
4 1

Every pair of cities can be the origin and destination.

-
16
Contest Jul 27, 2024

Problem J. Stick Lengths


Time limit 1000 ms
Mem limit 524288 kB

There are n sticks with some lengths. Your task is to modify the sticks so that each stick has
the same length.

You can either lengthen and shorten each stick. Both operations cost x where x is the
difference between the new and original length.

What is the minimum total cost?

Input

The first input line contains an integer n: the number of sticks.

Then there are n integers: p1 , p2 , … , pn : the lengths of the sticks.


​ ​ ​

Output

Print one integer: the minimum total cost.

Constraints

1 ≤ n ≤ 2 ⋅ 105
1 ≤ pi ≤ 109

Example

Input Output

5 5
2 3 1 5 2

-
17
Contest Jul 27, 2024

Problem K. Kefa and First Steps


Time limit 2000 ms
Mem limit 262144 kB

Kefa decided to make some money doing business on the Internet for exactly n days. He
knows that on the i-th day (1 ≤ i ≤ n) he makes ai money. Kefa loves progress, that's why he

wants to know the length of the maximum non-decreasing subsegment in sequence ai. Let
us remind you that the subsegment of the sequence is its continuous fragment. A
subsegment of numbers is called non-decreasing if all numbers in it follow in the non-
decreasing order.

Help Kefa cope with this task!

Input

The first line contains integer n (1 ≤ n ≤ 105).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print a single integer — the length of the maximum non-decreasing subsegment of


sequence a.

Examples
Input Output

6 3
2 2 1 3 4 1

Input Output

3 3
2 2 9

Note

-
18
Contest Jul 27, 2024

In the first test the maximum non-decreasing subsegment is the numbers from the third to
the fifth one.

In the second test the maximum non-decreasing subsegment is the numbers from the first
to the third one.

-
19

You might also like