UVA 10844 - Bloques bell数

Problem E
Bloques
Time Limit: 2 seconds

Little Joan has N blocks,all of them of different sizes. He is playing to build cities in the beach. A city is just a collection of buildings.

A single block over the sand can be considered as a building. Then he can construct higher buildings by putting a block above any other block. At most one block can be put immediately above any other block. However he can stack several blocks together to construct a building. However, it�s not allowed to put bigger blocks on top of smaller ones, since the stack of blocks may fall. A block can be specified by a natural number that represents its size.

It doesn�t matter the order among buildings. That is:

1 3
2 4
is the same configuration as:
3 1
4 2

Your problem is to compute the number of possible different cities using N blocks. We say that #(N) gives the number of different cities of size N. If N=2, for instance, there are only two possible cities:

City #1:
1 2

In this city both blocks of size 1 and 2 are put over the sand.

City #2:
1
2

In this city block of size 1 is over block is size 2, and block of size 2 is over the sand.

So, #(2)=2.

Input
A sequence of non-negative integer numbers, each of one in different line. All of them but the last one are natural numbers. The last one is 0 and means the end. Each natural number is less than 900.

Output
For each natural number I in the input, you must write a line with the pair of numbers I, #(I).

Sample InputSample Output
2
3
0
2, 2
3, 5


Problemsetter: Dr. Mauricio Javier Osorio Galindo


思路:首先注意到题目要求相当于是将n个block分成m个集合的情况数。 也就是第二类斯特林数。 然后答案是sigma (s[n][m]) (1<=m<=n) 也就是bell(n) .   很明显题目需要用高精度,由于s[i][j] = s[i-1][j-1] + s[i-1][j] * j. 这里用乘法,会很慢的。 首先bell数能够用bell三角形来求。那个只有加法,会快一些。 由于只有加法,高精度的int可以压成8位来处理。


代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <time.h>
#include <map>
#include <set>
#include <stdio.h>
#include <cmath>
#include <string>
#include <cassert>
#include <math.h>
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define rrep(i,b,a) for(int i = (b); i >= (a); --i)
#define clr(a,x) memset(a,(x),sizeof(a))
#define LL long long
#define eps 1e-9
#define mp make_pair
using namespace std;
const int maxn = 900+1;
const int maxl = 220;
const int bitlen = 4;
char buffer[maxl * bitlen + 10];
const int MAXL = 1e8;

LL tmp[bitlen * maxl];
LL q[bitlen * maxl];

struct Bignum
{
    int a[maxl];
    int sz;

    Bignum()
    {
        memset(a, 0, sizeof(a));
        sz = 0;
    }

    Bignum(LL x)
    {
        memset(a, 0, sizeof(a));
        sz = 0;
        if (x == 0) sz = 1;
        while (x != 0)
        {
            a[sz++] = x % MAXL;
            x /= MAXL;
        }
    }

    void add(const Bignum&bg)
    {
        sz = max(sz, bg.sz);
        for (int i = 0; i < sz; ++i)
        {
            a[i] += bg.a[i];
            if (a[i] >= MAXL) {
                a[i + 1] += a[i] / MAXL;
                a[i] %= MAXL;
            }
        }
        while (a[sz] != 0) ++sz;
    }
    Bignum operator+(const Bignum&bg)
    {
        Bignum ret = *this;
        ret.add(bg);
        return ret;
    }

    void output() const
    {
        printf("%d", a[sz - 1]);
        for (int i = sz - 2; i >= 0; --i) printf("%08d", a[i]);
        printf("\n");
    }
};

Bignum s[2][maxn];
Bignum bell[maxn];

void pre_init()
{
    //s[i][j] = s[i-1][j-1] + s[i][j-1];
    s[0][0] = 1;
    bell[0] = 1;
    int cur = 1;
    rep(i,1,maxn) {
        s[cur][0] = s[cur^1][i-1];
        rep(j,1,i+1) {
            s[cur][j] = s[cur^1][j-1] + s[cur][j-1];
        }
        bell[i] = s[cur][i];
        cur ^= 1;
    }
}

int main()
{
    #ifdef ACM
        freopen("in.txt", "r", stdin);
       // freopen("out.txt","w",stdout);
    #endif // ACM
    pre_init();
    //cout << bell[900].sz << endl;
    for(;;) {
        int n; scanf("%d",&n);
        if (n == 0) break;
        printf("%d, ",n);
        bell[n-1].output();
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值