poj2992

maksyuki 发表于 oj 分类,标签:
0

Divisors

Your task in this problem is to determine the number of divisors of Cnk. Just for fun -- or do you need any special reason for such a useful computation?

Input

The input consists of several instances. Each instance consists of a single line containing two integers n and k (0 ≤ k ≤ n ≤ 431), separated by a single space.

Output

For each instance, output a line containing exactly one integer -- the number of distinct divisors of Cnk. For the input instances, this number does not exceed 263 - 1.

Sample Input

5 1

6 3

10 4

Sample Output

2

6

16

Source

CTU Open 2005

 

题目类型:阶乘素因子分解

算法分析:将440以内的阶乘的素因子求出来,使用f[i][j]表示阶乘i的素因子j的个数有多少个,然后对于每个n和k,计算f[n][i] - f[n-k][i] - f[k][i]表示组合(n,k)中素因子为i的指数个数,然后直接累乘起来

 

poj2955

maksyuki 发表于 oj 分类,标签:
0

Brackets

We give the following inductive definition of a “regular brackets” sequence:

  • the empty sequence is a regular brackets sequence,
  • if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
  • if a and b are regular brackets sequences, then ab is a regular brackets sequence.
  • no other sequence is a regular brackets sequence

For instance, all of the following character sequences are regular brackets sequences:

(), [], (()), ()[], ()[()]

while the following character sequences are not:

(, ], )(, ([)], ([(]

Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest msuch that for indices i1i2, …, im where 1 ≤ i1 < i2 < … < im ≤ nai1ai2 … aim is a regular brackets sequence.

Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].

Input

The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (, ), [, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.

Output

For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

Sample Input

((()))

()()()

([]])

)[)(

([][][)

end

Sample Output

6

6

4

0

6

Source

Stanford Local 2004

 

题目类型:区间DP

算法分析:使用数组dp[i][j]表示下标从ai~aj中匹配的括号的个数,然后对于每一个ak(i <= k <= j),如果ai和ak是匹配的,则dp[i][j] = max (dp[i][j], dp[i+1][k-1] + dp[k+1][j] + 2)

否则dp[i][j] = max (dp[i][j], dp[i+1][j]),初始时dp[i][i] = 0,最后输出dp[0][len-1],其中len表示字符串的长度

 

poj2891

maksyuki 发表于 oj 分类,标签:
0

Strange Way to Express Integers

Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:

Choose k different positive integers a1a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) can be used to express m.

“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”

Since Elina is new to programming, this problem is too difficult for her. Can you help her?

Input

The input contains multiple test cases. Each test cases consists of some lines.

  • Line 1: Contains the integer k.
  • Lines 2 ~ k + 1: Each contains a pair of integers airi (1 ≤ i ≤ k).

Output

Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.

Sample Input

2

8 7

11 9

Sample Output

31

Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

Source

POJ Monthly--2006.07.30, Static

 

题目类型:线性同余方程组

算法分析:直接使用线性同余方程组求解的方法计算即可

 

poj2823

maksyuki 发表于 oj 分类,标签:
0

Sliding Window

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and k is 3.

Window position Minimum value Maximum value
[1  3  -1] -3  5  3  6  7 -1 3
 1 [3  -1  -3] 5  3  6  7 -3 3
 1  3 [-1  -3  5] 3  6  7 -3 5
 1  3  -1 [-3  5  3] 6  7 -3 5
 1  3  -1  -3 [5  3  6] 7 3 6
 1  3  -1  -3  5 [3  6  7] 3 7

Your task is to determine the maximum and minimum values in the sliding window at each position.

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.

Sample Input

8 3

1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3

3 3 5 5 6 7

Source

POJ Monthly--2006.04.28, Ikki

 

题目类型:单调队列

算法分析:这是一道使用单调队列解决的经典问题。维护一个严格单调增和一个严格单调减队列,重点是判断队头元素何时出队列,方法是向队列中添加元素的同时将元素的下标也添加进去,之后直接比较下标之间的关系即可

 

poj2785

maksyuki 发表于 oj 分类,标签:
0

4 Values whose Sum is 0

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

Input

The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 228 ) that belong respectively to A, B, C and D .

Output

For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input

6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45

Sample Output

5

Hint

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).

Source

Southwestern Europe 2005

 

题目类型:数字型Hash查找

算法分析:直接使用暴力枚举是一定会TLE的,这里使用的Hash可以将时间复杂度降到O(2*N^2),将输入的前两列数字枚举和并插入到Hash表中,然后对于输入的后两列数字枚举和并查询和的相反数是否在表中,如果在则累加sum,最后输出sum即可

 

poj2777

maksyuki 发表于 oj 分类,标签:
0

Count Color

Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:

1. "C A B C" Color the board from segment A to segment B with color C.
2. "P A B" Output the number of different colors painted between segment A and segment B (including).

In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.

Input

First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

Output

Ouput results of the output operation in order, each line contains a number.

Sample Input

2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2

Sample Output

2

1

Source

POJ Monthly--2006.03.26,dodo

 

题目类型:线段树(区间染色)

算法分析:这是一个不覆盖的区间染色问题,需要使用Lazy-Tag标记,使用lazy数组表示当前区间是否已经完全被覆盖。由于颜色数量比较小,所以可以使用位运算来压缩空间

 

poj2752

maksyuki 发表于 oj 分类,标签:
0

Seek the Name, Seek the Fame

The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:

Step1. Connect the father's name and the mother's name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).

Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)

Input

The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.

Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.

Output

For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.

Sample Input

ababcababababcabab

aaaaa

Sample Output

2 4 9 18

1 2 3 4 5

Source

POJ Monthly--2006.01.22,Zeyuan Zhu

 

题目类型:KMP中Next数组的应用

算法分析:直接将读入字符串的Next数组求出,然后从后向前将不是0的Next[len]的值记录下来,然后对所有的记录排序并输出

 

poj2709

maksyuki 发表于 oj 分类,标签:
0

Painter

The local toy store sells small fingerpainting kits with between three and twelve 50ml bottles of paint, each a different color. The paints are bright and fun to work with, and have the useful property that if you mix X ml each of any three different colors, you get X ml of gray. (The paints are thick and "airy", almost like cake frosting, and when you mix them together the volume doesn't increase, the paint just gets more dense.) None of the individual colors are gray; the only way to get gray is by mixing exactly three distinct colors, but it doesn't matter which three. Your friend Emily is an elementary school teacher and every Friday she does a fingerpainting project with her class. Given the number of different colors needed, the amount of each color, and the amount of gray, your job is to calculate the number of kits needed for her class.

Input

The input consists of one or more test cases, followed by a line containing only zero that signals the end of the input. Each test case consists of a single line of five or more integers, which are separated by a space. The first integer N is the number of different colors (3 <= N <= 12). Following that are N different nonnegative integers, each at most 1,000, that specify the amount of each color needed. Last is a nonnegative integer G <= 1,000 that specifies the amount of gray needed. All quantities are in ml.

Output

For each test case, output the smallest number of fingerpainting kits sufficient to provide the required amounts of all the colors and gray. Note that all grays are considered equal, so in order to find the minimum number of kits for a test case you may need to make grays using different combinations of three distinct colors.

Sample Input

3 40 95 21 0
7 25 60 400 250 0 60 0 500
4 90 95 75 95 10
4 90 95 75 95 11
5 0 0 0 0 0 333
0

Sample Output

2

8

2

3

4

Source

Mid-Central USA 2005

 

题目类型:贪心

算法分析:首先将读入的颜料按照体积的大小递增排序,然后得到初始的颜料套装数,并计算出使用初始套装后,每种颜料还剩的体积。接着每次取体积最大的三种颜料1毫升配成灰色染料,直到灰色染料已经配完或者是再使用一个颜料套装。注意这里只能每次枚举1毫升,否则会WA

 

poj2689

maksyuki 发表于 oj 分类,标签:
0

Prime Distance

The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality 阅读全文 »

poj2585

maksyuki 发表于 oj 分类,标签:
0

Window Pains

Boudreaux likes to multitask, especially when it comes to using his computer. Never satisfied with just running one application at a time, he usually runs nine applications, each in its own window. Due to limited screen real estate, he overlaps these windows and brings whatever window he currently needs to work with to the foreground. If his screen were a 4 x 4 grid of squares, each of Boudreaux's windows would be represented by the following 2 x 2 windows:

1 1 . .
1 1 . .
. . . .
. . . .
. 2 2 .
. 2 2 .
. . . .
. . . .
. . 3 3
. . 3 3
. . . .
. . . .
. . . .
4 4 . .
4 4 . .
. . . .
. . . .
. 5 5 .
. 5 5 .
. . . .
. . . .
. . 6 6
. . 6 6
. . . .
. . . .
. . . .
7 7 . .
7 7 . .
. . . .
. . . .
. 8 8 .
. 8 8 .
. . . .
. . . .
. . 9 9
. . 9 9

When Boudreaux brings a window to the foreground, all of its squares come to the top, overlapping any squares it shares with other windows. For example, if window 1and then window 2were brought to the foreground, the resulting representation would be:

1 2 2 ?
1 2 2 ?
? ? ? ?
? ? ? ?
If window 4 were then brought to the foreground:
1 2 2 ?
4 4 2 ?
4 4 ? ?
? ? ? ?

. . . and so on . . .
Unfortunately, Boudreaux's computer is very unreliable and crashes often. He could easily tell if a crash occurred by looking at the windows and seeing a graphical representation that should not occur if windows were being brought to the foreground correctly. And this is where you come in . . .

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

A single data set has 3 components:

  1. Start line - A single line:
    START
  2. Screen Shot - Four lines that represent the current graphical representation of the windows on Boudreaux's screen. Each position in this 4 x 4 matrix will represent the current piece of window showing in each square. To make input easier, the list of numbers on each line will be delimited by a single space.
  3. End line - A single line:
    END

After the last data set, there will be a single line:
ENDOFINPUT

Note that each piece of visible window will appear only in screen areas where the window could appear when brought to the front. For instance, a 1 can only appear in the top left quadrant.

Output

For each data set, there will be exactly one line of output. If there exists a sequence of bringing windows to the foreground that would result in the graphical representation of the windows on Boudreaux's screen, the output will be a single line with the statement:

THESE WINDOWS ARE CLEAN

Otherwise, the output will be a single line with the statement:
THESE WINDOWS ARE BROKEN

Sample Input

START
1 2 3 3
4 5 6 6
7 8 9 9
7 8 9 9
END
START
1 1 3 3
4 1 3 3
7 7 9 9
7 7 9 9
END
ENDOFINPUT

Sample Output

THESE WINDOWS ARE CLEAN
THESE WINDOWS ARE BROKEN

Source

South Central USA 2003

 

题目类型:拓扑排序

算法分析:由于输入中的窗口是最靠外面的,且题目中说调动了所有的窗口。这就意味着最外面的窗口中的数字覆盖了其他在该位置处的数字,使用这种覆盖关系构造有向图。首先构造9个窗口,然后对于输入的窗口中的每一个位置处的数字a遍历所有的9个窗口在该位置的取值vi,如果在该处存在值vi且不与a相等,则构造一条有向边,起点为a,终点为vi。最后对于构造好的有向图使用拓扑排序即可

 

poj2576

maksyuki 发表于 oj 分类,标签:
0

Tug of War

A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must not differ by more than 1; the total weight of the people on each team should be as nearly equal as possible.

Input

The first line of input contains n the number of people at the picnic. n lines follow. The first line gives the weight of person 1; the second the weight of person 2; and so on. Each weight is an integer between 1 and 450. There are at most 100 people at the picnic.

Output

Your output will be a single line containing 2 numbers: the total weight of the people on one team, and the total weight of the people on the other team. If these numbers differ, give the lesser first.

Sample Input

3

100

90

200

Sample Output

190 200

Source

Waterloo local 2000.09.30

 

题目类型:二维01背包

算法分析:每一个人都可以看做是一个物品,然后对于每一个物品,有一个重量的限制(总重量的一半)和一个人数的限制(总人数的一半),可以直接使用二维01背包求解

 

poj2570

maksyuki 发表于 oj 分类,标签:
0

Fiber Network

Several startup companies have decided to build a better Internet, called the "FiberNet". They have already installed many nodes that act as routers all around the world. Unfortunately, they started to quarrel about the connecting lines, and ended up with every company laying its own set of cables between some of the nodes. Now, service providers, who want to send data from node A to node B are curious, which company is able to provide the necessary connections. Help the providers by answering their queries.

Input

The input contains several test cases. Each test case starts with the number of nodes of the network n. Input is terminated by n=0. Otherwise, 1<=n<=200. Nodes have the numbers 1, ..., n. Then follows a list of connections. Every connection starts with two numbers A, B. The list of connections is terminated by A=B=0. Otherwise, 1<=A,B<=n, and they denote the start and the endpoint of the unidirectional connection, respectively. For every connection, the two nodes are followed by the companies that have a connection from node A to node B. A company is identified by a lower-case letter. The set of companies having a connection is just a word composed of lower-case letters.
After the list of connections, each test case is completed by a list of queries. Each query consists of two numbers A, B. The list (and with it the test case) is terminated by A=B=0. Otherwise, 1<=A,B<=n, and they denote the start and the endpoint of the query. You may assume that no connection and no query contains identical start and end nodes.

Output

For each query in every test case generate a line containing the identifiers of all the companies, that can route data packages on their own connections from the start node to the end node of the query. If there are no companies, output "-" instead. Output a blank line after each test case.

Sample Input

3
1 2 abc
2 3 ad
1 3 b
3 1 de
0 0
1 3
2 1
3 2
0 0
2
1 2 z
0 0
1 2
2 1
0 0
0

Sample Output

ab

d

-

 

z-

Source

Ulm Local 2001

 

题目类型:全源最短路思想(Floyd)

算法分析:使用Floyd算法的迭代思想,矩阵ans[i][j]中存放的是节点i到节点j中存在的公司集合(字符串)。使用位运算的相关技巧,用从低到高的26个二进制位表示两点之间存在的公司(1位)的情况。对于每一次迭代,使用ans[i][j] = 集合或运算(ans[i][j], ans[i][k] 集合与运算ans[k][j])。然后对于每个查询直接输出结果即可

 

poj2559

maksyuki 发表于 oj 分类,标签:
0

Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles: Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.

Input

The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that1<=n<=100000. Then follow n integers h1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.

Output

For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

Sample Input

7 2 1 4 5 1 3 3

4 1000 1000 1000 1000

0

Sample Output

8

4000

Hint

Huge input, scanf is recommended.

Source

Ulm Local 2003

 

题目类型:单调队列

算法分析:一道经典的使用单调队列思想解的题。坐标从小到大的分析,如果存在一个高度不超过先前的高度,则应该将先前高度的矩形块的面积出栈并计算,更新最大值。否则就一直入栈。最后将还在栈中的矩形块的面积计算出来并更新最大值

 

poj2528

maksyuki 发表于 oj 分类,标签:
0

Mayor's posters

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:

  • Every candidate can place exactly one poster on the wall.
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
  • The wall is divided into segments and the width of each segment is one byte.
  • Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections. Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed.

The picture below illustrates the case of the sample input.

Sample Input

1

5

1 4

2 6

8 10

3 4

7 10

Sample Output

4

Source

Alberta Collegiate Programming Contest 2003.10.18

 

题目类型:离散化+线段树(区间染色)

算法分析:这是一道会发生覆盖的区间染色问题,由于输入数据范围比较大,所以需要使用离散化操作。然后使用Lazy-Tag标记,lazy数组表示区间被覆盖到的海报的标号。最后累加整个离散化后的区间中存在的不同的海报的标号的个数即可

 

poj2513

maksyuki 发表于 oj 分类,标签:
0

Colored Sticks

You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

Input 阅读全文 »

poj2480

maksyuki 发表于 oj 分类,标签:
0

Longge's problem

Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N.

"Oh, I know, I know!" Longge shouts! But do you know? Please solve it.

Input

Input contain several test case.
A number N per line.

Output

For each N, output ,∑gcd(i, N) 1<=i <=N, a line

Sample Input

2

6

Sample Output

3

15

Source

POJ Contest,Author:Mathematica@ZSU

 

题目类型:素因子分解+积性函数

算法分析:由于n比较大,直接使用sigma (i * Euler(n/i)), i |n会TLE,此时应该进行进一步的推导。简单来说就是可以将积性函数f(x)=x和g(x)=Euler(x)进行一次狄利克雷卷积。而积性函数的狄利克雷卷积仍旧是积性函数。记卷积的结果是G(n),由积性函数的性质可得:G(n) = G(p1 ^ a1) * G(p2 ^ a2) * G(p3 ^ a3) * ……G(pn ^ an)。将公式进行化简最后可得G(n) =

[(a1+1) * p1 ^ a1 - a1 * p1 ^ (a1-1)] * [(a2+1) * p2 ^ a2 - a2 * p2 ^ (a2-1)] * [(a3+1) * p3 ^ a3 – a3 * p3 ^ (a3-1)] * …… * [(an+1) * pn ^ an - an * pn ^ (an-1)]