Задача A. Перевод длинных чисел

Автор:А. Кленин
Входной файл: input.txt   Ограничение времени:2 сек
Выходной файл: output.txt   Ограничение памяти:4 Мб

Условие

Дано неотрицательное целое число a, записанное в системе счисления по основанию p. Требуется перевести это число в систему счисления по основанию q. Для представления цифр больше 9 используются заглавные латинские буквы (A — 10, B — 11, …, Z — 35).

Формат входного файла

Первая строка содержит числа p q. Вторая строка содержит строку, представляющую число (a)p.

Формат выходного файла

Выходной файл должен содержать единственную строку, представляющую (a)q без незначащих нулей в начале.

Ограничения

2 ≤ p, q ≤ 36, длина входной строки не превышает 1000 символов.

Примеры тестов

Входной файл (input.txt) Выходной файл (output.txt)
1
2 10
10010
18
2
31 17
AF2J5
6DG3BE

Problem B. IP Networks

Author:NEERC 2005
Input file: ip.in   Time limit:2 sec
Output file: ip.out   Memory limit:64 Mb

Statement

Alex is administrator of IP networks. His clients have a bunch of individual IP addresses and he decided to group all those IP addresses into the smallest possible IP network.

Each IP address is a 4-byte number that is written byte-by-byte in a decimal dot-separated notation "byte0.byte1.byte2.byte3" (quotes are added for clarity). Each byte is written as a decimal number from 0 to 255 (inclusive) without extra leading zeroes.

IP network is described by two 4-byte numbers - network address and network mask. Both network address and network mask are written in the same notation as IP addresses.

In order to understand the meaning of network address and network mask you have to consider their binary representation. Binary representation of IP address, network address, and network mask consists of 32 bits: 8 bits for byte0 (most significant to least significant), followed by 8 bits for byte1, followed by 8 bits for byte2, and followed by 8 bits for byte3.

IP network contains a range of 2n IP addresses where 0 ≤ n ≤ 32. Network mask always has 32 − n first bits set to one, and n last bits set to zero in its binary representation. Network address has arbitrary 32 − n first bits, and n last bits set to zero in its binary representation. IP network contains all IP addresses whose 32 − n first bits are equal to 32 − n first bits of network address with arbitrary n last bits. We say that one IP network is smaller than the other IP network if it contains fewer IP addresses.

For example, IP network with network address 194.85.160.176 and network mask 255.255.255.248 contains 8 IP addresses from 194.85.160.176 to 194.85.160.183 (inclusive).

Input file format

The first line of the input file contains a single integer number m. The following m lines contain IP addresses, one address on a line. Each IP address may appear more than once in the input file.

Output file format

Write to the output file two lines that describe the smallest possible IP network that contains all IP addresses from the input file. Write network address on the first line and network mask on the second. line.

Constraints

1 ≤ m ≤ 1000

Sample tests

No. Input file (ip.in) Output file (ip.out)
1
3
194.85.160.177
194.85.160.183
194.85.160.178
194.85.160.176
255.255.255.248

0.024s 0.005s 9