uva580 Critical Mass

来源:互联网 发布:中日战争知乎 编辑:程序博客网 时间:2024/06/04 17:40

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 carefully, 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 Input The input is
a list of integers on separate lines. Each integer corresponds to the
size of the stack and is 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 the linear stack can either be “ L ” for lead, or “ U ”
for uranium. Output your answer as a single integer on a line by
itself.

设f[i]为i个盒子的合法方案数,g[i]为i个盒子的非法方案数。
对于f[n],考虑第一次出现三个U连续的情况是在i,i+1,i+2,则i-1【如果存在】必须是L,之前不能出现三个U连续,之后随便选。总方案数为g[i-2]*2^(n-i-2)。
另外在特殊处理一下i-1不存在的情况,即i=1,此时的方案数为2^(n-3)。
综上所述,f[n]=2^(n-3)+Σ(i=2..n-2)g[i-2]*2^(n-i-2)
g[n]=2^n-f[n]

#include<cstdio>#include<cstring>const int maxn=30;int f[35],g[35];int main(){    int i,j,k,m,n,p,q,x,y,z;    g[0]=1;    g[1]=2;    g[2]=4;    for (i=3;i<=maxn;i++)    {        f[i]=1<<(i-3);        for (j=2;j<=i-2;j++)          f[i]+=g[j-2]<<(i-j-2);        g[i]=(1<<i)-f[i];    }    while (scanf("%d",&n)&&n)      printf("%d\n",f[n]);}
0 0
原创粉丝点击