There is an equation ax + by + c = 0. Given a,b,c,x1,x2,y1,y2 you must determine, how many integer roots of this equation are satisfy to the following conditions : x1<=x<=x2, y1<=y<=y2. Integer root of this equation is a pair of integer numbers (x,y).
Input contains integer numbers a,b,c,x1,x2,y1,y2 delimited by spaces and line breaks. All numbers are not greater than 108 by absolute value.
Write answer to the output.
1  1 1 3 
1  4 
1 

Let f(n) be a sum of digits for positive integer n. If f(n) is onedigit number then it is a digital root for n and otherwise digital root of n is equal to digital root of f(n). For example, digital root of 987 is 6. Your task is to find digital root for expression A1*A2*...*AN + A1*A2*...*AN1 + ... + A1*A2 + A1
.
Input file consists of few test cases. There is K (1<=K<=5) in the first line of input. Each test case is a line. Positive integer number N is written on the first place of test case (N<=1000). After it there are N positive integer numbers (sequence A). Each of this numbers is nonnegative and not more than 109.
Write one line for every test case. On each line write digital root for given expression.
1  1 
1  5 
1 

You are given an undirected connected graph, with N vertices and N1 edges (a tree). You must find the centroid(s) of the tree.
In order to define the centroid, some integer value will be assosciated to every vertex. Let’s consider the vertex k. If we remove the vertex k from the tree (along with its adjacent edges), the remaining graph will have only N1 vertices and may be composed of more than one connected components. Each of these components is (obviously) a tree. The value associated to vertex k is the largest number of vertices contained by some connected component in the remaining graph, after the removal of vertex k. All the vertices for which the associated value is minimum are considered centroids.
The first line of the input contains the integer number N (1<=N<=16 000). The next N1 lines will contain two integers, a and b, separated by blanks, meaning that there exists an edge between vertex a and vertex b.
You should print two lines. The first line should contain the minimum value associated to the centroid(s) and the number of centroids. The second line should contain the list of vertices which are centroids, sorted in ascending order.
1  7 
1  3 1 
1 

His Royal Highness King of Berland Berl XV was a very wise man and had a very accomplished wife, who was aware of the fact, that prominent and outstanding personalities once having written down their names on the pages of glorious History, remain there forever. His Royal Highness King Berl XV experienced an intrinsic, lost nowadays, deep and sincere sense of respect and trust for his beloved spouse. So he decided to acquire a chronicler of his own. Due to the ambiguous nature of misunderstanding and the crying injustice of history to ambiguity, he decided to leave all his royal responsibilities aside and made up his royal mind to find the chronicler, who will make him famous, depicting all his heroic deeds truthfully and gloriously enough.
The King assembled the greatest minds of his kingdom at the Academic Chroniclers Meeting (ACM), as he named it, and decided to test their might. The task was to build the Smallest Lexicographical Concatenation (SLC) out of the given N strings. SLC of N strings s1,…, sN is the lexicographically smallest their concatenation si1 +… + siN, where i1,…, iN is a permutation of integers from 1 through N. It’s a great privilege to be a chronicler, so don’t miss your chance and don’t screw it up! Make the king choose you!
The first line of the input file contains a single integer N (1 ≤ N ≤ 100) indicating the number of strings. The following N lines contain N strings, one string per line. The length of each string is no more than 100 characters. Each string consists only of lowercase Latin letters. There are no any leading or trailing spaces.
Print the SLC of the given N strings to the output file as a single line.
sample input  sample output 
6 it looks like an easy problem  aneasyitlikelooksproblem 
1 

You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The vases are glued onto the shelf and are numbered consecutively 1 through V, where V is the number of vases, from left to right so that the vase 1 is the leftmost, and the vase V is the rightmost vase. The bunches are moveable and are uniquely identified by integers between 1 and F. These idnumbers have a significance: They determine the required order of appearance of the flower bunches in the row of vases so that the bunch i must be in a vase to the left of the vase containing bunch j whenever i < j. Suppose, for example, you have bunch of azaleas (idnumber=1), a bunch of begonias (idnumber=2) and a bunch of carnations (idnumber=3). Now, all the bunches must be put into the vases keeping their idnumbers in order. The bunch of azaleas must be in a vase to the left of begonias, and the bunch of begonias must be in a vase to the left of carnations. If there are more vases than bunches of flowers then the excess will be left empty. A vase can hold only one bunch of flowers.
Each vase has a distinct characteristic (just like flowers do). Hence, putting a bunch of flowers in a vase results in a certain aesthetic value, expressed by an integer. The aesthetic values are presented in a table as shown below. Leaving a vase empty has an aesthetic value of 0.
V A S E S  
1  
Bunches  1 (azaleas)  7 
2 (begonias)  5  
3 (carnations)  21 
According to the table, azaleas, for example, would look great in vase 2, but they would look awful in vase 4.
To achieve the most pleasant effect you have to maximize the sum of aesthetic values for the arrangement while keeping the required ordering of the flowers. If more than one arrangement has the maximal sum value, any one of them will be acceptable. You have to produce exactly one arrangement.
1 ≤ F ≤ 100 where F is the number of the bunches of flowers. The bunches are numbered 1 through F.
F ≤ V ≤ 100 where V is the number of vases.
50 £ Aij £ 50 where Aij is the aesthetic value obtained by putting the flower bunch i into the vase j.
1  3 5 
1  53 
1 

The sequence of nonnegative integers A1, A2, …, AN is given. You are to find some subsequence Ai1, Ai2, …, Aik (1 <= i1 < i2 < … < ik <= N) such, that Ai1 XOR Ai2 XOR … XOR Aik has a maximum value.
The first line of the input file contains the integer number N (1 <= N <= 100). The second line contains the sequence A1, A2, …, AN (0 <= Ai <= 10^18).
Write to the output file a single integer number – the maximum possible value of Ai1 XOR Ai2 XOR … XOR Aik.
3
11 9 5
14
1 

Five silent philosophers sit at a round table with bowls of spaghetti. Forks are placed between each pair of adjacent philosophers.
Each philosopher must alternately think and eat. However, a philosopher can only eat spaghetti when they have both left and right forks. Each fork can be held by only one philosopher and so a philosopher can use the fork only if it is not being used by another philosopher. After an individual philosopher finishes eating, they need to put down both forks so that the forks become available to others. A philosopher can take the fork on their right or the one on their left as they become available, but cannot start eating before getting both forks.
Eating is not limited by the remaining amounts of spaghetti or stomach space; an infinite supply and an infinite demand are assumed.
Design a discipline of behavior (a concurrent algorithm) such that no philosopher will starve; i.e., each can forever continue to alternate between eating and thinking, assuming that no philosopher can know when others may want to eat or think.
The problem statement and the image above are taken from wikipedia.org
The philosophers’ ids are numbered from 0 to 4 in a clockwise order. Implement the function void wantsToEat(philosopher, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork) where:
Five threads, each representing a philosopher, will simultaneously use one object of your class to simulate the process. It is possible that the function will be called for the same philosopher more than once, even before the last call ends.
Example 1:
1 

Constraints:
1 

Write a program that outputs the string representation of numbers from 1 to n, however:
For example, for n = 15, we output: 1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz.
Suppose you are given the following code:
1  class FizzBuzz { 
Implement a multithreaded version of FizzBuzz with four threads. The same instance of FizzBuzz will be passed to four different threads:
1  class FizzBuzz { 
There are two kinds of threads, oxygen and hydrogen. Your goal is to group these threads to form water molecules. There is a barrier where each thread has to wait until a complete molecule can be formed. Hydrogen and oxygen threads will be given releaseHydrogen and releaseOxygen methods respectively, which will allow them to pass the barrier. These threads should pass the barrier in groups of three, and they must be able to immediately bond with each other to form a water molecule. You must guarantee that all the threads from one molecule bond before any other threads from the next molecule do.
In other words:
Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.
Example 1:
1  Input: "HOH" 
Example 2:
1  Input: "OOHHHH" 
Constraints:
1  class H2O { 
Suppose you are given the following code:
1  class ZeroEvenOdd { 
The same instance of ZeroEvenOdd will be passed to three different threads:
Each of the threads is given a printNumber method to output an integer. Modify the given program to output the series 010203040506… where the length of the series must be 2n.
Example 1:
1  Input: n = 2 
Example 2:
1  Input: n = 5 
1  class ZeroEvenOdd { 
Find amount of numbers for given sequence of integer numbers such that after raising them to the Mth power they will be divided by K.
Input consists of two lines. There are three integer numbers N, M, K (0<N, M, K<10001) on the first line. There are N positive integer numbers − given sequence (each number is not more than 10001) − on the second line.
Write answer for given task.
1  4 2 50 
1  1 
1 

Along the border between states A and B there are N defence outposts. For every outpost k, the interval [Ak,Bk] which is guarded by it is known. Because of financial reasons, the president of country A decided that some of the outposts should be abandoned. In fact, all the redundant outposts will be abandoned. An outpost i is redundant if there exists some outpost j such that Aj<Ai and Bi<Bj. Your task is to find the number of redundant outposts.
The first line of the input will contain the integer number N (1<=N<=16 000). N lines will follow, each of them containing 2 integers: Ak and Bk (0<= Ak < Bk <= 2 000 000 000), separated by blanks. All the numbers Ak will be different. All the numbers Bk will be different.
You should print the number of redundant outposts.
1  5 
1  3 
1 

Social networks are very popular now. They use different types of relationships to organize individual users in a network. In this problem friendship is used as a method to connect users. For each user you are given the list of his friends. Consider friendship as a symmetric relation, so if user a is a friend of user b then b is a friend of a.
A friend of a friend for a is such a user c that c is not a friend of a, but there is such b that b is a friend of a and c is a friend of b. Obviously c ≠ a.
Your task is to find the list of friends of friends for the given user x.
The first line of the input contains integer numbers N and x (1 ≤ N ≤ 50, 1 ≤ x ≤ N), where N is the total number of users and x is user to be processed. Users in the input are specified by their numbers, integers between 1 and N inclusive. The following N lines describe friends list of each user. The ith line contains integer di (0 ≤ di ≤ 50) — number of friends of the ith user. After it there are di distinct integers between 1 and N — friends of the ith user. The list doesn’t contain i. It is guaranteed that if user a is a friend of user b then b is a friend of a.
You should output the number of friends of friends of x in the first line. Second line should contain friends of friends of x printed in the increasing order.
sample input  sample output 

4 2 1 2 2 1 3 2 4 2 1 3  1 4 
sample input  sample output 

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

You are given natural number X. Find such maximum integer number that it square is not greater than X.
Input file contains number X (1≤X≤10^1000).
Write answer in output file.
16
4
1  if __name__ == '__main__': 
Little Johnny likes to draw a lot. A few days ago he painted lots of straight lines on his sheet of paper. Then he counted in how many zones the sheet of paper was split by these lines. He noticed that this number is not always the same. For instance, if he draws 2 lines, the sheet of paper could be split into 4, 3 or even 2 (if the lines are identical) zones. Since he is a very curious kid, he would like to know which is the maximum number of zones into which he can split the sheet of paper, if he draws N lines. The sheet of paper is to be considered a very large (=infinite) rectangle.
The input file will contain an integer number: N (0<=N<=65535).
You should output one integer: the maximum number of zones into which the sheet of paper can be split if Johnny draws N lines.
0
1
1
2
1 

Famous Berland ACMICPC team Anisovka consists of three programmers: Andrew, Michael and Ilya. A long time ago, during the first few months the team was founded, Andrew was very often late to the trainings and contests. To stimulate Andrew to be more punctual, Ilya and Andrew decided to introduce a new rule for team participants. If somebody is late (i.e. comes at least one second after appointed time) he owes a cup of tea to other team members. If he is late for 5 minutes, he owes two cups of tea. If he is late for 15 minutes, he owes three cups of tea. And if he is late for 30 minutes or more, he owes 4 cups of tea.
The training starts at the time S (counted in seconds, from some predefined moment of time) and Andrew comes at the time P (also in seconds, counted from the same moment of time).
Your task is to find how many cups of tea Andrew owes.
The input file contains single line with integer numbers S and P (0 <= S,P <= 10^4).
Write to the output file the number of cups Andrew owes.
10 10
10 11
0 300
0
1
2
1 

Inspired by a “Little Bishops” problem, Petya now wants to solve problem for rooks.
A rook is a piece used in the game of chess which is played on a board of square grids. A rook can only move horizontally and vertically from its current position and two rooks attack each other if one is on the path of the other.
Given two numbers n and k, your job is to determine the number of ways one can put k rooks on an n × n chessboard so that no two of them are in attacking positions.
The input file contains two integers n (1 ≤ n ≤ 10) and k (0 ≤ k ≤ n2).
Print a line containing the total number of ways one can put the given number of rooks on a chessboard of the given size so that no two of them are in attacking positions.
4 4
24
1 

First year of new millenium is gone away. In commemoration of it write a program that finds the name of the day of the week for any date in 2001.
Input is a line with two positive integer numbers N and M, where N is a day number in month M. N and M is not more than 100.
Write current number of the day of the week for given date (Monday – number 1, … , Sunday – number 7) or phrase “Impossible” if such date does not exist.
21 10
7
1 

For given number N you must output amount of Ndigit numbers, such, that last digits of their square is equal to 987654321.
Input contains integer number N (1<=N<=106)
Write answer to the output.
8
0
1 

It is not easy to prepare a problem for a programming contest. Petya and Vasya decided that problem “A+B” needs at least n distinct solutions to be written. It doesn’t matter how many solutions each of them will write, they need to write at least n solutions in total. We know that Petya needs t1 units of time to write a solution, and Vasya needs t2 units of time. They start to work simultaneously at time 0. Thus, for example, Petya finishes writing his first solution at time t1, his second solution at 2 · t1 and so on.
Petya and Vasya are working by the same algorithm. Each time Petya (Vasya) finishes writing a solution, he checks on how many solutions have already been written up to the current time moment t. Ready solutions are the solutions that have been fully written by this time. The solutions that were fully finished exactly at time t are also considered ready. If the number of such solutions is strictly less than n, then Petya (Vasya) starts writing the next solution. If a member of the jury began working on a problem, he doesn’t stop working under any circumstances, and he will surely finish it.
Petya and Vasya realize that if they act on this algorithm, they will not necessarily write exactly n solutions in total. Maybe they’ll write more solutions.
Considering that Petya and Vasya work nonstop, find, how many solutions they wrote in total and the moment when the latest solution was finished. The latest solution is one which was finished last.
The only input line contains three integers n, t1 and t2 (1 ≤ n, t1, t2 ≤ 5000).
Print two integers — m and f, where m is the number of written solutions, and f is the moment when the last solution was finished.
sample input  sample output 

5 2 3  5 6 
sample input  sample output 

5 2 4  6 8 
sample input  sample output 

3 30 50  4 100 
In the first sample Petya finished his solutions at time 2, 4 and 6, and Vasya — at time 3 and 6. They finished writing their last solutions simultaneously, at time 6, and at this exact moment they already had the total of 5 written solutions and stopped working.
1 
