XXV Interregional Programming Olympiad, Vologda SU, 2023
A. Natasha and Cats
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Natasha has several cats at home. It is known that during the night, one cat drops from $$$A$$$ to $$$B$$$ poorly secured items (Natasha has an infinite number of such items).

One night Natasha heard in her sleep something fall $$$N$$$ times. Determine the minimum number of cats she could have.

Input

Three integers $$$A$$$, $$$B$$$, and $$$N$$$, each on a separate line.

Constraints: $$$0 \le A, B, N \le 10^9$$$, $$$A \le B$$$.

Output

Output a single non-negative integer — the minimum number of cats Natasha has. If there is no solution, output -1.

Examples
Input
2
3
5
Output
2
Input
2
2
3
Output
-1

B. Inequalities
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are 2*N+1 empty cards in a row. Petya writes the sign ">" or "<" on each even card at his discretion. Will Vasya be able to write numbers from 1 to (N+1) on odd cards so that all inequalities are true? Each number must be written exactly once.

Input

The first line contains a natural number N ($$$N\le10^5$$$). The second line contains a sequence of N signs ">" and "<", separated by spaces.

Output

Output a sequence of natural numbers separated by spaces, which solves Vasya's problem if possible, otherwise output -1.

Example
Input
2
< >
Output
1 3 2

C. Regular expression
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Regular expressions are a flexible way to define patterns for searching and replacing text. In this problem, we consider a subset of regular expressions that includes only:

  1. Digits from 0 to 9
  2. Parentheses
  3. The symbol '*', which means any number of repetitions of the character or subexpression in parentheses that precedes it. For example, the expression '345*6' means that after the substring '34' there are zero or more digits '5', followed by the digit '6'. The expression '3(45)*6' means that after the digit '3' there can be zero or more repetitions of the substring '45', followed by the digit '6'.
  4. The symbol '|', which means the 'OR' operation. The priority of this operation is lower than the priority of the implicit concatenation operation. For example, the expression '12|345' means the string '12' or the string '345'. The expression '(12|34)5' means that either '12' or '34' comes first, followed by the digit '5'.

One of the applications of regular expressions is to check whether a string matches a given pattern. For example, to check whether an entered non-negative integer is even, you can write the following expression (leading zeros are allowed for simplicity): (0|1|2|3|4|5|6|7|8|9)*(0|2|4|6|8)

Your task is to solve a more complex problem. You need to determine whether it is possible to rearrange the digits in a non-negative integer so that the number is divisible by 6. For example, in the number 123, the digits can be rearranged in the required way, but in the number 31, they cannot. Write a regular expression to perform this check.

Input

There is no input data.

Output

Output one string of no more than 3000 characters, containing the desired regular expression. The string may only use the characters listed above.

Note

The correctness of your regular expression will be checked on various integers in the range from $$$0$$$ to $$$10^9$$$ inclusive (without leading zeros). The regex_match function from the regex library of the C++ language (GCC compiler) will be used for testing. The testing time should not exceed one second (in case of exceeding, the verdict 'Wrong answer' will be returned).

For information: the first test checks the number 123, the second — 31.

D. Antique Clock
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little girl Kristina came to visit her grandmother and saw antique mechanical clock with two hands on the dial — hour and minute. The clock was working properly, and the movement of the hands of such clocks is continuous (not discrete), that is, for example, in 1 minute the minute hand moves uniformly by 6 degrees, and the hour hand — by 0.5 degrees. At the moment of observation, the clock showed exactly h hours, m minutes, 0 seconds (that is, the minute hand pointed exactly to the mark of m minutes on the dial). For example, the clock in the picture below shows 8 hours 23 minutes and 0 seconds.

Kristina became interested in what is the nearest moment in time when the clock readings will be such that the minimum angle between the hands will be exactly k degrees. But since she is still little, you will have to help her solve this problem.

Input

The input consists of a single line containing three integers h, m, and k ($$$0 \le h \le 11$$$, $$$0 \le m \le 59$$$, $$$0 \le k \le 180$$$).

We will assume that the clock shows time from 0 hours 0 minutes to 11 hours 59 minutes. After 11:59, it is 0:00 again.

Output

Output three integers in a single line — hours, minutes, and seconds of the required moment in time. If necessary, round the time down to the nearest integer number of seconds.

Example
Input
6 30 90
Output
6 49 5

E. Bridge Construction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A certain tropical republic is an archipelago consisting of several (at least two) islands. The map of the archipelago is represented by an $$$n \times m$$$ matrix, where the symbol '1' means land and the digit '0' means water. Any two adjacent cells with the symbol '1' belong to the same island.

The newly elected president of the republic promised to connect all the islands with bridges during the election campaign. Now the promise needs to be fulfilled, but there is not much money in the country. Therefore, he decided to start with the construction of the smallest bridge. Determine the smallest number of cells that need to be changed from '0' to '1' so that any two islands (or more) are connected.

Input

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\le n, m \le 10^6$$$, $$$n \cdot m \le 10^6$$$).

The next $$$n$$$ lines contain $$$m$$$ characters '0' or '1' without spaces. It is guaranteed that there are at least two connected regions of unit cells in the matrix.

Output

Output a single integer — the answer.

Examples
Input
3 3
101
010
101
Output
1
Input
2 3
100
001
Output
2

F. Fragment
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya calculates the sum 2+22+202+2002+...20...02 and wants to see his lucky four-digit number $$$N$$$ among the consecutive digits of the resulting sum. Help Vasya determine the minimum number of summands he needs to take in order to have a fragment in the decimal representation of the sum that coincides with his lucky number $$$N$$$.

Input

An integer $$$N$$$ is entered ($$$1000 \le N \le 9999$$$).

Output

Output the minimum number of summands whose sum will allow Vasya to see a fragment in the form of his lucky number, if possible. Otherwise, output -1.

Examples
Input
2230
Output
5
Input
2023
Output
49005

G. Unusual Calculator
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

At the elective course in electronics, students have developed an unusual calculator. It displays not one number on the screen, but two, and supports three types of commands:

  1. from (m, n) get (m - n, n)
  2. from (m, n) get (m + n, n)
  3. from (m, n) get (n, m)

Determine the sequence of commands to obtain the pair (c, d) from the pair of numbers (a, b).

Input

The first line contains two integers a and b, and the second line contains two integers c and d ($$$-10^5 \le a, b, c, d \le 10^5$$$, $$$a \ne c$$$ or $$$b \ne d$$$).

Output

Output a single line consisting only of digits 1, 2, and 3 without spaces — the command numbers. You do not need to minimize the number of commands, but it should not exceed $$$10^6$$$. During calculations, numbers greater than $$$10^{18}$$$ in absolute value should not be obtained.

If there is no solution, output "IMPOSSIBLE" (without quotes, all uppercase).

Examples
Input
-3 5
3 2
Output
231
Input
3 6
2 3
Output
IMPOSSIBLE

H. Game Case
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are $$$N$$$ cards on the board, each with two integers written on it — one on the left and one on the right. One of the cards has the word "Prize" written on the back.

Two players are playing the game. The host whispers the left number on the prize card to the first player and the right number to the second player. The players are not allowed to explicitly tell each other their numbers, but they can give hints. If both players guess the prize card, they will share the prize between them. The less obvious the hints are, the more valuable the prize will be.

Once, during one of the games, the following dialogue took place between the players:

  • Player 1: I know that you don't know the prize card. And I don't know it either.
  • Player 2: Thank you, now I know the prize card.
  • Player 1: Thank you, now I know it too.

After this, one of the spectators in the audience exclaimed, "I know it too!" Can you determine the prize card?

Input

The first line of the input contains a positive integer $$$N$$$ — the number of cards ($$$N \le 100$$$). Each of the following $$$N$$$ lines contains a description of a card — two integers from 1 to 100 separated by a space, first the left one, then the right one. There are no repeated pairs. It is guaranteed that the host, players, and spectator tell the truth and do not make mistakes.

Output

Output two numbers on the prize card — first the left one, then a space, then the right one.

Example
Input
9
1 2
1 3
1 4
1 5
6 3
6 7
8 7
8 4
8 5
Output
6 3

I. Cutting a Chain
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Perhaps you are familiar with the following well-known problem. A traveler rented a room in a hotel for a week and offered the owner a chain of seven silver rings as payment — one ring per day, with the condition, however, that payment would be made daily. What is the minimum number of rings in the chain that he will have to cut?

The solution to this problem is as follows. It turns out that it is sufficient to cut only one ring — the third one. Then the traveler will have three pieces of chain — consisting of two, one, and four rings. On the first day, he will give the owner one ring. On the second day, he will give the piece consisting of two rings and take back the ring he gave on the first day, and so on.

You are asked to solve this problem in general. Let the chain consist of $$$N$$$ rings, and the traveler wants to stay in the hotel for $$$N$$$ days. Determine the minimum number of cuts required, as well as which rings to cut.

Input

One integer $$$N$$$ is entered ($$$1 \le N \le 10^9$$$).

Output

In the first line, output an integer $$$K$$$ — the minimum number of cuts. In the next line, output $$$K$$$ distinct integers from 1 to $$$N$$$ — the numbers of the rings that need to be cut. If there are multiple correct answers, output any one.

Examples
Input
7
Output
1
3 
Input
8
Output
2
2 4

J. Refactoring
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given the source code of a program in several popular programming languages. This program reads two integers $$$a$$$ and $$$n$$$ and then outputs one integer — the answer.

With large input numbers, this program can take a long time to run. Your task is to write an alternative program that will produce exactly the same answers, but will run for no more than one second for any input $$$a$$$ and $$$n$$$ in the range from $$$0$$$ to $$$10^{18}$$$.

Text in C++:

#include <iostream>

int main() {
long long a, n;
std::cin >> a >> n;
long long b = 0;
for (long long i = 0; i < n; i++) {
b = (b - a) & a;
}
std::cout << b << std::endl;
}
Text in Python:
a = int(input())
n = int(input())
b = 0
for _ in range(n):
b = (b - a) & a
print(b)

Text in Java:

import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long a = in.nextLong();
long n = in.nextLong();
long b = 0;
for (long i = 0; i < n; i++) {
b = (b - a) & a;
}
System.out.println(b);
}
}

Text in C#:

using System;

public class Solution {
static void Main() {
long a = long.Parse(Console.ReadLine());
long n = long.Parse(Console.ReadLine());
long b = 0;
for (long i = 0; i < n; i++) {
b = (b - a) & a;
}
Console.WriteLine(b);
}
}

Text in Pascal:

var
a, b, n, i: int64;
begin
read(a, n);
b := 0;
i := 0;
while i < n do begin
b := (b - a) and a;
inc(i);
end;
writeln(b);
end.
Input

The first line of input contains an integer $$$a$$$, the second line contains an integer $$$n$$$ ($$$0 \le a, n \le 10^{18}$$$).

Output

Output one integer — the answer.

Examples
Input
3
2
Output
2
Input
5
6
Output
4
Note

It is assumed that negative integers in memory are represented in two's complement as in most popular processor architectures (including x86-64 architecture). In two's complement, the negative number $$$-x$$$ is stored in memory as the complement to $$$2^{64}$$$ of $$$x$$$.

Note that in Python, integers are not limited by a fixed number of bits. But this does not affect the answer, so you can assume that the statement above is true for Python as well.