# Monthly Archives: September 2010

## SGU 118 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=118

## 118. Digital Root

time limit per test: 0.50 sec.
memory limit per test: 4096 KB

Let f(n) be a sum of digits for positive integer n. If f(n) is one-digit 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*…*AN-1 + … + A1*A2 + A1.

### Input

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 non-negative and not more than 109.

### Output

Write one line for every test case. On each line write digital root for given expression.

## SGU 133 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=133

## 133. Border

time limit per test: 0.50 sec.
memory limit per test: 4096 KB

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.

### Input

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.

### Output

You should print the number of redundant outposts.

## SGU 130 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=130

## 130. Circle

time limit per test: 0.50 sec.
memory limit per test: 4096 KB

On a circle border there are 2k different points A1, A2, ..., A2k, located contiguously. These points connect k chords so that each of points A1, A2, ..., A2k is the end point of one chord. Chords divide the circle into parts. You have to find N - the number of different ways to connect the points so that the circle is broken into minimal possible amount of parts P.

### Input

The first line contains the integer k (1 <= k <= 30).

### Output

The first line should contain two numbers N and P delimited by space.

## SGU 108 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=108

## 108. Self-numbers 2

time limit per test: 2.50 sec.
memory limit per test: 4096 KB

In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)), d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence 33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ... The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. Let the a[i] will be i-th self-number. There are thirteen self-numbers a..a less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97. (the first self-number is a=1, the second is a = 3, :, the thirteen is a=97);

1949年,印度数学家D.R. Kaprekar 发现了一类数，称为自我数.对于任何一个正整数 n ,定义 d(n) 的值为 n 与 n的每一位数字之和.(d 叫做digitadition, 这个单词是Kaprekar创造的.) 例如,d(75) = 75 + 7 + 5 = 87. 给你任意正整数 n 作为起点，您可以构建无限增加的整数 n 序列,n, d(n), d(d(n)), d(d(d(n))),.... 例如,起点是33,则下一个数是 33 + 3 + 3 = 39,再下一个数是 39 + 3 + 9 = 51,然后是 51 + 5 + 1 = 57,这样由 33 生成的数列是 33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141,... 这样我们称正整数 n 是 d(n)生成器.在刚才的序列中, 33 是 39 的生成器, 39 是 51 的生成器, 51 是 57 的生成器,等等.有些数有不止一个生成器,例如, 101 有两个生成器, 91 和 100.如果一个数没有生成器我们就称它为自我数.用 a[i] 表示第 i 个自我数.小于 100 有 13 个自我数:1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, 97.(第一个自我数是 a = 1,第二个是 a = 3,:,第十三个是 a = 97);

### Input

Input contains integer numbers N, K, s1...sk. (1<=N<=107, 1<=K<=5000) delimited by spaces and line breaks.

### Output

At first line you must output one number - the quantity of self-numbers in interval [1..N]. Second line must contain K numbers - a[s1]..a[sk], delimited by spaces. It`s a gaurantee, that all self-numbers a[s1]..a[sk] are in interval [1..N]. (for example if N = 100, sk can be 1..13 and cannot be 14, because 14-th self-number a = 108, 108 > 100)

## SGU 117 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=117

## 117. Counting

time limit per test: 0.50 sec.
memory limit per test: 4096 KB

Find amount of numbers for given sequence of integer numbers such that after raising them to the M-th power they will be divided by K.

### Input

Input consists of two lines. There are three integer numbers N, M, K (0

## SGU 101 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=101

## 101. Domino

time limit per test: 0.50 sec.
memory limit per test: 4096 KB

Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men,stones, or even cards.
The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice...

The principle in nearly all modern dominoes games is to match one end of a piece to another that is identically or reciprocally numbered.

ENCYCLOPÆDIA BRITANNICA

–––––– 《大英百科全书》

Given a set of domino pieces where each side is marked with two digits from 0 to 6. Your task is to arrange pieces in a line such way, that they touch through equal marked sides. It is possible to rotate pieces changing left and right side.

### Input

The first line of the input contains a single integer N (1 ≤ N ≤ 100) representing the total number of pieces in the domino set. The following N lines describe pieces. Each piece is represented on a separate line in a form of two digits from 0 to 6 separated by a space.

### Output

Write “No solution” if it is impossible to arrange them described way. If it is possible, write any of way. Pieces must be written in left-to-right order. Every of N lines must contains number of current domino piece and sign “+” or “-“ (first means that you not rotate that piece, and second if you rotate it).

## SGU 126 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=126

## 126. Boxes

time limit per test: 0.50 sec.
memory limit per test: 4096 KB

There are two boxes. There are A balls in the first box, and B balls in the second box (0 < A + B < 2147483648). It is possible to move balls from one box to another. From one box into another one should move as many balls as the other box already contains. You have to determine, whether it is possible to move all balls into one box.

### Input

The first line contains two integers A and B, delimited by space.

### Output

First line should contain the number N - the number of moves which are required to move all balls into one box, or -1 if it is impossible.