Plus
Pin


Problem description Progress
ID 32973. SNTP
Темы: Case Study   

In order for computers to maintain up-to-date time, they can access SNTP (Simple Network Time Protocol) time servers. Unfortunately, the computer cannot simply get the time from the server, because information is not transmitted over the network instantly: until the message with the current time reaches the computer, it will lose its relevance. The protocol of interaction between the client (computer requesting the exact time) and the server (computer issuing the exact time) is as follows:
 
1) The client sends a request to the server and remembers the departure time A (client time).
2) The server receives the request at time B (exact server time) and sends a message to the client containing the time B.
3) The client receives a response to its request at time C (client time) and remembers it. Now the client, under the assumption that the network delays in the transmission of messages from the client to the server and from the server to the client are the same, can determine and set the exact time for itself using the known values ​​​​A, B, C.

You have to implement an algorithm that determines the exact time to install on the client with known A, B, and C to the nearest second. If necessary, round the result to an integer number of seconds according to the rules of arithmetic (down if the fractional part of the number is less than ½, otherwise large side). 

It is possible that a new day passed by client time while the client was waiting for a response, but it is known that less than 24 hours elapsed between the client sending a request and receiving a response from the server. 

The program receives as input three timestamps A, B, C, one in each line. All timestamps are in the format "hh:mm:ss", where "hh" – this is the clock, «mm» – minutes, "ss" – seconds. Hours, minutes, and seconds are written as exactly two digits each (possibly with extra leading zeros). 
 
The program should output one timestamp in the format described in the input, – calculated exact time to install on the client. The output should not contain spaces, empty lines at the beginning of the output.

Enter Output Note
15:01:00
18:09:45
15:01:40
18:10:05 The client sent the request at 15:01:00 local time, the server received the request at 18:09:45 local time. The client received a response at 15:01:40, at which point the exact time will be 18:10:05.

ID 32947. Watch
Темы: Case Study    Conditional operator    Simulation tasks   

An electronic ink wristwatch can display the current time in several different forms. One of the forms is an imitation of a mechanical watch with arrows. The watch dial is divided into 12 large hour divisions, and each of them - into 5 small divisions. The angle between the small divisions on the dial is 6. To save energy, the image is redrawn once a minute when you need to move the minute hand. The hour hand also moves discretely, moving every 12 minutes by one small division. Thus at 12:35 the hour hand will point to the 2nd small division to the right of 12 o'clock, and the minute hand will point to 7 o'clock. The angle between the arrows at this moment is 162°. At 12:36 the hour hand will move to the 3rd small division after 12 o'clock and the minute — to the next minor division after 7 o'clock. The angle between the hands of the clock will not change.

Write a program that calculates the value of the "internal" the (smaller) angle between the hour hand and the minute hand at a given time.
The first line of input contains two integers separated by a single space — clock time, hours H and minutes M (\(1 <= H <= 12, 0 <= M <= 59\)).< /div>
Print a single integer between 0 and 180 — the angle between the arrows in degrees.
 
Examples
# Input Output
1 12 35 162

ID 38142. Comfortable cows
Темы: Queue    Case Study   

Farmer Eureka's pasture can be viewed as a huge 2D grid of cells (a chessboard). Initially, the pasture is empty.

Farmer Yurik adds N (\(1<=N<=10^5\)) cows to pasture one by one. ith cow will occupy the cell (xi,yi ) which is different from the cells occupied by all other cows (\(0<=x_i,y_i<=1000\)).

A cow is called "comfortable" if it has exactly three horizontal and vertical neighbors. Comfortable cows produce less milk, so Farmer Yurik wants to add cows until there are comfortable cows (including the one he will add). Note that the cows you add do not have to have  x and y coordinates in the range \(0… 1000\).

For each i in the range \(1…N\), print the minimum number of cows it needs to add so that there are no comfortable cows left, assuming that only cows are in the pasture \(1…i\).



Input
The first line contains the integer N. Each of the following N lines contains 2 space-separated integers (xy) indicating  cell coordinates with a cow.

Imprint
The minimum number of cows Farmer Yurik must add, for each i in the \(1…N\) range, on a separate line.
 
 
Examples
# Input Output Explanation
1 9
0 1
10
1 1
1 2
2 1
2 2
3 1
3 2
4 1
0
0
0
1
0
0
1
2
4

For i=4, Farmer Yurik must add a cow to position (2,1),
to make the cow in position (1,1) uncomfortable.

For i=9, the best Farmer can do is add cows at positions (2,0), (3,0), (2,−1), (2, 3).

ID 38273. New Capital
Темы: Bypass in width    Case Study   

In the country from the previous task, there are many specialists not only in the protection of children, but also in the design of cities. Therefore, in order to solve the problem of traffic jams in the overcrowded capital once and for all, it was decided to build a new capital and move the entire government there. It is said — done.

The streets in the new capital form a regular rectangular grid in which all streets intersect at exactly one local length unit. Vertically running streets are called streets, and horizontally running — alleys. In total, the city got 2000 streets and 2000 alleys, therefore, in order not to come up with many new names, they were all simply numbered. The streets were numbered from west to east with numbers from −1000 to 999, and alleys — from south to north, also numbers from −1000 to 999. The city center is considered to be blocks at the intersection of streets and alleys with numbers from −100 to 100.

In order to increase the capacity of roads in the city, it was decided to make all streets and alleys one-way. On streets with even numbers it is allowed to drive only from north to south, and on streets with odd numbers — only from south to north. Similarly, even-numbered alleys can only be driven from east to west, and odd-numbered — only from west to east.

How many local units of length will the mayor of the new capital have to pass every evening, returning home from the mayor's office of the city? Both the city hall and the mayor's house are located in the city center. The mayor is driving home by the shortest route, observing, however, the rules of the road.

Input
The first line contains two numbers x1 and y1 — street number and alley number at the intersection of which the city hall is located. The second line contains two numbers x2 and y2 — street number and alley number at the intersection of which the mayor's house is located. All numbers are integers and do not exceed 100 in absolute value.

Imprint
Print one number: the length of the shortest path from the mayor's office to the mayor's house by car.-
 

Examples
# Input Output
1 0 0
1 1
4
2 3 5
24
4

ID 38292. Candy selection
Темы: One-Dimensional Arrays    Case Study   

Harry Potter's birthday is coming soon! Hermione wants to prepare an unusual gift for him. She wants to give Harry a set of n magic candies. Each candy is characterized by its taste — an integer ti . The pleasure that Harry will get from a set of sweets — is the sum of the flavors of all the candies in that set. Note that the flavors of the candies, like Harry's pleasure, do not have to be positive.

Hermione has a huge box of sweets that contains exactly one candy flavor t for every integer t from - 109 to 109. Hermione wants to take n chocolates from this box to make Harry's set.

Hermione wants Harry to enjoy the set of sweets given to him, exactly equal to the integer s . Help her choose the right set, or determine that it's not possible.

Input
The first line of the input contains a single integer n ( 1 ≤ n ≤ 100 ) — how many sweets Hermione wants to put in the set. The second line of the input contains a single integer s ( - 109 ≤ s ≤ 109 ) — the pleasure Harry must get from the set.

Imprint
If it is impossible to make the desired set of candies from Hermione's, print « NO ". Otherwise, in the first line print « YES », and in the second line randomly n numbers — candy flavors in the desired set. If there are several correct answers, print any of them.

Examples
# Input Output
1 3
10
YES
500000000 -500000000 10

ID 38299. Checking the machine
Темы: Case Study   

Andryusha — young engineer. Now he is designing a modern machine for converting numbers. In the process of construction, more and more blocks are added to the automaton, and Andryusha is interested in how the automaton will work after each such modification.

The automaton is a sequence of blocks of two types: maximizers and minimizers. Each block has some natural number x written on it. The maximizer takes a natural number a as input and outputs the number max ( x , a ) . The minimizer takes a natural number a as input and outputs the number min ( x , a ) .

The automaton works as follows: it takes some natural number, which it feeds to the input of the first block, then what came out of the output of the first block is fed to the input of the second block, and so on. As a result, the machine returns the number obtained at the output of the last block. In other words, the automaton simply passes the number given to it sequentially through all the blocks.

Initially, there is not a single block in the machine, and it simply returns the number it accepts.

Andryusha consistently performs actions with the machine gun. Actions are of three types:

  1. Add a maximizer with the number x written on it to the end of the sequence of automaton blocks.
  2. Add a minimizer with the number x written on it to the end of the automaton block sequence.
  3. Feed the number x to the automaton. In this case, Andryusha wants to know what the machine will return to the exit.
Andryusha has already planned what actions and in what order he will perform. Write a program that will determine the result of Andryusha's automaton, so that he can make sure that it works!

Input
The first line of the input contains a single integer n ( 1 ≤ n ≤ 4·105 ) — the total number of Andryusha's actions.

Each of the next n lines contains two integers t and x ( 1 ≤ t ≤ 3 , 1 ≤ x ≤ 109 ), where t — this is the next action type. If t = 1 , then Andryusha wants to add a maximizer to the automaton, on which the number x is written. If t = 2 , then Andryusha wants to add a minimizer to the automaton, on which the number x is written. If t = 3 , then Andryusha wants to feed the automaton a number x and find out what the output will be.

Imprint
For each action of the third type, print in a separate line one number, which should be the output of the automaton after this action.
 
Examples
# Input Output
1 7
3 5
15
3 2
37
27
38
36
5
5
7
7
6

ID 38491. Cards for three
Темы: Simple games    Case Study   

Antoninus, Balbinus and Caesar play the game "Cards for three", the algorithm of which is as follows:
- first, each of the three players has a deck consisting of a certain number of cards. Each card has the letter a, b or c written on it. The order of cards in decks cannot be changed;
- The players take turns. Antonin goes first;
- if there is at least one card in the current player's deck, he must discard the top card in the deck;
- The next turn goes to the player whose name begins with the letter on the discarded card (a - Antoninus, b - Balbinus, c - Caesar );
- if the current player's deck is empty, the game ends and the current player wins the game.
You are given the starting player decks (Sa, Sb, Sc). The state of Antonin's deck is written in the line Sa, where i-th (\(1<=i<=len(S_a)\ )) character is the letter in the i-th card in the deck. The Balbin string (Sb) and the Caesar string () are described in the same way. 
Determine the winner of the game.

Input
The input is three non-zero strings Sa, Sb  and Sc, each on a new line. The length of each line is no more than 100 characters. Each line consists only of the letters a, b, or c.

Imprint
If Antonin won. then print the letter A, if Balbin - the letter B, if Caesar - the letter C.
 

 

Examples
# Input Output Explanations
1
aca
accc
ca
A
The game will evolve as follows:

Antoninus discards the top card of his deck, a. Antonin makes his next move.
Antoninus discards the top card of his deck, c. Caesar is next.
Caesar discards the top card of his deck, c. Caesar is next.
Caesar discards the top card of his deck: a. Antonin makes his next move.
Antoninus discards the top card of his deck: a. Antonin makes his next move.
Antonin's deck is empty. The game ends and Antonin wins the game.
2
abcb
aacb
bcc
C
 

 

ID 38489. Raft
Темы: Case Study   

A rectangular raft floats in the middle of the lake. The sides of the raft are directed along parallels and meridians. Let's introduce a coordinate system in which the OX  axis is directed to the east, and the OY axis – on North. Let the southwest corner of the raft have the coordinates  (x1, y1), the northeast corner – coordinates (x2, y2).
The swimmer is at the point with coordinates (x, y). Determine which side of the raft (north, south, west, or east) or which corner of the raft (northwest, northeast, southwest, southeast) the swimmer needs to swim to get to as soon as possible to the raft.
The program receives six numbers as input in the following order: x1, y1 (coordinates of the southwest corner of the raft), x2, y 2 (coordinates of the northeast corner of the raft), x, y (coordinates swimmer). All numbers are integers and do not exceed 100 in absolute value. It is guaranteed that x1 < x2, y1 < y2, x ≠ x1, x ≠ x2, y ≠ y1, y ≠ y2, the swimmer coordinates are outside the raft.
If the swimmer should swim to the north side of the raft, the program should output the character  "N", to the south– the symbol "S", to the western – symbol "W", to the eastern – the symbol "E". If the swimmer should swim to the corner of the raft, print one of the following lines: "NW", "NE", "SW", "SE".
 

Examples
# Input Output Explanation
1 -1
-2
5
3
-4
6
NW

ID 38493. Divisibility
Темы: Case Study   

Today at school in a math lesson they are divisible. To demonstrate the properties of divisibility, the teacher wrote down on the board all the integers from 1 to
N into several groups, and if one number is divisible by another, then they necessarily ended up in different groups. For example, if you take N = 10, you get 4 groups.

  • First group: 1.
  • Second group: 2, 7, 9.
  • Third group: 3, 4, 10.
  • Fourth group: 5, 6, 8.
You guessed it, since any number is divisible by 1, one group will always consist of only the number 1, but otherwise such a split can be done in various ways. You are required to determine the minimum number of groups into which all numbers from 1 to N can be divided in accordance with the above condition.
The program receives as input one natural number N, not exceeding 109, and should output a single number – the desired minimum number of groups.
Examples
# Input Output
1 10 4

ID 38513. Equations of Mathematical Magic
Темы: Case Study    Bit operations   

Colossal! — exclaimed the hunchback. — Programmer! We need a programmer.
Arkady and Boris Strugatsky, Monday starts on Saturday
Studying the book "Equations of Mathematical Magic" Roman Oira-Oira and Cristobal Junta discovered an interesting equation: a−(a⊕x)−x=0 for a given a, where   ⊕   stands for bitwise exclusive OR (XOR) of two numbers (this operation is denoted as ^ or xor in many modern programming languages). Since this equation was intended to be solved on the Aldan-3 machine, all calculations were performed on non-negative integers modulo 232. Oira-Oira quickly found x, which is a solution, but Cristobal Junta found Oira-Oira's result not interesting enough, so he asked a colleague how many solutions there are to this equation. Since all calculations are done modulo 232, Cristobal Junto is interested in the number of solutions x such that 0 ≤ x≤ 232. Such a task turned out to be too difficult for Oira-Oira, so he turned to you for help.

Input
The first line contains a single integer a (0 ≤ a ≤ 232−1).

Imprint
Print a single integer — the number of non-negative solutions of the equation.

Note
Let's define the bitwise OR (XOR) operation. Let two non-negative integers x and y be given, consider their binary representations (possibly with leading zeros): xk...x2x1x0 and yk...y2y1y0 . Here xi is the i-th bit of x and yi is the i-th bit of y. Let r=x⊕y — the result of an XOR operation on the numbers x and y. Then the binary representation of r is rk...r2r1r0, where:

\(r_i = \begin{cases} 1, & \quad \text{if } x_i \neq y_i \\ 0 , & \quad \text{if } x_i = y_i \end{cases} \)

In the first example, the solutions to the equation are 0 and 2147483648=231, because 0−(0⊕0)−0=0−0−0=0 and 0−(0⊕ 2147483648)− 2147483648=−4294967296=−232=0 modulo 232.

In the second example, the solutions to the equation are 0, 2, 2147483648=231 and 2147483650=231+2.

In the third example, the solutions are all x for which 0 ≤ x≤ 232.
 
Examples
# Input Output
1 0 2
2 2 4
3 4294967295 4294967296

ID 38749. Vote
Темы: Cycles    Constructive    Case Study   

The shorties decided to take Dunno or Donut on a flight to the moon. Unable to reach an agreement, they decided to vote. Dunno and Donut watch the voting summary. The Shorties show Dunno and Donut the ratio of the current number of votes received by Dunno and Donut, but not the actual number of votes. Dunno and Donut looked at the report N times, and when they looked at it the i (1<=i<=N) times, the ratio was Pi:Ni. Dunno and Donut are known to have had at least one vote when they first saw the report. Find the minimum possible total number of votes Dunno and Donut received when they checked the report for the Nth time. It can be assumed that the number of votes received by Dunno and Donut never decreases.

Input
The first line specifies an integer N (1<=N<=1000). The following N lines contain 2 numbers each Pi and Ni  ;(1<=Pi,Ni<=1000). Pi and N are relatively prime numbers. 

Imprint
Output the minimum possible total number of votes. It is guaranteed that the correct answer is no more than 1018.
 

Examples
# Input Output Explanation
1 3
23
1 1
3 2
10 The number of votes received by Donut and Dunno changes as follows: 2.3 → 3.3 → 6.4.
The total number of votes at the end is 10, which is the lowest possible number.
2 4
1 1
1 1
15
1 100
101 It's possible that neither Donut nor Dunno got votes between the time they viewed the report and the next time they viewed it.
3 5
3 10
48 17
31 199
231 23
3 2
6930  

ID 39390. Social distancing I
Темы: Case Study    Conditional operator    Simple puzzles   

A terrible disease afflicts the cows. Farmer John wants to protect them.
The FD Barn is a narrow long building containing N stalls in a row (2≤N≤105). Some of these stalls are already occupied by cows, some are free. After reading about the need for social distancing, the FD wants to maximize D, where D is the distance between the two closest occupied stalls. For example, if stalls 3 and 8 are the closest that are occupied, then D=5.

Two new cows have been added to FD's herd, and he must decide which unoccupied stall to place each of them in. Determine how k he should place these cows so that the result D, becomes the maximum possible. The FD cannot move any existing cows, he can only assign stalls to new cows.

Input
The first line of input contains N. The next line contains a string of length N, consisting of 0 and 1, describing the sequence of stalls in the barn. 0 means an empty stall, 1 means an occupied stall. There are at least two zeros in the string, which is enough to accommodate two cows.
Imprint
Output the largest value of D (the smallest distance between two occupied stalls) that the FD can obtain by adding two new cows in an optimal way.

Examples
# Input Output  
1 14
10001001000010
2 In this example, the FD can add cows like this: 10x010010x0010 where x represents new cows. In this case D=2. It is not possible to mark cows in such a way as to get more D.

ID 44504. frogs
Темы: Case Study   

Three frogs sit on a number line at three different points with integer coordinates ab, c. The frogs are afraid to retreat too far from each other, so only one of the extreme frogs (such that there are no other frogs to the left or right of it) can jump, and only to an integer point between the other two frogs, if there is one. Note that in some positions none of the frogs can jump, let's call them stable .

It is required, given the initial position of the frogs, to determine the minimum and maximum number of jumps that the frogs can make until they get to some stable position.



Input

Three lines contain three different integers - ab, c (1 < = ab, c <= 1018), frog starting positions .

Please note that the input data can be larger than the possible value of a 32-bit integer variable, so you must use 64-bit integer data types (int64 type in Pascal, long long type in C and C++, long type in Java and C#). Python will work correctly with the int type as well.


Output

Print two numbers -  the minimum and maximum number of jumps it takes frogs to reach a stable state.

 

Note

In the first example from the condition, the frog from position 4 can jump to position 2 and form a stable position (1,2,3). It can be shown that they cannot make more than one jump.

In the second test from the condition, the frog from position 1 can jump to position 4, and then the frog from position 10 can jump to position 3, thereby coming to a stable position (2,3,4)  for two jumps. It can be shown that the frogs could not make more 7 jumps according to the described rules.

In the third test, the frogs are already in a stable position, so they won't be able to jump.

In the fourth test from the condition, the frog from position 1 can jump to position 4, and then the frog from position 5 can jump to position 3, thereby arriving at position (2,3,4) in two jump. It can be shown that frogs could not make more than two jumps.

 
Examples
# Input Output
1
1
3
4
1
1
2
1
10
2
2
7
3
1
2
3
0
0
4
2
1
5
2
2