月度存档: 九月 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.

设 f(n) 表示十进制正整数 n 的各位数字之和。如果 f(n) 是一个1位数那么他就是 n 的数根。否则的话 f(n) 的数根就是 n 的数根。举例说明:987的数根是 6(9+8+7=24 2+4=6)。你的任务是算出这样的数的数根: 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.

输入包含K个测试点.在第一行会给出 K (1 <= K <= 5).每个测试点一行.首先是一个正整数N (N <= 1000). 接着N个非负整数 (序列 A). 均不超过109.

Output

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

每个测试点一行,输出给定数的数根.

[……]

Continue

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.

在 A 和 B 的边界出有 N 个前哨站.对于每一个前哨站 k,他的管辖范围 [Ak,Bk] 是已知的.由于经济原因,A 国总统决定减少一些前哨站.多余的前哨站都会被抛弃.我们定义 i 号前哨站是多余的如果存在另一个前哨战 j 满足 Aj < Ai 且 Bi < Bj.你的任务是计算多余的前哨站个数.

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.

第一行是整数N (1 <=N <= 16 000).接下来N行每行包含2个整数: Ak 和 Bk (0 <= Ak < Bk <= 2 000 000 000),空格隔开.所有的 Ak 都是不同的,所有的 Bk 也是不同的.

Output

You should print the number of redundant outposts.

输出多余的前哨站个数.

[……]

Continue

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.

在圆上有 2k 个不重合的点 A1, A2, …, A2k, 通过这些点可以作k条弦,使得每个点都恰好属于一条弦。这样我们就把这个圆分成了很多区域,你要求出 N 个方案数,使这些弦将圆划分的区域数量最小。

Input

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

输入 K.

Output

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

输出方案数量和最少的区域数量,用空格隔开。

[……]

Continue

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[1]..a[13] less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97. (the first self-number is a[1]=1, the second is a[2] = 3, :, the thirteen is a[13]=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] = 1,第二个是 a[2] = 3,:,第十三个是 a[13] = 97);

Input

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

输入数据包含整数 N, K, s1…sk. (1<=N<=107, 1<=K<=5000) 用空格或回车隔开。

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[14] = 108, 108 > 100)

第一行输出 [1..N]范围内自我数的数量.第二行包含 K 个数 – a[s1]..a[sk], 用空格隔开.必须满足所有自我数 a[s1]..a[sk] 是在[1..N]范围内.(例如,如果 N =100,sk可以是 1..13 但不能是 14,因为 第 14 个自我数 a[14] = 108 , 108 > 100 )

[……]

Continue

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.

给出一系列数,统计出其中 M 次幂能被 K 整除的个数.

Input

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

输入包含两行.第一行有三个整数 N, M, K (0

Output

Write answer for given task.

输出结果.

[……]

Continue

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.

给定一系列两端标记为0~6数字的骨牌.你的任务是排列这些骨牌,使他们相接触的部分数字相同,你可以左右翻转骨牌.

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.

第一行输入一个整数 N (1 ≤ N ≤ 100) 代表骨牌的数量. 接下来 N 行代表 N 块骨牌. 每块骨牌使用两个0~6的使用空格隔开的数字表示.

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).

如果无法将骨牌连成串,则输出”No solution”. 否则输出任何一种符合条件的排列方案.输出是从左到右描述骨牌的排列.每行包含一个数字和一个符号,分别表示骨牌的编号和是否翻转骨牌,(+表示不翻转,-表示翻转)。

[……]

Continue

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.

现在有两个盒子。第一个盒子有A个球,另一个盒子有B个球 (0 < A + B < 2147483648).有一种移动球的方式.从一个盒子移动球到另一个盒子,但要求被移动的球的数量要与另一个盒子已有的球的数量相同.你必须算出是否有可能将其中一个盒子清空。

Input

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

第一行包含两个整数 A 和 B,以空格隔开.

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.

第一行输出整数 N -如果可以清空其中一个盒子所需要的移动次数,如果不可能,则输出-1.

[……]

Continue

SGU 175 解题报告

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

175.Encoding

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

Let phi(W) is the result of encoding for algorithm:
1.If the length of W is 1 then phi(W) is W;
2.Let coded word is W = w1w2…wN and K = N / 2 (rounded down);
3.phi(W) = phi(wNwN-1…wK+1) + phi(wKwK-1…w1).
For example, phi(‘Ok’) = ‘kO’, phi(‘abcd’) = ‘cdab’.
Your task is to find position of letter wq in encoded word phi(W).

令phi(W)为字符串W按以下方法编码的结果:
1、如果W长度为1,那么phi(W)就是W
2、设W=w1 w2 …… wN 且 K=N/2 (向下取整)
3、phi(W)=phi(wN wN-1 …… wK+1)+phi(wK wK-1 …… w1)
例如,phi(‘Ok’)=’kO’,phi(‘abcd’)=’cdab’。
你的任务就是找到原字符串W内第q个字符wq在编码后的字符串phi(W)中的位置。

Input

Given integers N, q (1 <= N <= 10^9; 1<= q <= N), where N is the length of word W.

整数N,q(1<=N<=10^9;1<=q<=N),其中N是字符串W的长度

Output

Write position of letter wq in encoded word phi(W).

输出wq在编码后的字符串phi(W)中的位置

[……]

Continue

SGU 104 解题报告

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

104. Little shop of flowers

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

You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The vases are glued onto the shelf and are numbered consecutively 1 through V, where V is the number of vases, from left to right so that the vase 1 is the leftmost, and the vase V is the rightmost vase. The bunches are moveable and are uniquely identified by integers between 1 and F. These id-numbers have a significance: They determine the required order of appearance of the flower bunches in the row of vases so that the bunch i must be in a vase to the left of the vase containing bunch j whenever i < j. Suppose, for example, you have bunch of azaleas (id-number=1), a bunch of begonias (id-number=2) and a bunch of carnations (id-number=3). Now, all the bunches must be put into the vases keeping their id-numbers in order. The bunch of azaleas must be in a vase to the left of begonias, and the bunch of begonias must be in a vase to the left of carnations. If there are more vases than bunches of flowers then the excess will be left empty. A vase can hold only one bunch of flowers.

你现在想用最令人满意的方案来装饰你花店的橱窗。你有 F 束花, 每束花种类不同, 种数不超过窗台上的花瓶总数。花瓶被嵌入窗台,并且被顺序从1到 V 编号。V 表示花瓶总数。顺序编号使得1号花瓶在最左边, V 号花瓶在最右边。花束是可以移动的,而且按照不同种类从1 到 F 编号。这些编号是有特殊含义的: 编号小的花必须出现在编号大的花的左边。现在,所有的花必须按编号顺序放在花瓶中,每个花瓶对应一种花。杜鹃花必须在秋海棠左面,秋海棠必须在康乃馨前面。如果花不够,将会有花瓶是空的.

Each vase has a distinct characteristic (just like flowers do). Hence, putting a bunch of flowers in a vase results in a certain aesthetic value, expressed by an integer. The aesthetic values are presented in a table as shown below. Leaving a vase empty has an aesthetic value of 0.

每个花瓶都有自己的特征(就像花一样)。于是,把一种花放入一个花瓶会产生一定的美学价值。用一个整数来表示美学价值,那么空花瓶的美学价值为0,花与花瓶搭配的美学价值如下表:

V A S E S 花瓶

1

2

3

4

5

Bunches 花束

1 (azaleas 杜鹃花)

7

23

-5

-24

16

2 (begonias 秋海棠)

5

21

-4

10

23

3 (carnations 康乃馨)

-21

5

-4

-20

20

According to the table, azaleas, for example, would look great in vase 2, but they would look awful in vase 4.

根据这个表,杜鹃花放在2号花瓶中会好看些,在4号花瓶中现得很别扭。

To achieve the most pleasant effect you have to maximize the sum of aesthetic values for the arrangement while keeping the required ordering of the flowers. If more than one arrangement has the maximal sum value, any one of them will be acceptable. You have to produce exactly one arrangement.

为了让方案最令人满意,你需要使美学价值最大化。如果有多种方案美学价值相同,选取任意一种都算对。你只需要算出一种方案。

ASSUMPTIONS

  • 1 ≤ F ≤ 100 where F is the number of the bunches of flowers. The bunches are numbered 1 through F.
  • FV ≤ 100 where V is the number of vases.
  • -50 Aij 50 where Aij is the aesthetic value obtained by putting the flower bunch i into the vase j.
  • 1 ≤ F ≤ 100 表示花的种数。
  • FV ≤ 100 表示花瓶数。
  • -50 Aij 50 表示i号花束放入j号花瓶产生的美学价值。

Input

  • The first line contains two numbers:F, V.
  • The following F lines: Each of these lines contains V integers,so that Aij is given as thej th number on the (j+1) st line of the input file.
  • 第一行: F, V.
  • 接下来F行每行V个数,其中Aij第(i+1) 行j 列 。

Output

  • The first line will contain the sum of aesthetic values for your arrangement.
  • The second line must present the arrangement as a list of F numbers, so that the k’th number on this line identifies the vase in which the bunch k is put.
  • 第一行包括一个整数表示最大美学价值。
  • 第二行从左到右F个数表示最优方案的每个花束放在哪一个花瓶里,数据用空格隔开。

[……]

Continue

SGU 184 解题报告

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

184.Patties

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

Petya is well-known with his famous cabbage patties. Petya’s birthday will come very soon, and he wants to invite as many guests as possible. But the boy wants everybody to try his specialty of the house. That’s why he needs to know the number of the patties he can cook using the stocked ingredients. Petya has P grams of flour, M milliliters of milk and C grams of cabbage. He has plenty of other ingredients. Petya knows that he needs K grams of flour, R milliliters of milk and V grams of cabbage to cook one patty. Please, help Petya calculate the maximum number of patties he can cook.

Input

The input file contains integer numbers P, M, C, K, R and V, separated by spaces and/or line breaks (1 <= P, M, C, K, R, V <= 10000).

Output

Output the maximum number of patties Petya can cook.

[……]

Continue