UVaOJ 580 Critical Mass(基础DP)

来源:互联网 发布:ipad玩游戏网络不稳定 编辑:程序博客网 时间:2024/04/29 11:44

During the early stages of the Manhattan Project, the dangers of the new radioctive materials werenot widely known. Vast new factory cities were built to manufacture uranium and plu- tonium in bulk.Compounds and solutions of these substances were accumulating in metal barrels, glass bottles andcardboard box piles on the cement floors of store rooms. Workers did not know that the substances theywere handling could result in sickness, or worse, an explosion. The officials who new the danger assumedthat they could ensure safety by never assembling any amount close to the critical mass estimated bythe physicists. But mistakes were made. The workers, ignorant of the the dangers, often did not trackthese materials carefully, and in some cases, too much material was stored together — an accident waswaiting to happen.

Fortunately, the dangers were taken seriously by a few knowledgeable physicists. They drew upguidelines for how to store the materials to eliminate the danger of critical mass accumulations. Thesystem for handling uranium was simple. Each uranium cube was marked “U”. It was to be stackedwith lead cubes (marked “L”) interspersed. No more than two uranium cubes could be next to eachother on a stack. With this simple system, a potential for the uranium reaching critical mass (threestacked next to each other) was avoided. The second constraint is that no more than thirty cubes canbe stacked on top of each other, since the height of the storage room can only accommodate that many.

One of the physicists was still not completely satisfied with this solution. He felt that a worker, notpaying attention or not trained with the new system, could easily cause a chain reaction. He posedthe question: consider a worker stacking the radioactive cubes and non radioactive cubes at randomon top of each other to a height of n cubes; how many possible combinations are there for a disaster tohappen?

For example, say the stack is of size 3. There is one way for the stack to reach critical mass — ifall three cubes are radioactive.

1: UUU

However, if the size of the stack is 4, then there are three ways:

1: UUUL

2: LUUU

3: UUUU

Input

The input is a list of integers on separate lines. Each integer corresponds to the size of the stack andis always greater than 0. The input is terminated with a integer value of ‘0’.

Output

For each stack, compute the total number of dangerous combinations where each cube position in thelinear stack can either be “L” for lead, or “U” for uranium.Output your answer as a single integer ona line by itself.

Sample Input

4

5

0

Sample Output

3

8

题目大意:有两种物品L和U堆成一摞,要求不能出现连续的3个或3个以上U,否则就会有危险。给出物品的个数,求危险的摆放方式的数目。

解题思路:危险排列 = 全部排列 - 安全排列,危险排列数等于2 ^ n,设dp[i]为第i个位置以L结束的安全拍列,则dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3](i > 3),两者相减即得答案。

代码如下:

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 31;int f[maxn] = {0,2,4,7},ans[maxn];int main(){    for(int i = 4;i < maxn;i++){        f[i] = f[i - 1] + f[i - 2] + f[i - 3];    }    int temp;    for(int i = 1;i < maxn;i++){        ans[i] = (int)pow(2,i) - f[i];    }    int n;    while(scanf("%d",&n) != EOF && n){        printf("%d\n",ans[n]);    }    return 0;}


0 0
原创粉丝点击