杰拉斯的博客

标签:算法

[ACM_POJ_2081]动态规划入门练习(三)Recaman's Sequence

杰拉斯 杰拉斯 | 时间:2012-04-03, Tue | 5,622 views
编程算法 

Recaman's Sequence

Time Limit: 3000MS Memory Limit: 60000K
Total Submissions: 17939 Accepted: 7450

Description

The Recaman's sequence is defined by a0 = 0 ; for m > 0, am = am−1 − m if the rsulting am is positive and not already in the sequence, otherwise am = am−1 + m.
The first few numbers in the Recaman's Sequence is 0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9 ...
Given k, your task is to calculate ak.

Input

The input consists of several test cases. Each line of the input contains an integer k where 0 <= k <= 500000.
The last line contains an integer −1, which should not be processed.

Output

For each k given in the input, print one line containing ak to the output.

(阅读全文…)

[ACM_POJ_1579]动态规划入门练习(二)Function Run Fun

杰拉斯 杰拉斯 | 时间:2012-04-03, Tue | 21,926 views
编程算法 

Function Run Fun

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12362 Accepted: 6466

Description

We all love recursion! Don't we?

Consider a three-parameter recursive function w(a, b, c):

if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns: 1 if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)

if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)

otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)

This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.

(阅读全文…)

[ACM_POJ_1163]动态规划入门练习(一)The Triangle

杰拉斯 杰拉斯 | 时间:2012-04-02, Mon | 21,756 views
编程算法 

The Triangle

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 28092 Accepted: 16504

Description

7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

(Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.

(阅读全文…)

[ACM_HDU_1465]不容易系列之一(错排)

杰拉斯 杰拉斯 | 时间:2012-04-02, Mon | 17,819 views
编程算法 

不容易系列之一

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8025 Accepted Submission(s): 3328

Description

大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了!
做好“一件”事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就像花钱总是比挣钱容易的道理一样。
话虽这样说,我还是要告诉大家,要想失败到一定程度也是不容易的。比如,我高中的时候,就有一个神奇的女生,在英语考试的时候,竟然把40个单项选择题全部做错了!大家都学过概率论,应该知道出现这种情况的概率,所以至今我都觉得这是一件神奇的事情。如果套用一句经典的评语,我们可以这样总结:一个人做错一道选择题并不难,难的是全部做错,一个不对。

不幸的是,这种小概率事件又发生了,而且就在我们身边:
事情是这样的——HDU有个网名叫做8006的男性同学,结交网友无数,最近该同学玩起了浪漫,同时给n个网友每人写了一封信,这都没什么,要命的是,他竟然把所有的信都装错了信封!注意了,是全部装错哟!

现在的问题是:请大家帮可怜的8006同学计算一下,一共有多少种可能的错误方式呢?

Input

输入数据包含多个多个测试实例,每个测试实例占用一行,每行包含一个正整数n(1<n<=20),n表示8006的网友的人数。

Output

对于每行输入请输出可能的错误方式的数量,每个实例的输出占用一行。

(阅读全文…)

[ACM_HDU_2045]LELE的RPG难题

杰拉斯 杰拉斯 | 时间:2012-04-02, Mon | 11,927 views
编程算法 

不容易系列之(3)—— LELE的RPG难题

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15209 Accepted Submission(s): 6025

Description

人称“AC女之杀手”的超级偶像LELE最近忽然玩起了深沉,这可急坏了众多“Cole”(LELE的粉丝,即"可乐"),经过多方打探,某资深Cole终于知道了原因,原来,LELE最近研究起了著名的RPG难题:

有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法.

以上就是著名的RPG难题.

如果你是Cole,我想你一定会想尽办法帮助LELE解决这个问题的;如果不是,看在众多漂亮的痛不欲生的Cole女的面子上,你也不会袖手旁观吧?

Input

输入数据包含多个测试实例,每个测试实例占一行,由一个整数N组成,(0<n<=50)。

Output

对于每个测试实例,请输出全部的满足要求的涂法,每个实例的输出占一行。

(阅读全文…)