问题 D: A Famous Stone Collector

时间限制: 1 Sec 内存限制: 128 MB
题目描述
Mr. B loves to play with colorful stones. There are n colors of stones in his collection. Two stones with the same color are indistinguishable. Mr. B would like to select some stones and arrange them in line to form a beautiful pattern. After several arrangements he finds it very hard for him to enumerate all the patterns. So he asks you to write a program to count the number of different possible patterns.

Two patterns are considered different, if and only if they have different number of stones or have different colors on at least one position.
输入
Each test case starts with a line containing an integer n indicating the kinds of stones Mr. B have. Following this is a line containing n integers - the number of available stones of each color respectively. All the input numbers will be nonnegative and no more than 100.
输出
For each test case, display a single line containing the case number and the number of different patterns Mr. B can make with these stones, modulo 1,000,000,007, which is a prime number.
样例输入 Copy
3
1 1 1
2
1 2
样例输出 Copy
Case 1: 15
Case 2: 8
提示
In the first case, suppose the colors of the stones Mr. B has are B, G and M, the different patterns Mr. B can form are: B; G; M; BG; BM; GM; GB; MB; MG; BGM; BMG; GBM; GMB; MBG; MGB.

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long ll;

const ll mod = 1e9 + 7, N = 110;

// 读入数据
ll read(){
    
    
    ll x = 0, flag = 1;char c;
    while ((c = getchar()) < '0' || c > '9') 
        if (c == '-') flag = -1;
    while (c >= '0' && c <= '9') 
        x = x * 10 + (c^48), c = getchar();
    
    return x * flag;
}

ll n;
ll a[N], pre[N], inv[N*N], fac[N*N];
ll f[N][N*N], Case;

void init(ll n){
    
    
    inv[0] = inv[1] = fac[0] = fac[1] = 1;
    for (ll i = 2; i <= n; i ++ ){
    
    
        inv[i] = inv[mod % i] * (mod - mod / i) % mod;
        //cout << inv[i] << endl;
    }
    for (ll i = 2; i <= n; i ++ )
        inv[i] = inv[i] * inv[i - 1] % mod;
    for (ll i = 1; i <= n; i ++ )
        fac[i] = fac[i - 1] * i % mod;
}

ll C(ll n, ll m){
    
    
    return fac[n] * inv[m] % mod * inv[n - m] % mod;
}

int main(){
    
    
    init(10000);
    while (~scanf("%lld", &n)){
    
    
        memset(f, 0, sizeof f);
        for (ll i = 1; i <= n; i ++ ){
    
    
            a[i] = read();
            pre[i] = pre[i - 1] + a[i];
        }
        f[0][0] = 1;
        for (ll i = 1; i <= n; i ++ )
            for (ll j = 0; j <= pre[i]; j ++ )
                for (ll k = 0; k <= a[i] && k <= j; k ++ )
                    f[i][j] = (f[i][j] + f[i - 1][j - k] * C(j,k) % mod);
        
        ll ans = 0;
        for (ll i = 1; i <= pre[n]; i ++ )
            ans = (ans + f[n][i] % mod);
        printf("Case %lld: %lld\n", ++Case, ans);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_45914558/article/details/108530230