zoukankan      html  css  js  c++  java
  • [ACM] hdu 1521 排列组合(指数型母函数)

    排列组合

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2132    Accepted Submission(s): 877


    Problem Description
    有n种物品,并且知道每种物品的数量。要求从中选出m件物品的排列数。例如有两种物品A,B,并且数量都是1,从中选2件物品,则排列有"AB","BA"两种。
     

    Input
    每组输入数据有两行,第一行是二个数n,m(1<=m,n<=10),表示物品数,第二行有n个数,分别表示这n件物品的数量。
     

    Output
    对应每组数据输出排列数。(任何运算不会超出2^31的范围)
     

    Sample Input
    2 2 1 1
     

    Sample Output
    2


    解题思路:

    本题的母函数为  f(x)=  ( 1 + x +x^2 / 2!  + x^3 / 3! +.....+ x^n1 /  n1! ) (1+  x +x^2/ 2!+ x^3 / 3! +.....+ x^n2 /  n2! ) ..........( 1 + x +x^2 / 2!  + x^3 / 3! +.....+ x^nk /  nk! )

    模拟式子相乘。

    代码:

    #include <iostream>
    #include <iomanip>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    double c[12];
    double temp[12];//double类型
    int k[12];
    
    double  fac(int n)
    {
        double sum=1.0;//别忘了初始化
        for(int i=1;i<=n;i++)
        {
            sum*=i;
        }
        return sum;
    }
    int main()
    {
        int n,m;
        while(cin>>n>>m)
        {
            for(int i=1;i<=n;i++)
                cin>>k[i];
            memset(c,0,sizeof(c));
            memset(temp,0,sizeof(temp));
            for(int i=0;i<=k[1];i++)
            {
                c[i]=1.0/fac(i)//第一个式子系数初始化
            }
            for(int i=2;i<=n;i++)//从第二个式子开始
            {
                for(int j=0;j<=m;j++)//上一个式子的指数
                 for(int t=0;t<=k[i]&&j+t<=m;t++)//第i中物品可以选多少件
                {
                    temp[j+t]+=c[j]/fac(t);//关键,相乘以后指数的系数,等于上一个式子的系数乘以该式子的系数即(1/fac(t))
                }
                for(int j=0;j<=m;j++)
                   {
                       c[j]=temp[j];
                       temp[j]=0;
                   }
            }
            cout<<setiosflags(ios::fixed)<<setprecision(0)<<c[m]*fac(m)<<endl;//控制精度,否则会出错
        }
        return 0;
    }
    


  • 相关阅读:
    快速认识 Spring Boot 技术栈
    Spring JDBC Template -实验介绍
    Spring 基于注解的配置 -实验介绍
    Spring 自动扫描与装配 -实验介绍
    Spring IoC 容器 -实验介绍
    Spring 框架的 AOP -实验介绍
    使用 AspectJ 框架实现 Spring AOP
    Spring框架自动创建 proxy
    Spring 框架的 AOP
    考研计算机专业课基础:计算机结构化编程
  • 原文地址:https://www.cnblogs.com/sr1993/p/3697926.html
Copyright © 2011-2022 走看看