Problem A. Hexes in viewport

Author:A. Klenin
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:256 Mb

Statement

A game development company has started prototyping a new game, which is played on a field based on a hexagonal grid. The first task is to display a part of the grid in the player's viewport. To speed up the prototyping phase, it was decided to use text-based representation instead of graphics.

The hexagonal grid is composed of nearly perfect hexagons with the side of N characters. In every hexagon, the top and the bottom sides are composed of N "_" characters (ASCII 95), the right-top and the left-bottom sides are composed of N "\" characters (ASCII 92), the left-top and the right-bottom sides are composed of N "/" characters (ASCII 47). All other characters of the field are "." (ASCII 46).

The grid is assumed to be infinite, with the position (0; 0) corresponding to the leftmost character of the top side of a hexagon. The player's viewport is a rectangle displaying some part of the field.

You program must, given the coordinates x, y of the top left corner of the viewport and w, h — the viewport width and height, output the content of the viewport.

Input file format

Input file contains integers N x y w h.

Output file format

Output file must contain h lines of w characters each — the viewport content.

Constraints

1 ≤ N ≤ 100, 0 ≤ x, y ≤ 109, 1 ≤ w, h ≤ 100

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
3 0 2 33 10
....\...../.....\...../.....\....
.....\___/.......\___/.......\___
...../...\......./...\......./...
..../.....\...../.....\...../....
___/.......\___/.......\___/.....
...\......./...\......./...\.....
....\...../.....\...../.....\....
.....\___/.......\___/.......\___
...../...\......./...\......./...
..../.....\...../.....\...../....

Problem B. Elite number

Author:A. Klenin
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:256 Mb

Statement

Let's call an integer number elite if it is divisible by every digit of its decimal representation.

Given an integer x, your program must find the smallest elite number greater than or equal to x.

Input file format

Input file contains the integer x.

Output file format

Output file must contain a single integer — the smallest elite number.

Constraints

1 ≤ x ≤ 1010

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
10
11
2
7777777777
7777777777
3
579916
593595

Problem C. Judging tires

Author:A. Zhuplev
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:256 Mb

Statement

According to ISO stadnard Metric tire code, tire dimensions are described by a string of letters and numbers in the following format:

Rules of ACM (Any Car Modification) race specify a fixed tire dimensions for all participating cars. Young racer Vasya has not been able to find this type of tire. However, his car will still be approved for the race by judges only if radius of the wheel (wheel disk radius plus sidewall height) differs by at most K percent from that of the specified tire.

Note: 1 inch = 25.4 millimeters. Number a differs from b by at most c percent when 100 × |a − b| / b ≤ c.

Input file format

First line of input file contains integer K.

Second and third lines contain Metric tire codes of specified tire and Vasya's tire respectively. Tire codes contain only digits, R and slash (ASCII 47) characters.

Output file format

Output file must contain a single string — APPROVED if Vasya's car is approved to the race or DISAPPROVED otherwise.

Constraints

0 ≤ K ≤ 100

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
2
195/65R15
205/55R15
DISAPPROVED
2
1
195/65R15
185/070R15
APPROVED

Problem D. Easy money

Author:A. Klenin
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:256 Mb

Statement

Young programmer Vasya was not at all interested in car races, but many of his friends were. One day, friends persuaded Vasya to come with them to a sports bar to drink beer and watch some very important race involving N cars — they told Vasya the name of the race, but he immediately forgot it.

At first Vasya was not keen to go, but the perspective of a good beer won him over.

After an hour at the bar, Vasya understood that that the endless images of the cars driving around and around were just as boring with beer as without it. His friends, however, got quite excited and started to place bets on race results. Due to the amount of beer consumed, the bets were wild and some of them did not reflect actual chances at all. In total, M bets were suggested.

Since Vasya was unable to have fun, he decided at least to make some profit. All bets are of the form "I bet A roubles versus B roubles that car p will come ahead of car q". That means betting person agrees to pay A roubles if he was wrong (i.e. q will come ahead of car p), and requires the other side to pay him B roubles if he was right.

By betting on the same cars with different friends, Vasya would in some cases guarantee himself a net win, even if he loses some of the bets. So as not to alienate too many of his friends, Vasya decided to accept only two bets.

Find two bets for Vasya which will guarantee him the largest profit in the worst case.

Input file format

Input file contains integers N M followed by M bet descriptions.

Each bet description contains integers Ai Bi pi qi.

Output file format

Output file must contain a single integer — the maximum amount of guaranteed profit. If there is no way to select two bets to be always profitable, output must contain 0.

Constraints

2 ≤ N, M ≤ 10000;

1 ≤ Ai, Bi ≤ 10000;

1 ≤ pi, qi ≤ N, pi ≠ qi;

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
2 2
100 10 1 2
100 10 2 1
90
2
3 3
200 100 1 2
40 50 2 1
100 1 1 3

0

Problem E. Elementary arithmetic

Author:A. Zhuplev, A. Klenin
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:256 Mb

Statement

Underwater arithmetic is very elementary. There are just two operations: WITH and WITHOUT which mean addition and subtraction respectively.

Underwater mathematicians did not yet invent brackets, so all expressions are calculated from right to left. For example, the expression 3 WITH 5 WITHOUT 4 WITHOUT 7 is calculated as (3 + (5 − (4 − 7))).

Scientists of Nearsea Institute of Underwater Arithmetic need a program to evaluate such expressions.

Input file format

First line of input file contains a single integer N. Following 2 × N + 1 lines describe the expression. Odd lines contain integer operands, even lines contain operations.

Output file format

Output file must contain a single integer — calculation result.

Constraints

1 ≤ N ≤ 103

All operands are in range from 0 to 103.

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
1
2
WITH
3
5
2
3
10
WITHOUT
5
WITH
3
WITH
15
-13

Problem F. How many iterations?

Author:I. Tuphanov
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:256 Mb

Statement

Young programmer Vasya is learning the beginnings of the algorithm complexity theory. For starters, he wants to be able to quickly estimate the number of iterations of nested loops. As a first example, he wrote the following code:

CPascal
long long f(int n)
{
   long long ans = 0;
   for (int i = 1; i <= n; i++) 
      for (int j = i+1; j <= n; j++)
         for (int k = j+1; k <= n; k++)
            ans++;
   return ans;
}
function f(n: integer): int64;
var i, j, k: integer;
begin
   result := 0;
   for i := 1 to n do 
      for j := i+1 to n do 
         for k := j+1 to n do
            inc(result);
end;

Using your knowledge of the subject, help Vasya calculate f(n) for given n.

Input file format

Input file contains an integer n.

Output file format

Output file must contain a single integer — f(n).

Constraints

1 ≤ n ≤ 106

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
1
0
2
1000
166167000

Problem G. Array access

Author:A. Klenin
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:256 Mb

Statement

During the course on compiler construction, students were assigned a task: implement a parser for expressions with array access according to Pascal programming language syntax. Student Vasya has slacked on this task, and implemented only a very small language subset, consisting of a single integer constant 0 and accesses to a single two-dimensional array a. In other words, his "language" has the following grammar:

  expr ::= 0 | a[expr,expr]

After seeing this sad result, a teacher assigned an additional task to Vasya: suppose an array a is defined as a: array [0..N - 1, 0..N - 1] of 0..N. Given N and the values of array elements, generate the shortest possible expression in Vasya's language which will have the value of N.

Input file format

Input file contains an integer N, followed by N2 integers — values aij, in row-by-row order.

Output file format

Output file must contain a single string — an expression in Vasya's language. The expression must exactly correspond to the grammar above. If there is no expression with the value of N, output string IMPOSSIBLE. If there are several shortest expressions, output any of them.

Constraints

1 ≤ N ≤ 22, 0 ≤ aij ≤ N

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
2
1 2
0 0
a[0,a[0,0]]
2
3
2 0 0
0 3 0
0 0 0
IMPOSSIBLE

Problem H. Compression research

Author:A. Klenin
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:256 Mb

Statement

Many compression algorithms are based on finding frequently repeating substrings in the input data. Since it is often impractical to search the whole input for repetitions, only a limited compression window is considered on each step.

While researching a new compression algorithm, young computer scientist Vasya encountered the following problem.

Given input string of N bits, let the compression window be any substring of M bits. Inside each compression window, find the maximum number of occurrences of any substring of length L (L ≤ M).

In the sample input 1 below, compression window length is equal to string length, so there is only a single window. Most frequent substring of length 2 is 01, which occurs 5 times.

Input file format

First line of input file contains integers L M. Second line input file contains a string of length N, each character either 0 or 1.

Output file format

Output file must contain N − M + 1 integers — maximum substring frequencies for each compression window.

Constraints

1 ≤ M ≤ N ≤ 2 × 105, 1 ≤ L ≤ 100

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
2 10
0101010101
5

2
1 3
1110000
3 2 2 3 3 

Problem I. Hype spreading

Author:A. Klenin
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:256 Mb

Statement

One famous car race takes place on a stadium with a racetrack. Spectators watching the race sit on a stand consisting of H rows with W seats each. During the race, some seats are occupied by spectators, and others are empty.

Marketing research demonstrated that if one spectator will loudly shout out a nicely rhymed slogan, spectators on seats located immediately to the left, right, above and below him will repeat it. Their neighbors, in turn, will catch up, and the slogan will spread throughout the stand.

An advertisement company invented a new slogan and wants it to spread among as many spectators as possible. To simulate "spontaneous" appearance of the slogan the company hired an agent who will move to one of the unoccupied seats and shout the slogan from there.

You program must determine the position of the agent which will result in maximum slogan penetration.

Input file format

Input file contains integers W H in the first line, followed by H lines of W characters each.

Each character is either a '.' (ASCII 46) or '#' (ASCII 35), denoting free and occupied seat correspondingly.

There is at least one free seat.

Output file format

Output file must contain two integers — x y, denoting seat number and row number for the agent to take. Rows are numbered starting from 1, top to bottom. Seats are numbered starting from 1, left to right. If there are several optimal solutions, output any of them.

Constraints

2 ≤ W, H ≤ 2000

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
2 2
#.
.#
2 1
2
5 5
..#.#
..###
#..##
#.##.
#....
2 4
3
6 4
######
.....#
..##.#
...#.#

3 2

0.104s 0.007s 25