UVA 580 危险的组合 (组合计数)*

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 cardboardboxpilesonthecementfloorsofstorerooms. Workersdidnotknowthatthesubstancesthey werehandlingcouldresultinsickness,orworse,anexplosion. Theofficialswhonewthedangerassumed 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. Sample Input 4 5 0 Sample Output 3 8

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
#define maxn 205
#define MAX 1000000000
#define ms memset
using namespace std;
/*
题目大意:用L,U去排列组合,
要求至少要有三个U连续排列在一起,
求满足这种组合的个数。

首先枚举三个U出现的区间,
即后缀,这时就将区间长度减去三,即把三个U
当成一个整体带入去排列,然后前缀的计数是
不出现连续三个的情况,即全部计数减去f(i),

上面的计数方法有个小瑕疵,前缀计数中,
如果邻接者三个U的位置是U则不用构成三个U
就可以造成重复度,所以强制前缀区间最后一个
是L,即可,最后的求和公式简单化简下即可。

*/
int n;
ll f[50],table[50];

void init()
{
    table[0]=1,table[1]=2;
    for(int i=2;i<=30;i++)   table[i]=2*table[i-1];///2的幂次数的计数

    memset(f,0,sizeof(f));
    f[0]=f[1]=f[2]=0,f[3]=1;
    for(int i=4;i<=30;i++)
    {
        f[i]=table[i-3];
        f[i]+=(i-3)*table[i-4];
        for(int j=2;j<=i-2;j++)
            f[i]-=f[j-2]*table[i-j-2];
    }
}



int main()
{
    init();
    while(scanf("%d",&n) && n )
       printf("%lld\n",f[n]);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值