You are on page 1of 9

Problem Solving Set IV

1. GCD and LCM


Two integers A and B are the inputs. Write a program to find GCD and LCM of A and B.

Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an
integer A and B.

Output
Display the GCD and LCM of A and B separated by space respectively.

Constraints
 1 ≤ T ≤ 1000
 1 ≤ A,B ≤ 1000000

Example
Input
3
120 140
10213 312
10 30

Output

20 840
1 3186456
10 30
2. Tanu and Head-bob
Tanu has got interested in signs and gestures that we use for communication. One such gesture is the head-
bob.
When we want to signal "Yes" to someone, we move the head up-and-down. For "No", the head is moved
left-and-right, rotating about the vertical axis.
There is a peculiar way of gesturing "Yes", commonly seen in India, by moving head sideways (rotating
about the forward-back axis). This is called the Indian head-bob.
Tanu observed many people on the railways station, and made a list of gestures that they made. Usual
"Yes" gesture is recorded as "Y", no as "N" and Indian "Yes" gesture as "I". (Assume no foreigner uses the
Indian "Yes" gesture and vice-versa). Identify which of them were Indians, which were not Indian, and
which one you cannot be sure about.

Input
First line contains T, number of people observed by Tanu.
Each person is described in two lines. First line of the description contains a single integer N, the number of gestures
recorded for this person. Next line contains a string of N characters, each character can be "Y", "N" or "I".

Output
For each person, print "INDIAN" if he/she is from India, "NOT INDIAN" if not from India, and "NOT SURE" if the
information is insufficient to make a decision.

Constraints
 For 30 points: 1 ≤ T,N ≤ 100
 For 70 points: 1 ≤ T,N ≤ 1000

Example
Input:
3
5
NNNYY
6
NNINNI
4
NNNN

Output:
NOT INDIAN
INDIAN
NOT SURE

3. Puppy and Sum


Yesterday, puppy Tuzik learned a magically efficient method to find the sum of the integers from 1 to N.
He denotes it as sum(N). But today, as a true explorer, he defined his own new function: sum(D, N), which
means the operation sum applied D times: the first time to N, and each subsequent time to the result of the
previous operation.
For example, if D = 2 and N = 3, then sum(2, 3) equals to sum(sum(3)) = sum(1 + 2 + 3) = sum(6) = 21.
Tuzik wants to calculate some values of the sum(D, N) function. Will you help him with that?

Input
The first line contains a single integer T, the number of test cases. Each test case is described by a single
line containing two integers D and N.

Output
For each testcase, output one integer on a separate line.

Constraints
 1 ≤ T ≤ 16
 1 ≤ D, N ≤ 4

Example
Input:
2
14
23

Output:
10
21

Explanation:
The first test case: sum(1, 4) = sum(4) = 1 + 2 + 3 + 4 = 10.
The second test case: sum(2, 3) = sum(sum(3)) = sum(1 + 2 + 3) = sum(6) = 1 + 2 + 3 + 4 + 5 + 6 = 21.

4. Gross Salary
In a company an emplopyee is paid as under: If his basic salary is less than Rs. 1500, then HRA = 10% of
base salary and DA = 90% of basic salary.
If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the
Employee's salary is input, write a program to find his gross salary.

NOTE: Gross Salary = Basic Salary + HRA + DA

Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an
integer salary.

Output
Output the gross salary of the employee. Your answer will be considered correct if the absolute error is less
than 10-2.

Constraints
 1 ≤ T ≤ 1000
 1 ≤ salary ≤ 100000

Example
Input
3
1203
10042
1312

Output
2406.00
20383.16
2624

5. Three Way Communications


The Chef likes to stay in touch with his staff. So, the Chef, the head server, and the sous-chef all carry two-
way transceivers so they can stay in constant contact. Of course, these transceivers have a limited range so
if two are too far apart, they cannot communicate directly.
The Chef invested in top-of-the-line transceivers which have a few advanced features. One is that even if
two people cannot talk directly because they are out of range, if there is another transceiver that is close
enough to both, then the two transceivers can still communicate with each other using the third transceiver
as an intermediate device.
There has been a minor emergency in the Chef's restaurant and he needs to communicate with both the
head server and the sous-chef right away. Help the Chef determine if it is possible for all three people to
communicate with each other, even if two must communicate through the third because they are too far
apart.

Input
The first line contains a single positive integer T ≤ 100 indicating the number of test cases to follow. The
first line of each test case contains a positive integer R ≤ 1,000 indicating that two transceivers can
communicate directly without an intermediate transceiver if they are at most R meters away from each
other. The remaining three lines of the test case describe the current locations of the Chef, the head server,
and the sous-chef, respectively. Each such line contains two integers X,Y (at most 10,000 in absolute
value) indicating that the respective person is located at position X,Y.

Output
For each test case you are to output a single line containing a single string. If it is possible for all three to
communicate then you should output "yes". Otherwise, you should output "no".
To be clear, we say that two transceivers are close enough to communicate directly if the length of the
straight line connecting their X,Y coordinates is at most R.

Example
Input:
3
1
01
00
10
2
01
00
10
2
00
02
21

Output:
yes
yes
no

6. Grade The Steel


A certain grade of steel is graded according to the following conditions.

The grades are as follows:

i) and (ii) are met.

Write a program, if the user gives values of hardness, carbon content and tensile strength of the steel under
consideration and display the grade of the steel.

Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line contains three
numbers hardness, carbon content and tensile strength of the steel.

Output
Print Grade of the steel depending on Conditions.

Constraints
 1 ≤ T ≤ 1000
 0≤ hardness, carbon content, tensile strength ≤ 10000

Example
Input

3
53 0.6 5602
45 0 4500
000
Output

10
6
6

7. Greedy puppy
Tuzik is a little dog. But despite the fact he is still a puppy he already knows about the pretty things that
coins are. He knows that for every coin he can get very tasty bone from his master. He believes that some
day he will find a treasure and have loads of bones.
And finally he found something interesting. A wooden chest containing N coins! But as you should
remember, Tuzik is just a little dog, and so he can't open it by himself. Actually, the only thing he can
really do is barking. He can use his barking to attract nearby people and seek their help. He can set the
loudness of his barking very precisely, and therefore you can assume that he can choose to call any number
of people, from a minimum of 1, to a maximum of K.
When people come and open the chest they divide all the coins between them in such a way that everyone
will get the same amount of coins and this amount is maximal possible. If some coins are not used they will
leave it on the ground and Tuzik will take them after they go away. Since Tuzik is clearly not a fool, he
understands that his profit depends on the number of people he will call. While Tuzik works on his barking,
you have to find the maximum possible number of coins he can get.

Input
The first line of the input contains an integer T denoting the number of test cases. Each of next T lines
contains 2 space-separated integers: N and K, for this test case.

Output
For each test case output one integer - the maximum possible number of coins Tuzik can get.

Constraints
 1 ≤ T ≤ 50
 1 ≤ N, K ≤ 105

Example
Input:
2
52
11 3

Output:
1
2

Explanation
In the first example he should call two people. Each of them will take 2 coins and they will leave 1 coin
for Tuzik.
In the second example he should call 3 people.

8. Closing the Tweets


Little kids, Jack and Evan like playing their favorite game Glass-and-Stone. Today they want to play
something new and came across Twitter on their father's laptop.
They saw it for the first time but were already getting bored to see a bunch of sentences having at most 140
characters each. The only thing they liked to play with it is, closing and opening tweets.
There are N tweets on the page and each tweet can be opened by clicking on it, to see some statistics
related to that tweet. Initially all the tweets are closed. Clicking on an open tweet closes it and clicking on a
closed tweet opens it. There is also a button to close all the open tweets. Given a sequence of K clicks by
Jack, Evan has to guess the total number of open tweets just after each click. Please help Evan in this game.

Input
First line contains two integers N K, the number of tweets (numbered 1 to N) and the number of clicks
respectively (1 ≤ N, K ≤ 1000). Each of the following K lines has one of the following.
 CLICK X , where X is the tweet number (1 ≤ X ≤ N)
 CLOSEALL

Output
Output K lines, where the ith line should contain the number of open tweets just after the ith click.

Example
Input:
36
CLICK 1
CLICK 2
CLICK 3
CLICK 2
CLOSEALL
CLICK 1

Output:
1
2
3
2
0
1

Explanation:

Let open[x] = 1 if the xth tweet is open and 0 if its closed.


Initially open[1..3] = { 0 , 0 , 0 }. Here is the state of open[1..3] after each click and corresponding count of open
tweets.

CLICK 1 : { 1, 0, 0 }, open count = 1


CLICK 2 : { 1, 1, 0 }, open count = 2
CLICK 3 : { 1, 1, 1 }, open count = 3
CLICK 2 : { 1, 0, 1 }, open count = 2
CLOSEALL : { 0, 0, 0 }, open count = 0
CLICK 1 : { 1, 0, 0 }, open count = 1

9. Decrement OR Increment
Write a program to obtain a number NN and increment its value by 1 if the number is divisible by
4 otherwise decrement its value by 1.

Input:
 First line will contain a number NN.

Output:
Output a single line, the new value of the number.

Constraints
 0≤N≤10000≤N≤1000

Sample Input:
5

Sample Output:
4

EXPLANATION:
Since 5 is not divisible by 4 hence, its value is decreased by 1.

10. Chef and Two Strings


Chef has found two very old sheets of paper, each of which originally contained a string of lowercase Latin
letters. The strings on both the sheets have equal lengths. However, since the sheets are very old, some
letters have become unreadable.
Chef would like to estimate the difference between these strings. Let's assume that the first string is
named S1, and the second S2. The unreadable symbols are specified with the question mark symbol '?'.
The difference between the strings equals to the number of positions i, such that S1i is not equal to S2i,
where S1i and S2i denote the symbol at the i the position in S1 and S2, respectively.
Chef would like to know the minimal and the maximal difference between the two strings, if he changes all
unreadable symbols to lowercase Latin letters. Now that you're fully aware of Chef's
programming expertise, you might have guessed that he needs you help solving this problem as well. Go
on, help him!

Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test
cases follows.
The first line of a test case contains a string S1.
The second line of a test case contains a string S2.
Both strings consist of lowercase Latin letters and question marks in places where the symbols are
unreadable.

Output
For each test case, output the minimal and the maximal difference between two given strings separated with
a single space.

Constraints
 1 ≤ T ≤ 100
 1 ≤ |S1|, |S2| ≤ 100
 Subtask 1 (25 points): |S1| = 1
 Subtask 2 (10 points): neither S1 nor S2 contains unreadable symbols
 Subtask 3 (65 points): 1 ≤ |S1|, |S2| ≤ 100

Example
Input:
3
a?c
??b
???a
???a
?abac
aba?w

Output:
13
03
35

Explanation
Example case 1. You can change the question marks in the strings so that you
obtain S1 = abc and S2 = abb. Then S1 and S2 will differ in one position. On the other hand, you can change
the letters so that S1 = abc and S2 = bab. Then, the strings will differ in all three positions.
Example case 2. Change the question marks this way: S1 = dcba, S2 = dcba, then the strings will differ
in 0 positions. You can also change the question marks so that S1 = aaaa, S2 = dcba, then the strings will
differ in 3 positions.
Example case 3. Change the question marks this way: S1 = aabac, S2 = abaaw, then the strings will differ
in 3 positions. Then, change the question marks this way: S1 = xabac, S2 = abayw, then they will differ
in 5 positions.

11. Chef and Fruits


Today is Chef's birthday. His mom has surprised him with truly fruity gifts: 2 fruit baskets. The first
basket contains N apples, and the second one contains M oranges. Chef likes apples and oranges very much
but he likes them equally, and therefore, wants to have the minimum possible difference between the
number of apples and oranges he has. To do so, he can purchase 1 apple or 1 orange by paying
exactly 1 gold coin (that's some expensive fruit, eh?). Chef can purchase fruits at most K times (as he
has only K gold coins in his pocket) to make the difference the minimum possible.
Our little Chef is busy in celebrating his birthday to the fullest, and therefore, he has handed this job to his
best friend — you. Can you help him by finding the minimum possible difference he can achieve between
the number of apples and orange he owns?

Input
The first line of input contains a single integer T denoting the number of test cases. The first and only line
of each test case contains 3 space separated integers — N, M and K — denoting the number of apples,
number of oranges, and number of gold coins our little Chef has.

Output
For each test case, output the minimum possible difference between the number of apples and oranges that
Chef can achieve.

Constraints
 1 ≤ T ≤ 100
 1 ≤ N, M, K ≤ 100

Example
Input
3
341
521
343

Output
0
2
0

Explanation
 Test 1: Chef will buy 1 apple by paying 1 gold coin and will have equal number of apples and oranges.
 Test 2: Chef will buy 1 orange by paying 1 gold coin and will have 5 apples and 3 oranges.

12. Cops and the Thief Devu


There are 100 houses located on a straight line. The first house is numbered 1 and the last one is numbered
100. Some M houses out of these 100 are occupied by cops.
Thief Devu has just stolen PeePee's bag and is looking for a house to hide in.
PeePee uses fast 4G Internet and sends the message to all the cops that a thief named Devu has just stolen
her bag and ran into some house.
Devu knows that the cops run at a maximum speed of x houses per minute in a straight line and they will
search for a maximum of y minutes. Devu wants to know how many houses are safe for him to escape from
the cops. Help him in getting this information.

Input
First line contains T, the number of test cases to follow.
First line of each test case contains 3 space separated integers: M, x and y.
For each test case, the second line contains M space separated integers which represent the house numbers
where the cops are residing.

Output
For each test case, output a single line containing the number of houses which are safe to hide from cops.

Constraints
 1 ≤ T ≤ 104
 1 ≤ x, y, M ≤ 10

Example
Input:
3
478
12 52 56 8
2 10 2
21 75
258
10 51

Output:
0
18
9

Explanation
Example 1 : Cops in house 12 can cover houses 1 to 68, and cops in house 52 can cover the rest of the
houses. So, there is no safe house.
Example 2 : Cops in house 21 can cover houses 1 to 41, and cops in house 75 can cover houses 55 to 95,
leaving houses numbered 42 to 54, and 96 to 100 safe. So, in total 18 houses are safe.

You might also like