Question CP-2 (Lab Final - C+D)
Question CP-2 (Lab Final - C+D)
Football
time limit per test
2 seconds
memory limit per test
256 megabytes
One day Vasya decided to have a look at the results of Berland 1910 Football Championship’s finals.
Unfortunately he didn't find the overall score of the match; however, he got hold of a profound description of the
match's process. On the whole there are n lines in that description each of which described one goal. Every goal
was marked with the name of the team that had scored it. Help Vasya, learn the name of the team that won the
finals. It is guaranteed that the match did not end in a tie.
Input
The first line contains an integer n (1 ≤ n ≤ 100) — the number of lines in the description. Then follow n lines —
for each goal the names of the teams that scored it. The names are non-empty lines consisting of uppercase
Latin letters whose lengths do not exceed 10 symbols. It is guaranteed that the match did not end in a tie and
the description contains no more than two different teams.
Output
Print the name of the winning team. We remind you that in football the team that scores more goals is
considered the winner.
Examples
Input
Copy
1
ABC
Output
Copy
ABC
Input
Copy
5
A
ABA
ABA
A
A
Output
Copy
A
A. Lucky Division
time limit per test
2 seconds
memory limit per test
256 megabytes
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal
representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky
and 5, 17, 467 are not.
Petya calls a number almost lucky if it could be evenly divided by some lucky number. Help him find out if the
given number n is almost lucky.
Input
The single line contains an integer n (1 ≤ n ≤ 1000) — the number that needs to be checked.
Output
In the only line print "YES" (without the quotes), if number n is almost lucky. Otherwise, print "NO" (without the
quotes).
Examples
Input
Copy
47
Output
Copy
YES
Input
Copy
16
Output
Copy
YES
Input
Copy
78
Output
Copy
NO
Note
Note that all lucky numbers are almost lucky as any number is evenly divisible by itself.
Caps lock is a computer keyboard key. Pressing it sets an input mode in which typed letters are capital by
default. If it is pressed by accident, it leads to accidents like the one we had in the first passage.
Let's consider that a word has been typed with the Caps lock key accidentally switched on, if:
either it only contains uppercase letters;
or all letters except for the first one are uppercase.
In this case we should automatically change the case of all letters. For example, the case of the letters that form
words "hELLO", "HTTP", "z" should be changed.
Write a program that applies the rule mentioned above. If the rule cannot be applied, the program should leave
the word unchanged.
Input
The first line of the input data contains a word consisting of uppercase and lowercase Latin letters. The word's
length is from 1 to 100 characters, inclusive.
Output
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
The first line contains a single positive integer, n (1 ≤ n ≤ 105), 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
Copy
3
4 5 6
Output
Copy
YES
NO
NO
Note
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".
A. New Year Transportation
time limit per test
2 seconds
memory limit per test
256 megabytes
New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as
a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty
of escaping the cell. People wanted to meet people who live in other cells.
So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year.
First, he thought of n - 1 positive integers a1, a2, ..., an - 1. For every integer i where 1 ≤ i ≤ n - 1 the
condition 1 ≤ ai ≤ n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th
(1 ≤ i ≤ n - 1) portal connects cell i and cell (i + ai), and one can travel from cell i to cell (i + ai) using the i-th
portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + ai) to
cell i using the i-th portal. It is easy to see that because of condition 1 ≤ ai ≤ n - i one can't leave the Line World
using portals.
Currently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go
there. Please determine whether I can go to cell t by only using the construted transportation system.
Input
The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) and t (2 ≤ t ≤ n) — the number of cells,
and the index of the cell which I want to go to.
The second line contains n - 1 space-separated integers a1, a2, ..., an - 1 (1 ≤ ai ≤ n - i). It is guaranteed, that
using the given transportation system, one cannot leave the Line World.
Output
If I can go to cell t using the transportation system, print "YES". Otherwise, print "NO".
Examples
Input
Copy
8 4
1 2 1 2 1 2 1
Output
Copy
YES
Input
Copy
8 5
1 2 1 2 1 1 1
Output
Copy
NO
Note
In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.
In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to
visit.
A. Raising Bacteria
time limit per test
1 second
memory limit per test
256 megabytes
You are a lover of bacteria. You want to raise some bacteria in a box.
Initially, the box is empty. Each morning, you can put any number of bacteria into the box. And each night, every
bacterium in the box will split into two bacteria. You hope to see exactly x bacteria in the box at some moment.
What is the minimum number of bacteria you need to put into the box across those days?
Input
Input
Copy
8
Output
Copy
1
Note
For the first sample, we can add one bacterium in the box in the first day morning and at the third morning there
will be 4 bacteria in the box. Now we put one more resulting 5 in the box. We added 2 bacteria in the process so
the answer is 2.
For the second sample, we can put one in the first morning and in the 4-th morning there will be 8 in the box. So
the answer is 1.
A. The Way to Home
time limit per test
1 second
memory limit per test
256 megabytes
A frog lives on the axis Ox and needs to reach home which is in the point n. She starts from the point 1. The
frog can jump to the right at a distance not more than d. So, after she jumped from the point x she can reach the
point x + a, where a is an integer from 1 to d.
For each point from 1 to n is known if there is a lily flower in it. The frog can jump only in points with a lilies.
Guaranteed that there are lilies in the points 1 and n.
Determine the minimal number of jumps that the frog needs to reach home which is in the point n from the
point 1. Consider that initially the frog is in the point 1. If the frog can not reach home, print -1.
Input
The first line contains two integers n and d (2 ≤ n ≤ 100, 1 ≤ d ≤ n - 1) — the point, which the frog wants to
reach, and the maximal length of the frog jump.
The second line contains a string s of length n, consisting of zeros and ones. If a character of the string s equals
to zero, then in the corresponding point there is no lily flower. In the other case, in the corresponding point there
is a lily flower. Guaranteed that the first and the last characters of the string s equal to one.
Output
In the other case, print the minimal number of jumps that the frog needs to reach the home which is in the
point n from the point 1.
Examples
Input
Copy
8 4
10010101
Output
Copy
2
Input
Copy
4 2
1001
Output
Copy
-1
Input
Copy
8 4
11100101
Output
Copy
3
Input
Copy
12 3
101111100101
Output
Copy
4
Note
In the first example the from can reach home in two jumps: the first jump from the point 1 to the point 4 (the
length of the jump is three), and the second jump from the point 4 to the point 8 (the length of the jump is four).
In the second example the frog can not reach home, because to make it she need to jump on a distance three,
but the maximum length of her jump equals to two.
There is a n×mn×m grid. You are standing at cell (1,1)(1,1) and your goal is to finish at cell (n,m)(n,m).
You can move to the neighboring cells to the right or down. In other words, suppose you are standing at
cell (x,y)(x,y). You can:
move right to the cell (x,y+1)(x,y+1) — it costs xx burles;
move down to the cell (x+1,y)(x+1,y) — it costs yy burles.
Can you reach cell (n,m)(n,m) spending exactly kk burles?
Input
The first line contains the single integer tt (1≤t≤1001≤t≤100) — the number of test cases.
The first and only line of each test case contains three integers nn, mm,
and kk (1≤n,m≤1001≤n,m≤100; 0≤k≤1040≤k≤104) — the sizes of grid and the exact amount of money you
need to spend.
Output
For each test case, if you can reach cell (n,m)(n,m) spending exactly kk burles, print YES. Otherwise, print NO.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all
recognized as positive answer).
Example
Input
Copy
6
1 1 0
2 2 2
2 2 3
2 2 4
1 4 3
100 100 10000
Output
Copy
YES
NO
YES
NO
YES
NO
Note
In the first test case, you are already in the final cell, so you spend 00 burles.
In the second, third and fourth test cases, there are two paths
from (1,1)(1,1) to (2,2)(2,2): (1,1)(1,1) →→ (1,2)(1,2) →→ (2,2)(2,2) or (1,1)(1,1) →→ (2,1)(2,1) →→ (2,2)(
2,2). Both costs 1+2=31+2=3 burles, so it's the only amount of money you can spend.
In the fifth test case, there is the only way from (1,1)(1,1) to (1,4)(1,4) and it costs 1+1+1=31+1+1=3 burles.
B. ICPC Balloons
time limit per test
1 second
memory limit per test
256 megabytes
Determine the total number of balloons that the teams received. Note that some problems may be solved by
none of the teams.
Input
The first line of the input contains an integer tt (1≤t≤1001≤t≤100) — the number of testcases.
The first line of each test case contains an integer nn (1≤n≤501≤n≤50) — the length of the string.
The second line of each test case contains a string ss of length nn consisting of uppercase English letters,
denoting the order of solved problems.
Output
For each test case, output a single integer — the total number of balloons that the teams received.
Example
Input
Copy
6
3
ABA
1
A
3
ORZ
5
BAAAA
4
BKPT
10
CODEFORCES
Output
Copy
5
2
6
7
8
17
Note
In the first test case, 55 balloons are given out:
Problem AA is solved. That team receives 22 balloons: one because they solved the problem, an an
additional one because they are the first team to solve problem AA.
Problem BB is solved. That team receives 22 balloons: one because they solved the problem, an an
additional one because they are the first team to solve problem BB.
Problem AA is solved. That team receives only 11 balloon, because they solved the problem. Note that
they don't get an additional balloon because they are not the first team to solve problem AA.
The total number of balloons given out is 2+2+1=52+2+1=5.
In the second test case, there is only one problem solved. The team who solved it receives 22 balloons: one
because they solved the problem, an an additional one because they are the first team to solve problem AA.
C. Can I Square?
time limit per test
1 second
memory limit per test
256 megabytes
Calin has nn buckets, the ii-th of which contains aiai wooden squares of side length 11.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be
recognized as a positive answer).
Example
Input
Copy
5
1
9
2
14 2
7
1 2 3 4 5 6 7
6
1 3 5 7 9 11
4
2 2 2 2
Output
Copy
YES
YES
NO
YES
NO
Note
In the first test case, Calin can build a 3×33×3 square.
In the second test case, Calin can build a 4×44×4 square.
In the third test case, Calin cannot build a square using all the given squares.
A. Nene's Game
time limit per test
1 second
memory limit per test
256 megabytes
In this game, initially nn players are lined up in a row. In each of the rounds of this game, the following happens:
Nene finds the a1a1-th, a2a2-th, ……, akak-th players in a row. They are kicked out of the game
simultaneously. If the ii-th player in a row should be kicked out, but there are fewer than ii players in a
row, they are skipped.
Once no one is kicked out of the game in some round, all the players that are still in the game are declared as
winners.
For example, consider the game with a=[3,5]a=[3,5] and n=5n=5 players. Let the players be named player A,
player B, ……, player E in the order they are lined up initially. Then,
Before the first round, players are lined up as ABCDE. Nene finds the 33-rd and the 55-th players in a
row. These are players C and E. They are kicked out in the first round.
Now players are lined up as ABD. Nene finds the 33-rd and the 55-th players in a row. The 33-rd player
is player D and there is no 55-th player in a row. Thus, only player D is kicked out in the second round.
In the third round, no one is kicked out of the game, so the game ends after this round.
Players A and B are declared as the winners.
Nene has not yet decided how many people would join the game initially. Nene gave
you qq integers n1,n2,…,nqn1,n2,…,nq and you should answer the following question for
each 1≤i≤q1≤i≤q independently:
How many people would be declared as winners if there are nini players in the game initially?
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤2501≤t≤250). The
description of test cases follows.
The first line case contains two integers kk and qq (1≤k,q≤1001≤k,q≤100) — the length of the sequence aa and
the number of values nini you should solve this problem for.
The second line contains kk integers a1,a2,…,aka1,a2,…,ak (1≤a1<a2<…<ak≤1001≤a1<a2<…<ak≤100) — the
sequence aa.
The third line contains qq integers n1,n2,…,nqn1,n2,…,nq (1≤ni≤1001≤ni≤100).
Output
For each test case, output qq integers: the ii-th (1≤i≤q1≤i≤q) of them should be the number of players declared
as winners if initially nini players join the game.
Example
Input
Copy
6
2 1
3 5
5
5 3
2 4 6 7 9
1 3 5
5 4
3 4 5 6 7
1 2 3 4
2 3
69 96
1 10 100
1 1
100
50
3 3
10 20 30
1 10 100
Output
Copy
2
1 1 1
1 2 2 2
1 10 68
50
1 9 9
Note
Today, Sakurako was studying arrays. An array aa of length nn is considered good if and only if:
Example
Input
Copy
5
1 2
1 5
2 2
10 20
1 1000000000
Output
Copy
2
3
1
5
44721
Note
For l=1l=1 and r=5r=5, one possible array could be (1,2,5)(1,2,5). It can be proven that an array of
length 44 does not exist for the given ll and rr.
For l=2l=2 and r=2r=2, the only possible array is (2)(2).
For l=10l=10 and r=20r=20, the only possible array is (10,11,13,16,20)(10,11,13,16,20).
A. Meaning Mean
time limit per test
1 second
memory limit per test
256 megabytes
Pak Chanek has an array aa of nn positive integers. Since he is currently learning how to calculate the floored
average of two numbers, he wants to practice it on his array aa.
While the array aa has at least two elements, Pak Chanek will perform the following three-step operation:
1. Pick two different indices ii and jj (1≤i,j≤|a|1≤i,j≤|a|; i≠ji≠j), note that |a||a| denotes the current size of the
array aa.
2. Append ⌊ai+aj2⌋⌊ai+aj2⌋∗∗ to the end of the array.
3. Remove elements aiai and ajaj from the array and concatenate the remaining parts of the array.
For example, suppose that a=[5,4,3,2,1,1]a=[5,4,3,2,1,1]. If we choose i=1i=1 and j=5j=5, the resulting array
will be a=[4,3,2,1,3]a=[4,3,2,1,3]. If we choose i=4i=4 and j=3j=3, the resulting array will
be a=[5,4,1,1,2]a=[5,4,1,1,2].
After all operations, the array will consist of a single element xx. Find the maximum possible value of xx if Pak
Chanek performs the operations optimally.
∗∗⌊x⌋⌊x⌋ denotes the floor function of xx, which is the greatest integer that is less than or equal to xx. For
example, ⌊6⌋=6⌊6⌋=6, ⌊2.5⌋=2⌊2.5⌋=2, ⌊−3.6⌋=−4⌊−3.6⌋=−4 and ⌊π⌋=3⌊π⌋=3
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤50001≤t≤5000).
The description of the test cases follows.
The first line of each test case contains a single integer nn (2≤n≤502≤n≤50) — the length of the array aa.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the
elements of the array aa.
Do note that the sum of nn over all test cases is not bounded.
Output
For each test case, output a single integer: the maximum possible value of xx after all numbers have been
picked.
Example
Input
Copy
3
5
1 7 8 4 5
3
2 6 5
5
5 5 5 5 5
Output
Copy
6
4
5
Note
In the first test case, the array is initially a=[1,7,8,4,5]a=[1,7,8,4,5]. Pak Chanek will perform the following
operations:
1. Pick i=1i=1 and j=2j=2, then a=[8,4,5,4]a=[8,4,5,4].
2. Pick i=3i=3 and j=2j=2, then a=[8,4,4]a=[8,4,4].
3. Pick i=2i=2 and j=3j=3, then a=[8,4]a=[8,4].
4. Pick i=1i=1 and j=2j=2, then a=[6]a=[6].
After all the operations, the array consists of a single element x=6x=6. It can be proven that there is no series of
operations that results in xx greater than 66 in the end.
In a desert city with a hilly landscape, the city hall decided to level the road surface by purchasing a dump truck.
The road is divided into nn sections, numbered from 11 to nn from left to right. The height of the surface in the ii-
th section is equal to aiai. If the height of the ii-th section is greater than 00, then the dump truck must take sand
from the ii-th section of the road, and if the height of the ii-th section is less than 00, the dump truck must fill the
pit in the ii-th section of the road with sand. It is guaranteed that the initial heights are not equal to 00.
When the dump truck is in the ii-th section of the road, it can either take away xx units of sand, in which case the
height of the surface in the ii-th section will decrease by xx, or it can fill in xx units of sand (provided that it
currently has at least xx units of sand in its bed), in which case the height of the surface in the ii-th section of the
road will increase by xx.
The dump truck can start its journey from any section of the road. Moving to an adjacent section on the left or
right takes 11 minute, and the time for loading and unloading sand can be neglected. The dump truck has an
infinite capacity and is initially empty.
You need to find the minimum time required for the dump truck to level the sand so that the height in each
section becomes equal to 00. Note that after all movements, the dump truck may still have sand left in its bed.
You need to solve this problem independently for the segments numbered from lili to riri. Sand outside the
segment cannot be used.
Input
Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the
number of test cases. The description of the test cases follows.
The first line of each test case contains two integers nn and qq (1≤n,q≤3⋅1051≤n,q≤3⋅105) — the number of
sections and the number of queries.
The second line of each test case
contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109, ai≠0ai≠0) — the initial height in each
section.
The ii-th of the following qq lines contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n) — the boundaries of the
segment of sections for which the minimum time needs to be determined.
It is guaranteed that the sum of nn over all test cases and the sum of qq over all test cases do not
exceed 3⋅1053⋅105.
Output
For each query, output the minimum time required to level the sand in the segment [li,ri][li,ri], or −1−1 if it is
impossible.
Example
Input
Copy
5
1 1
-179
1 1
5 3
-2 2 -1 3 -1
2 4
1 5
1 3
7 1
1 1 1 -4 1 1 1
1 7
7 2
2 -2 2 -2 1 2 -1
1 7
2 7
4 4
1000000000 1000000000 999999999 -1000000000
2 4
3 4
2 3
1 3
Output
Copy
-1
2
5
-1
8
6
6
2
-1
1
2
Note
In the first test case, 179179 units of sand need to be added to the only section. However, there is nowhere to
take it from, so this is impossible.
There are two screens which can display sequences of uppercase Latin letters. Initially, both screens display
nothing.