hdu 2082 hdu1521 生成函数

2082
普通型生成函数
G ( x ) = ∑ i = 0 n x i G(x) = \sum_{i = 0}^n x^i G(x)=i=0nxi

#include 
using namespace std;
typedef long long LL;
const int MAXN = 55;
LL c[MAXN], temp[MAXN], a[MAXN];
int main()
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);cout.tie(0);
    int N;
    cin >> N;
    while (N--)
    {
        for (int i = 1; i <= 26; i++)
        {
            scanf("%lld", &a[i]);
        }
        for (int i = 0; i < MAXN; i++)
            c[i] = temp[i] = 0;
        c[0] = 1;
        for (int i = 1; i <= 26; i++)
        {
            for (int j = 0; j <= a[i] && j * i <= 50; j++)
            {
                for (int k = 0; i * j + k <= 50; k++)
                {
                    temp[i * j + k] += c[k];
                }
            }
            for (int k = 0; k <= 50; k++)
            {
                c[k] = temp[k];
                temp[k] = 0;
            }
        }
        LL Ans = 0;
        for (int i = 1; i <= 50; i++)
        {
            Ans += c[i];
        }
        cout << Ans << '\n';
    }
}

1521
指数型生成函数 n n n种物品,共选 m m m个物品,排列数。
G ( i ) = ∑ 0 n x i i ! G(i) = \sum_{0}^{n} \frac{x^i}{i!} G(i)=0ni!xi
多个函数相乘,最后要再乘 m ! m! m!

#include 
using namespace std;
typedef long long LL;
const int MAXN = 20;
double c[MAXN], temp[MAXN];LL a[MAXN];
LL pre[MAXN];
void init()
{
    pre[1] = 1;
    for (int i = 2; i <= 10; i++)
    {
        pre[i] = pre[i - 1] * i;
    }
    
}
int main()
{
    init();pre[0] = 1;
    int n, m;
    while (~scanf("%d %d", &n, &m))
    {
        for (int i = 1; i <= n; i++)
            scanf("%lld", &a[i]);
        // cout<<"ai is "<
        for (int i = 0; i <= 10; i++)
            c[i] = temp[i] = 0;
        // cout<<"ooooooo "<
        for (int i = 0; i <= a[1]; i++)
        {
            // cout<<"iii is "<
            c[i] = 1.0 / (pre[i] + 0.0);
            // cout<<"ccc is "<
        }
            
        for (int i = 2; i <= n; i++)
        {
            for (int j = 0; j <= a[i] && j <= m; j++)
            {
                for (int k = 0; k + j <= m; k++)
                {
                    
                    temp[k + j] += c[k] / (pre[j] + 0.0);
                }
            }
            for (int j = 0; j <= 10; j++)
            {
                c[j] = temp[j];
                temp[j] = 0;
            }
        }
        // cout<<"m is "<
        double Ans = pre[m] * c[m];

        printf("%.0lf\n", Ans);
    }
}

你可能感兴趣的:(2020,happy,new,year)