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 4is 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 Input | Sample 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();
}
}