You are on page 1of 16

A.

Little Aliya and Chocolate Bar


100 Points · Limits 1s, 512 MB

Little Aliya stood first in her school this year. Hearing this news,
her father gifted her a rectangular chocolate bar. Little Aliya wants
to share the chocolate bar with her younger brother Hasan. She
divided the chocolate bar into two parts. To be sure if she divided
the chocolate bar into two almost equal parts, she has to know
the area of the divided parts. As little Aliya doesn't know
geometry, she wants you to help her.

You’ll be given the ratio of the two divided parts. You’ve to tell her
the area of the divided parts.

Input
The first line contains two integers a and b (1 ≤ a, b ≤  1000),
indicating the height and width of the chocolate bar.

The second line contains two integers c and d (1 ≤ c, d  ≤ 1000),


indicating the ratio of the two divided parts.

Output
For each test case, print two integers a1, a2 - the area of the two
divided parts. The answer will be considered correct if its absolute
or relative error does not exceed 10^{-4}10−4.

Sample
Input Output

4 5 8.0000 12.0000
2 3

 In the first test case, if we divide the chocolate bar into two
parts as (4 ✕ 2) and (4 ✕ 3) then the ratio of the area will be
(2 ∶ 3).

B. Diagonals
100 Points · Limits 1s, 512 MB
Given an array of n integers. All integers of the given array are
pairwise distinct. Your task is to place each element of the array
at the lattice points of the diagonals of a two-dimensional
coordinate system i.e. at position (i, j) such that, a_kak = i + j.

Find the maximum number of elements that can be placed at


the same diagonal.

You can place a number P at position (i, j) having (i + j = P) and


you can't place more than 1 number in a specific position.

A lattice point is a point in a Cartesian coordinate system such


that both its X and Y coordinates are integers. For example, (1,
2), (5, 3), (7, 7), etc. are lattice points.

See the note section for more clarifications.

Input
The first line contains a single integer t (1≤ t ≤ 100). then t test
cases follow.

The second line contains a single integer n (1 ≤ n ≤10^5105) size


of the array a.

The third line contains n integers (2 ≤ a_kak≤ 10^9109)  elements


of the array a.

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

Output
For each test case, print a single integer X - the maximum
number of elements of the same diagonal.

Sample
Input Output

1 2
3
5 11 10

 Here, the numbers 5 and 11 are placed on the same


diagonal. Though 5 and 11 could have been placed on other
diagonals too. But this is one of the optimal solutions. This
can be proven that the maximum number of elements in the
same diagonal will be 2 for the test case.

C. How to Train Your Dragon


100 Points · Limits 1s, 512 MB

After the city of Berk started living in harmony with the dragons,
there are now many Dragon riders in the village. To entertain
them, Hiccup decided to invent a game.

A sheep is placed on a train. Which moves about in the whole


village. And the dragon riders spread out across the village  have
to capture the sheep and put them in their respective nets.

For simplicity, let the city of Berk be a 2D grid of size n*n.

The train containing the sheep will start moving from the position
(1,1). And it will move back and forth along the main diagonal
each second  as shown below:

(1,1),(2,2),(3,3),..............(n,n),(n-1,n-1)(n-2,n-2)(n-3,n-3),..........

You will be given the initial position of the players. Can you find
the minimum time needed for the dragon riders to reach the
sheep?

The player can move only in 4 directions from its current


position(x,y) in 1 second:

up(x,y+1), down(x,y-1),left(x-1,y) and right(x+1,y).


Or stay in the same position.

There can be multiple players in the same spot.


Input
The first line of input contains a single integer T (1 ≤ T ≤
10) denoting the number of test cases.

First line of each test cases will contain 2 integers, n and m (1 ≤


n,m ≤ 10^5105) denoting the size of the 2D grid and number of
players respectively.

This will be followed by m lines where the i-th line will contain 2


integers, x_ixiand  y_iyi (1 ≤ x_ixi,y_iyi ≤ n) denting the starting
position of the i-th player.

There will be a blank line after every test case.

Output
You have to output a single line for each test case denoting the
minimum time needed for the players to reach the sheep. 

Sample
Input Output

2 0
1
1 1
1 1

4 5
4 1
3 2
Input Output

4 3
2 4
1 4

In the second test case, the second player will move from (3,2) to
(2,2) in 1 second. and the train will move from (1,1) to (2,2) in one
second. so the player 2 can reach the train in one second.

D. Beautiful City
100 Points · Limits 1s, 512 MB

There is a country named ‘A’ which has n city. The cities are
connected by (n-1) two way roads. It is guaranteed that there is
no cycle and all cities are connected. Those cities are beautiful
which are connected with at most 1 city.

In this problem, you are given a city ‘x’ to find the distance of the
nearest beautiful city from x.

Input
The first line of input contains an integer n denoting the number of
cities in country ‘A’. The cities are numbered from 1, 2, 3, …., n.
Then there are n-1 lines describing the roads. Each line contains
two integers u and v - there is a road between city u and v.

Then there is an integer q which denotes the number of queries.

Each of the next q lines contains an integer x which is the city


number.

1 <= n,q <= 200000

1 <= u,v,x <= n

Output
For each query output the distance of the nearest beautiful city.

Sample
Input Output

5 1
1 2 0
1 3
3 4
3 5
2
3
5

E. Gandalf and the Dictionary


100 Points · Limits 1s, 512 MB
Gandalf loves to play with his dictionary. Recently he came up
with a new game to play with it.

There are regular and magical words in his dictionary. Magical


words are those, which can be obtained by concatenating exactly
two words of any kind.

Suppose, his dictionary contains 4 words {half, phoenix, blood,


halfblood}, here {half, phoenix, blood} are regular words, but
{halfblood} is a magical word, as it can be obtained by
concatenating two words {half, blood} from his dictionary.

Gandalf wants to count how many magical words there are in his
dictionary, help him.

Input
Each test contains multiple test cases. The first line contains the
number of test cases T (1 ≤ T ≤ 10). Description of the test cases
follows.

The first line of each test case contains a single integer N (1 ≤ N


≤ 10^5105) - number of words in the dictionary.

Next N lines each contains a string S (1 ≤ |S| ≤ 10) - where |S| is


the length of the string and it consists of lowercase English
letters.

It is guaranteed that the sum of N over all test cases does not


exceed 10^5105.
Output
For each test case, print a single integer - the count of magical
words in his dictionary.

Sample
Input Output

2 1
4 1
half
phoenix
blood
halfblood
3
ab
goblet
abab

The first test case is described in the statement.

In the second test case, {abab} can be obtained by concatenating {ab} twice.

F. Attentiveness
100 Points · Limits 1s, 512 MB
Jamil sir is really concerned about student’s attentiveness in the
class. Recently he got an idea. He will track students’ attendance
from Zoom Meeting by using their chat responses. He will ask
them some questions in a zoom meeting session and students
may or may not respond to those questions. A response will be
valid for a question if it is written after that question is asked and
till the next question occurs from Jamil sir. If Jamil sir
asked X questions, then Jamil sir will consider a student present if
he responds to at least ⌈ X/2 ⌉  different questions
( here ⌈A⌉ means ceil of A).

Sometimes Jamil sir may write normal chat which will not be
considered as a question.

Zoom meeting chat will be in the following format:

 If Jamil sir asks a question it will appear as - “Jamil: Q “


followed by the question Jamil sir wants to ask. Ex: “Jamil:
Q What is the time complexity of linear search?” (without
quotation).

 If Jamil sir writes a normal chat it will appear as - “Jamil: “


followed by the written chat. Here ‘Q’ will not be followed by
“Jamil:”. Ex: “Jamil: C181013, turn your mic off.”

 If a student gives a response it will appear as - “student_id: “


followed by the response. Here student id will be a seven-
character string whose first character is ‘C’ and the
remaining are digits. Ex: “C181013: Sir, the time
complexity will be, order of N.”(without quotation).

As the midterm is coming, Jamil sir is really busy. So he wants


you to track the student’s attendance and give him the ID of the
students who will be considered as present.
Note: In a zoom session Jamil sir may ask at most nine
questions and students will be at most thirty.

Input
Input will consist of some line. Which will represent one of those - 
Jamil sir’s question or Jamil sir’s random chat or student’s
response for any question.

Process the input till end of file.

There will not be more than 500 lines in total and each line will
consist of at most 100 characters.

Output
Print the IDs of the students who will be considered as present
according to the above-mentioned rules. You should print the IDs
in lexicographically ascending order.

Sample
Input Output

Jamil: Q How many times does the loop occur in C100001


my code? C100003
C100001: 50 times sir. C100004
C100002: No no, I think it is 150 times.
C100005: My network is disturbing sir. But it
may be 10 times.
C100002: Sir sir sir. 150 was wrong. It will
be 1500.
Input Output

Jamil: C100003, What is your answer ?


C100003: it may be 90.
Jamil: Q What will be the base case of this
recursion ?
C100006: i <= 50.
C100001: it may be i == 50, but I can do it in
another way.
C100003: Confusing for me. I am guessing i <
50.
C100004: It is really easy. i + 1 < 50.
Jamil: Q How many of you know about dp ?
C100007: DP means what sir?
C100008: I am the master of it, sir.
C100007: Please someone tell me what is this ?
C100004: I know dp a little bit.
C100007: What is dp ? What is dp ? What is
dp ?
C100007: Please tell me about it !!!

G. Mr.X and a Map


100 Points · Limits 1s, 512 MB

Mr. X has a map. The map contains an even number of cities of


a country Y. The interesting thing is that every city has an integer
value, given by the government of that country. Now,  Mr. X wants
to play a game with these values.

He wants to take any two cities from the map and calculate the
absolute difference of values of those two cities. He can’t use
those two cities again, so he will cut off the cities from the map by
using a pencil. Then he will take another two cities and calculate
the absolute difference and cut those cities off. He will continue
this until all the cities on the map are cut off. After calculating all
the absolute differences he will sum them up.

What he wants is that the sum should be maximum. But he


doesn’t know how to select the cities so that the sum of the
absolute differences will be maximum. So he wants your help.

Input
At first, you will be given an even integer N, the number of cities.
You will be given N positive integers after that which are the
values of each city. Here,

1 <= N <= 10^5

1 <= value of each city <= 1000

Output
You will have to calculate the maximum sum according to the
above-mentioned way.

Sample
Input Output

4 2
5 6 4 5
Input Output

Here, He can select cities 1 and 3 first, with values 5 and 4 respectively. The absolu
difference between these two values is 1. Then the cities 2 and 4 are selected. Again
the absolute difference is 1. So the sum is 2. It can be proven that the sum over all
absolute differences, in this case, can not be more than 2.

H. Optimus PRIME!!
100 Points · Limits 1s, 512 MB

After the Autobots defeated the Decepticons, they are now


looking for another planet to save. They have numbered all the
planets in the milky way galaxy from 1 to 10^{15}1015.

Optimus is obsessed with prime numbers so he decided to


choose the planets which can be represented as the multiplication
of 3 or more consecutive prime numbers.

Can you help the autobots decide which planets to save??

Input
The first line will contain a single integer T (1 ≤ T ≤ 100000)
- denoting the number of test cases.

Each line will contain a single integer N (1 ≤ N ≤ 10^{15}1015) -


denoting the number of the  planet Autobots want to save.
Output
Output “NO” , if Optimas will not choose the planet. Otherwise
output “YES”.

If the answer is “YES”, you have to print another line where you
have to print the prime factorization of N in ascending order.

Sample
Input Output

3 YES
30 2 3 5
100 NO
70 NO

You might also like