Sie sind auf Seite 1von 24

BEGINNER

CHEF AND LUCKY NUMBER


Mr. Chef has been given a number N. He has a tendency to double whatever he get. So now he
has got the number N with him and he has multiplied the number N by 2. Now Chef is
superstitious. He believes in something known as Lucky Number. His lucky number is defined
as any number, which
when multiplied by 2 has no other factors other than 1, 2, and N. If the number is lucky all you
have to do is print LUCKY NUMBER. If the number is not a lucky number, print Sorry..
Input
The first line consists of T, which is the number of test cases. Every line of the next T lines
consists of N.
Output
Print LUCKY NUMBER if the number is lucky and Sorry if the number is not lucky followed
by a new line.
Constraints
1<=T<=1000
1<=N<=1000000
Input
3
26
12
11
Output:
Sorry
Sorry
LUCKY NUMBER
CHEF AND WAY
After visiting a childhood friend, Chef wants to get back to his home. Friend lives at the first
street, and Chef himself lives at the N-th (and the last) street. Their city is a bit special: you can
move from the X-th street to the Y-th street if and only if 1 <= Y - X <= K, where K is the
integer value that is given to you. Chef wants to get to home in such a way that the product of all
the visited streets' special numbers is minimal (including the first and the N-th street). Please,
help him to find such a product.
Input
The first line of input consists of two integer numbers - N and K - the number of streets and the
value ofK respectively. The second line consist of N numbers - A1, A2, ..., AN respectively,
where Ai equals to the special number of the i-th street.
Output
Please output the value of the minimal possible product, modulo 1000000007.
Constraints
1 N 10^5
1 Ai 10^5
1KN

Example
Input:
42
1 2 3 4.
Output:
8
GREGOIAN CALENDAR
According to Gregorian Calendar, it was Monday on the date 01/01/2001. If any year is input,
Write a program to display what is the day on the 1st January of this year.
Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line
contains an integer year.
Output
Display the day on the 1st January of that year in lowercase letter.
Constraints
1 T 1000
1900 A,B,C 2500
Example
Input
3
1994
1991
2014
Output
saturday
tuesday
wednesday
CHEF AND HIS SEQUENCE
Chef has a sequence of N numbers. He like a sequence better if the sequence contains his
favorite sequence as a substring.
Given the sequence and his favorite sequence(F) check whether the favorite sequence is
contained in the sequence
Input
The first line will contain the number of test cases and are followed by the cases.
Each test case consists of four lines: The length of the sequence, the sequence N,the length of F
and the sequence F
Output
Print "Yes" if the sequence contains the favourite sequence int it otherwise print "No"
Constraints

1<=T<=10
11
Input:
2
6
123456
3
234
6
22 5 6 33 1 4
2
4 15
Output:
Yes
No
CHEF AND DOLLS
Chef is fan of pairs and he likes all things that come in pairs. He even has a doll collection in
which all dolls have paired.One day while going through his collection he found that there are
odd number of dolls. Someone had stolen a doll!!!
Help chef find which type of doll is missing..
Input
The first line contains the number of test cases.
Second line of the input contains the number of elements in the array.
The next n lines are the types of each doll that is left.
Output
Find the type of doll that doesn't have a pair
Constraints
1<=T<=10
1<=N<=100000 (10^5)
1<=ti<=100000
Input:
1
3
1
2
1
Output:
2
Input:
1
5
1
1
2
2

3
Output:
3
FARMER AND HIS PLOT
Santosh has a farm at Byteland. He has a very big family to look after. His life takes a sudden
turn and he runs into a financial crisis. After giving all the money he has in his hand, he decides
to sell some parts of his plots. The specialty of his plot is that it is rectangular in nature. Santosh
comes to know that he will get more money if he sells square shaped plots. So keeping this in
mind, he decides to divide his plot into minimum possible square plots so that he can get
maximum profit out of this.
So your task is to find the minimum number of square plots that can be formed out of the
rectangular plot.
Input
The input consists of T number of test cases. T lines follow. Each line consists of two integers N
and M which denotes the length and breadth of the rectangle.
Output
Output is a single line which denotes the minimum number of square plots that can be formed
Constraints
1<=T<=20
1<=M<=10000
1<=N<=10000
Input:
2
10 15
46
Output:
6
6
GRADE THE STEEL
A certain grade of steel is graded according to the following conditions.
Hardness must be greater than 50.
Carbon content must be less than 0.7.
Tensile strength must be greater than 5600.
The grades are as follows:
Grade is 10 if all three conditions are met.
Grade is 9 if conditions (i) and (ii) are met.
Grade is 8 if conditions (ii) and (iii) are met.
Grade is 7 if conditions (i) and (iii) are met.
Garde is 6 if only one condition is met.
Grade is 5 if none of three conditions 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 1 hardness, carbon content, tensile strength 10000
Example
Input
3
53 0.6 5602
45 0 4500
000
Output
10
6
6
SMALLEST NUMBERS OF NOTES
Consider a currency system in which there are notes of seven denominations, namely, Rs. 1, Rs.
2, Rs. 5, Rs. 10, Rs. 50, Rs. 100.
If the sum of Rs. N is input, write a program to computer smallest number of notes that will
combine to give Rs. N.
Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line
contains an integer N.
Output
Display the smallest number of notes that will combine to give N.
Constraints
1 T 1000
1 N 1000000
Example
Input
3
1200
500
242
Output
12
5
7
int notes[] = {100, 50, 10, 5, 2, 1};
for (i = 0; i < 6 && n; ++i) {
if (notes[i] <= n) {
s += n / notes[i];
n = n % notes[i];

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 toS2i, 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 Ttest 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
andS2 = 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.
DEVU AND GRAPES
Grapes of Coderpur are very famous. Devu went to the market and saw that there were N people
selling grapes. He didnt like it because things were not very structured. So, he gave a task to
Dhinwa to make things better. If Dhinwa successfully completes the task, Devu will be happy.
Devu wants to change the number of grapes in a bucket of zero or more sellers in such a way that
theGCD of all the number of grapes is divisible by K. Dhinwa can add or remove any number of
grapes from each of the buckets. Adding or removing a grape will be counted as an operation.
Also after the operation, none of the sellers bucket should be empty.
Help Dhinwa in finding the minimum number of operations needed to make Devu happy.
Input
First line of input contains an integer T denoting the number of test cases.
For each test case, first line will contain an integer N denoting the number of buckets and
integerK.
Next line contains N space separated integers denoting the number of grapes in each of the
bucket.
Output
For each test case, print a single integer representing the answer of that test case.
Constraints
Subtask #1: 10 points
1 T 10, 1 N ,K 10, 1 number of grapes in a bucket 10
Subtask #2: 10 points
1 T 10, 1 N,K 100, 1 number of grapes in a bucket 100
Subtask #3: 80 points
1 T 10, 1 N 105, 1 K 109, 1 number of grapes in a bucket 109
Example
Input:
2
22
35
37
10 16 18
Output:
2
8
Explanation
For the first test case, add or remove 1 grape in each of the bucket.

For the second test case, remove three grapes in the first bucket, remove two grapes from the
second bucket and add three grapes in the third bucket.
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.
CHEF AND STRING
Chef likes playing with strings. The most interesting game are named "CHEF in string". The
move of the game consists of the following: Chef takes a subsequence of string's letters that
form the word "CHEF" and then he removes that symbols. The goal of the game is to make the

maximal number of moves. Please, help Chef and tell him the maximal possible number of
moves that he is able to make for the given string S.
Input
The first line of each test case contains a given string. This string consists of uppercase letters
from the set {"C", "H", "E", "F"}.
Output
Output a single line containing the maximal possible number of moves.
Constraints
1 |S| 100000
Example
Input:
CHEFCHEFFFF
Output:
2
Input:
CHHHEEEFFCC
Output:
1
BRACKETS
A valid parentheses sequence is a non-empty string where each character is either '(' or ')', which
satisfies the following constraint:
You can find a way to repeat erasing adjacent pairs of parentheses '()' until it becomes empty.
For example, '(())' and '()((()()))' are valid parentheses sequences, but ')()(' and '(()' are not.
Mike has a valid parentheses sequence. He really likes everything about his sequence, except the
fact that it is quite long. So Mike has recently decided that he will replace his parentheses
sequence with a new one in the near future. But not every valid parentheses sequence will satisfy
him. To help you understand his requirements we'll introduce the pseudocode of function F(S):
FUNCTION F( S - a valid parentheses sequence )
BEGIN
balance = 0
max_balance = 0
FOR index FROM 1 TO LENGTH(S)
BEGIN
if S[index] == '(' then balance = balance + 1
if S[index] == ')' then balance = balance - 1
max_balance = max( max_balance, balance )
END
RETURN max_balance
END
In other words, F(S) is equal to the maximal balance over all prefixes of S.
Let's denote A as Mike's current parentheses sequence, and B as a candidate for a new one. Mike
is willing to replace A with B if F(A) is equal to F(B). He would also like to choose B with the
minimal possible length amongst ones satisfying the previous condition. If there are several such

strings with the minimal possible length, then Mike will choose the least one lexicographically,
considering '(' to be less than ')'.
Help Mike!
Input
The first line of the input contains one integer T denoting the number of testcases to process.
The only line of each testcase contains one string A denoting Mike's parentheses sequence. It is
guaranteed that A only consists of the characters '(' and ')'. It is also guaranteed that A is a valid
parentheses sequence.
Output
The output should contain exactly T lines, one line per each testcase in the order of their
appearance. The only line of each testcase should contain one string B denoting the valid
parentheses sequence that should be chosen by Mike to replace A.
Constraints
1 T 5;
1 |A| 100000(105).
Example
Input:
1
()((()()))
Output: ((()))
PIECE OF CAKE
This is a very easy warm-up problem.
You are given a string. Your task is to determine whether number of occurrences of some
character in the string is equal to the sum of the numbers of occurrences of other characters in the
string.
Input
The first line of the input contains an integer T denoting the number of test cases. Each of the
next Tlines contains one string S consisting of lowercase latin letters.
Output
For each test case, output a single line containing "YES" if the string satisfies the condition
given above or "NO" otherwise.
Constraints
1 T 1000
1 length of S 50
Example
Input:
4
acab
zzqzqq
abc
kklkwwww
Output:
YES
YES

NO
YES
VERSION CONTROL SYSTEM
A version control system(VCS) is a repository of files, often the files for the source code of
computer programs, with monitored access. Every change made to the source is tracked, along
with who made the change, why they made it, and references to problems fixed, or enhancements
introduced, by the change.
Version control systems are essential for any form of distributed, collaborative development.
Whether it is the history of a wiki page or large software development project, the ability to track
each change as it was made, and to reverse changes when necessary can make all the difference
between a well managed and controlled process and an uncontrolled first come, first served
system. It can also serve as a mechanism for due diligence for software projects.
In this problem we'll consider a simplified model of a development project. Let's suppose, that
there are N source files in the project. All the source files are distinct and numbered from 1 to N.
A VCS, that is used for maintaining the project, contains two sequences of source files. The first
sequence contains the source files, that are ignored by the VCS. If a source file is not in the first
sequence, then it's considered to be unignored. The second sequence contains the source files,
that are tracked by the VCS. If a source file is not in the second sequence, then it's considered to
be untracked. A source file can either be or not be in any of these two sequences.
Your task is to calculate two values: the number of source files of the project, that are both
tracked and ignored, and the number of source files of the project, that are both untracked and
unignored.
Input
The first line of the input contains an integer T denoting the number of test cases. The
description of Ttest cases follows.
The first line of the test case description contains three integers N, M and K denoting the number
of source files in the project, the number of ignored source files and the number of tracked
source files.
The second line contains M distinct integers denoting the sequence A of ignored source files.
The sequence is strictly increasing.
The third line contains K distinct integers denoting the sequence B of tracked source files. The
sequence is strictly increasing.
Output
For each test case, output a single line containing two integers: the number of the source files,
that are both tracked and ignored, and the number of the source files, that are both untracked and
unignored.
Constraints
1 T 100
1 M, K N 100
1 A1 < A2 < ... < AM N
1 B1 < B2 < ... < BK N
Example
Input:
2
746
1467

123467
422
14
34
Output:
41
11
Explanation
In the first test case, the source files {1, 4, 6, 7} are both tracked and ignored, the source file {5}
is both untracked and unignored.
In the second test case, the source file {4} is both tracked and ignored, the source file {2} is both
untracked and unignored.
GOOD JOKE!
Vadim and Roman like discussing challenging problems with each other. One day Vadim told his
friend following problem:
Given N points on a plane. Each point p is defined by it's two integer coordinates px and py.
The distance between points a and b is min(|ax - bx|, |ay - by|). You should choose a starting point
and make a route visiting every point exactly once, i.e. if we write down numbers of points in
order you visit them we should obtain a permutation. Of course, overall distance walked should
be as small as possible. The number of points may be up to 40.
"40? Maybe 20? Are you kidding?" asked Roman. "No, it's not a joke" replied Vadim. So
Roman had nothing to do, but try to solve this problem. Since Roman is really weak in problem
solving and you are the only friend, except Vadim, with whom Roman can discuss challenging
tasks, he has nobody else to ask for help, but you!
Input
Input description.
The first line of the input contains an integer T denoting the number of test cases. The
description of Ttest cases follows.The first line of each test case contains a single
integer N denoting the number of points on a plane. The following N lines contain two spaceseparated integers each coordinates of points.
Output
Output description.
Output the answer for every test case in a separate line. The answer for every test case is a
permutation of length N. In case there are several solutions that lead to minimal distance walked,
you should choose the lexicographically smallest one. Let P denote such permutation. To make
output smaller, you should output H(P). H(P) = P1 xor P2 xor ... xor PN. Have a look at the
example and it's explanation for better understanding.
Constraints
1 T 10
1 N 40
0 absolute value of each coordinate 1000
1 sum over all N in a single test file 120
Example
Input:

2
2
12
00
3
33
00
03
Output:
3
0
Explanation
For the first test case permutation [1, 2] is optimal. 1 xor 2 = 3.
For the second one both [2, 3, 1] and [1, 3, 2] lead us to the shortest walk, but the second one is
lexicographically smaller. So the answer is H([1, 3, 2]) = 1 xor 3 xor 2 = 0 .
FIT SQUARES IN TRIANGLE
What is the maximum number of squares of size 2x2 that can be fit in a right angled isosceles
triangle of base B.
One side of the square must be parallel to the base of the isosceles triangle.
Base is the shortest side of the triangle
Input
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.
Output
Output exactly T lines, each line containing the required answer.
Constraints
1 T 103
1 B 104
Sample Input
11
1 2 3 4 5 6 7 8 9 10 11(READ ONE BY ONE)
Sample Output
0 0 0 1 1 3 3 6 6 10 10
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
CHEF AND REMISSNESS
Chef is now a corporate person. He has to attend office regularly. But chef does not want to go to
office, rather he wants to stay home and discover different recipes and cook them.
In the office where chef works, has two guards who count how many times a person enters into
the office building. Though the duty of a guard is 24 hour in a day, but sometimes they fall asleep
during their duty and could not track the entry of a person in the office building. But one better

thing is that they never fall asleep at the same time. At least one of them remains awake and
counts who enters into the office.
Now boss of Chef wants to calculate how many times Chef has entered into the building. He
asked to the guard and they give him two integers A and B, count of first guard and second guard
respectively.
Help the boss to count the minimum and maximum number of times Chef could have entered
into the office building.
Input
The first line of the input contains an integer T denoting the number of test cases. The
description of the T test cases follows.
Each test case consists of a line containing two space separated integers A and B.
Output
For each test case, output a single line containing two space separated integers, the minimum and
maximum number of times Chef could have entered into the office building.
Constraints
1 T 100
0 A, B 1000000
Example
Input:
1
19 17
Output:
19 36
SNAPE AND LADDER
Professor Snape has lots of potions. Bottles containing all types of potions are stacked on shelves
which cover the entire wall from floor to ceiling. Professor Snape has broken his bones several
times while climbing the top shelf for retrieving a potion. He decided to get a ladder for him. But
he has no time to visit Diagon Alley. So he instructed Ron Weasley to make a ladder for him.
Professor Snape specifically wants a step ladder which looks like an inverted 'V' from side view.
Professor just mentioned two things before vanishingB - separation between left side (LS) and right side (RS) on the ground
LS - the length of left side
What should be the length of RS? At one extreme LS can be vertical and at other RS can be
vertical. Ron is angry and confused. Since Harry is busy battling Voldemort, its your duty to help
him find the minimum and maximum length of RS.
Input
First line contains single integer T, the number of test cases. Then T lines follow each containing
2 integers - B and LS.
Output
Output T lines, each containing minimum value of RS and maximum value of RS, separated by
space. The answer (RS) will be considered correct if it has relative and absolute error less than
10-2.
Constraints

1 T 1000
1 B < LS 1000
Example
MIN= IF(B<L)->SQRT(L2-B2) IF(B>L)->SQRT(B2-L2)->ELSE->B
Input:
MAX=SQRT(B2+L2)
3
45
10 12
10 20
Output:
3.0 6.40312
6.63325 15.6205
17.3205 22.3607
CUTTING RECIPES
The chef has a recipe he wishes to use for his guests,
but the recipe will make far more food than he can serve to the guests.
The chef therefore would like to make a reduced version of the recipe which has the same ratios
of ingredients, but makes less food.
The chef, however, does not like fractions.
The original recipe contains only whole numbers of ingredients,
and the chef wants the reduced recipe to only contain whole numbers of ingredients as well.
Help the chef determine how much of each ingredient to use in order to make as little food as
possible.
Input
Input will begin with an integer T, the number of test cases.
Each test case consists of a single line.
The line begins with a positive integer N, the number of ingredients.
N integers follow, each indicating the quantity of a particular ingredient that is used.
Output
For each test case, output exactly N space-separated integers on a line,
giving the quantity of each ingredient that the chef should use in order to make as little food as
possible.
Sample Input
3
244
3234
4 3 15 9 6
Sample Output
11
234
1532
Constraints
T100
2N50
All ingredient quantities are between 1 and 1000, inclusive.
Ciel and Receipt

Tomya is a girl. She loves Chef Ciel very much.


Tomya like a positive integer p, and now she wants to get a receipt of Ciel's restaurant whose
total price is exactly p.
The current menus of Ciel's restaurant are shown the following table.
Name of Menu

price

eel flavored water

deep-fried eel bones

clear soup made with eel livers

grilled eel livers served with grated radish

savory egg custard with eel

16

eel fried rice (S)

32

eel fried rice (L)

64

grilled eel wrapped in cooked egg

128

eel curry rice

256

grilled eel over rice

512

deluxe grilled eel over rice

1024

eel full-course

2048

Note that the i-th menu has the price 2i-1 (1 i 12).
Since Tomya is a pretty girl, she cannot eat a lot.
So please find the minimum number of menus whose total price is exactly p.
Note that if she orders the same menu twice, then it is considered as two menus are ordered.
(SeeExplanations for details)
Input
The first line contains an integer T, the number of test cases.
Then T test cases follow.
Each test case contains an integer p.
Output
For each test case, print the minimum number of menus whose total price is exactly p.
Constraints
1T5
1 p 100000 (105)
There exists combinations of menus whose total price is exactly p.
Sample Input
4
10

256
255
4096
Sample Output
2
1
8
2
Explanations
In the first sample, examples of the menus whose total price is 10 are the following:
1+1+1+1+1+1+1+1+1+1 = 10 (10 menus)
1+1+1+1+1+1+1+1+2 = 10 (9 menus)
2+2+2+2+2 = 10 (5 menus)
2+4+4 = 10 (3 menus)
2+8 = 10 (2 menus)
Here the minimum number of menus is 2.
In the last sample, the optimal way is 2048+2048=4096 (2 menus).
Note that there is no menu whose price is 4096.
TRANSFORM THE EXPRESSION
Reverse Polish Notation (RPN) is a mathematical notation where every operator follows all of its
operands. For instance, to add three and four, one would write "3 4 +" rather than "3 + 4". If
there are multiple operations, the operator is given immediately after its second operand; so the
expression written "3 4 + 5" would be written "3 4 5 +" first subtract 4 from 3, then add 5 to
that.
Transform the algebraic expression with brackets into RPN form.
You can assume that for the test cases below only single letters will be used, brackets [] will not
be used and each expression has only one RPN form (no expressions like a*b*c)
Input
The first line contains t, the number of test cases (less then 100).
Followed by t lines, containing an expression to be translated to RPN form, where the length of
the expression is less then 400.
Output
The expressions in RPN form, one per line.
Example
Input:
3
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))
Output:
abc*+
ab+zx+*
at+bac++cd+^*
Packaging Cupcakes

Now that Chef has finished baking and frosting his cupcakes, it's time to package them. Chef
has Ncupcakes, and needs to decide how many cupcakes to place in each package. Each package
must contain the same number of cupcakes. Chef will choose an integer A between 1 and N,
inclusive, and place exactly A cupcakes into each package. Chef makes as many packages as
possible. Chef then gets to eat the remaining cupcakes. Chef enjoys eating cupcakes very much.
Help Chef choose the package size A that will let him eat as many cupcakes as possible.
Input
Input begins with an integer T, the number of test cases. Each test case consists of a single
integer N, the number of cupcakes.
Output
For each test case, output the package size that will maximize the number of leftover cupcakes. If
multiple package sizes will result in the same number of leftover cupcakes, print the largest such
size.
Constraints
1 T 1000
2 N 100000000 (108)
Sample Input
2
2
5
Sample Output
2
3
Explanation
In the first test case, there will be no leftover cupcakes regardless of the size Chef chooses, so he
chooses the largest possible size. In the second test case, there will be 2 leftover cupcakes.
AMBIGUOUS PERMUTATIONS
Some programming contest problems are really tricky: not only do they
require a different output format from what you might have expected, but
also the sample output does not show the difference. For an example,
let us look at permutations.
A permutation of the integers 1 to n is an
ordering of
these integers. So the natural way to represent a permutation is
to list the integers in this order. With n = 5, a
permutation might look like 2, 3, 4, 5, 1.
However, there is another possibility of representing a permutation:
You create a list of numbers where the i-th number is the
position of the integer i in the permutation.
Let us call this second
possibility an inverse permutation. The inverse permutation
for the sequence above is 5, 1, 2, 3, 4.
An ambiguous permutation is a permutation which cannot be
distinguished from its inverse permutation. The permutation 1, 4, 3, 2
for example is ambiguous, because its inverse permutation is the same.

To get rid of such annoying sample test cases, you have to write a
program which detects if a given permutation is ambiguous or not.
Input Specification
The input contains several test cases.
The first line of each test case contains an integer n
(1 n 100000).
Then a permutation of the integers 1 to n follows
in the next line. There is exactly one space character
between consecutive integers.
You can assume that every integer between 1 and n
appears exactly once in the permutation.
The last test case is followed by a zero.
Output Specification
For each test case output whether the permutation is ambiguous or not.
Adhere to the format shown in the sample output.
Sample Input
4
1432
5
23451
1
1
0
Sample Output
ambiguous
not ambiguous
ambiguous
SUMS IN A TRIANGLE
Let's consider a triangle of numbers in which a number appears in the first line, two numbers
appear in the second line, three in the third line, etc. Develop a program which will compute the
largest of the sums of numbers that appear on the paths starting from the top towards the base, so
that:
on each path the next number is located on the row below, more precisely either directly below
or below and one place to the right;
the number of rows is strictly positive, but less than 100
all numbers are positive integers between O and 99.
Input
In the first line integer n - the number of test cases (equal to about 1000).
Then n test cases follow. Each test case starts with the number of lines which is followed by their
content.
Output
For each test case write the determined value in a separate line.
Example
Input:
2

3
1
21
123
4
1
12
412
2311
Output:
5
9
THE LEAD GAME
The game of billiards involves two players knocking 3 balls around
on a green baize table. Well, there is more to it, but for our
purposes this is sufficient.
The game consists of several rounds and in each round both players
obtain a score, based on how well they played. Once all the rounds
have been played, the total score of each player is determined by
adding up the scores in all the rounds and the player with the higher
total score is declared the winner.
The Siruseri Sports Club organises an annual billiards game where
the top two players of Siruseri play against each other. The Manager
of Siruseri Sports Club decided to add his own twist to the game by
changing the rules for determining the winner. In his version, at the
end of each round the leader and her current lead are calculated. Once
all the rounds are over the player who had the maximum lead at the
end of any round in the game is declared the winner.
Consider the following score sheet for a game with 5 rounds:
Round Player 1
Player 2
1
140
82
2
89
134
3
90
110
4
112
106
5
88
90
The total scores of both players, the leader and the lead after
each round for this game is given below:
Round Player 1
Player 2 Leader Lead
1
2
3
4
5

140
229
319
431
519

82
216
326
432
522

Player 1
Player 1
Player 2
Player 2
Player 2

58
13
7
1
3

The winner of this game is Player 1 as he had the maximum lead (58
at the end of round 1) during the game.
Your task is to help the Manager find the winner and the winning
lead. You may assume that the scores will be such that there will
always be a single winner. That is, there are no ties.
Input
The first line of the input will contain a single integer N (N
10000) indicating the number of rounds in the game. Lines
2,3,...,N+1 describe the scores of the two players in the N rounds.
Line i+1 contains two integer Si and Ti, the scores of the Player 1
and 2 respectively, in round i. You may assume that 1 Si
1000 and 1 Ti 1000.
Output
Your output must consist of a single line containing two integers
W and L, where W is 1 or 2 and indicates the winner and L is the
maximum lead attained by the winner.
Example
Input:
5
140 82
89 134
90 110
112 106
88 90
Output:
1 58
FACTORIAL
The most important part of a GSM network is so called Base Transceiver Station (BTS). These
transceivers form the areas called cells (this term gave the name to the cellular phone) and every
phone connects to the BTS with the strongest signal (in a little simplified view). Of course,
BTSes need some attention and technicians need to check their function periodically.
The technicians faced a very interesting problem recently. Given a set of BTSes to visit, they
needed to find the shortest path to visit all of the given points and return back to the central
company building. Programmers have spent several months studying this problem but with no
results. They were unable to find the solution fast enough. After a long time, one of the
programmers found this problem in a conference article. Unfortunately, he found that the
problem is so called "Traveling Salesman Problem" and it is very hard to solve. If we have N
BTSes to be visited, we can visit them in any order, giving us N! possibilities to examine. The
function expressing that number is called factorial and can be computed as a product
1.2.3.4....N. The number is very high even for a relatively small N.
The programmers understood they had no chance to solve the problem. But because they have
already received the research grant from the government, they needed to continue with their
studies and produce at least some results. So they started to study behavior of the factorial
function.
For example, they defined the function Z. For any positive integer N, Z(N) is the number of
zeros at the end of the decimal form of number N!. They noticed that this function never

decreases. If we have two numbers N1<N2, then Z(N1) <= Z(N2). It is because we can never
"lose" any
trailing zero by multiplying by any positive number. We can only get new and new zeros. The
function Z is very interesting, so we need a computer program that can determine its value
efficiently.
Input
There is a single positive integer T on the first line of input (equal to about 100000). It stands for
the number of numbers to follow. Then there are T lines, each containing exactly one positive
integer number N, 1 <= N <= 1000000000.
Output
For every number N, output a single line containing the single non-negative integer Z(N).
Example
Sample Input:
6
3
60
100
1024
23456
8735373
Sample Output:
0
14
24
253
5861
2183837
ENORMOUS INPUT TEST
The purpose of this problem is to verify whether the method you are using to read input data is
sufficiently fast to handle problems branded with the enormous Input/Output warning. You are
expected to be able to process at least 2.5MB of input data per second at runtime.
Input
The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one
positive integer ti, not greater than 109, each.
Output
Write a single integer to output, denoting how many integers ti are divisible by k.
Example
Input:
73
1
51
966369
7
9
999996
11

Output:
4

Das könnte Ihnen auch gefallen