Problem Solving Set IV
Problem Solving Set IV
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
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.
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
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
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.
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:
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.
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.
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.
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.