Critical Mass |
During the early stages of the Manhattan Project, the dangers of the new radioctive materials were not 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 and cardboard box piles on the cement floors of store rooms. Workers did not know that the substances they were handling could result in sickness, or worse, an explosion. The officials who new the danger assumed that they could ensure safety by never assembling any amount close to the critical mass estimated by the physicists. But mistakes were made. The workers, ignorant of the the dangers, often did not track these materials care- fully, and in some cases, too much material was stored together - an accident was waiting to happen.
Fortunately, the dangers were taken seriously by a few knowledgeable physicists. They drew up guidelines for how to store the materials to eliminate the danger of critical mass accumulations. The system for handling uranium was simple. Each uranium cube was marked ``U''. It was to be stacked with lead cubes (marked ``L'') interspersed. No more than two uranium cubes could be next to each other on a stack. With this simple system, a potential for the uranium reaching critical mass (three stacked next to each other) was avoided. The second constraint is that no more than thirty cubes can be 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, not paying attention or not trained with the new system, could easily cause a chain reaction. He posed the question: consider a worker stacking the radioactive cubes and non radioactive cubes at random on top of each other to a height of n cubes; how many possible combinations are there for a disaster to happen?
For example, say the stack is of size 3. There is one way for the stack to reach critical mass - if all 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
4 5 0
3 8
解题报告: 题目很长。意思是长度不超过30的L与U组成的串,存在连续3个连续U的串有多少种。
分析下,长度为n的串总共有2^n种,减去连续U不超过3的串的数量即可。
我们可以这样递推。dp[i][j]表示长度为i, 结尾为j个U的串的数量。j为0代表以L结尾,那么转移方程如下:
dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];
dp[i][1]=dp[i-1][0];
dp[i][2]=dp[i-1][1];
边界条件为:dp[1][0]=1, dp[1][1]=1, dp[1][2]=0。
长度不超过30,数据量不超过int,很容易A。代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[31][3];
void init()
{
dp[1][0]=1;
dp[1][1]=1;
dp[1][2]=0;
for(int i=2;i<=30;i++)
{
dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];
dp[i][1]=dp[i-1][0];
dp[i][2]=dp[i-1][1];
}
}
int main()
{
init();
int n;
while(~scanf("%d", &n) && n)
printf("%d\n", (1<<n)-dp[n][0]-dp[n][1]-dp[n][2]);
}
至此,训练指南中计数的beginner题A完了,收获还是非常大的。得好好总结一下呢。