HDOJ1521组合数学 指数型母函数计数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1521

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define pf printf
#define mem(a,b) memset(a,b,sizeof(a))
#define prime1 1e9+7
#define prime2 1e9+9
#define pi 3.14159265
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define scand(x) scanf("%llf",&x) 
#define f(i,a,b) for(int i=a;i<=b;i++)
#define scan(a) scanf("%d",&a)
#define mp(a,b) make_pair((a),(b))
#define P pair<int,int>
#define dbg(args) cout<<#args<<":"<<args<<endl;
#define inf 0x7ffffff
inline int read(){
    int ans=0,w=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch==‘-‘)w=-1;ch=getchar();}
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-‘0‘,ch=getchar();
    return ans*w;
}
int n,m,t;
const int maxn=15;
const ll mod=10000;
double fac(int x){
    double ans = 1.0;
    f(i,1,x)ans*=i;
    return ans;
}
int num[maxn];
double f[maxn],c1[maxn],c2[maxn];//分别为阶乘,当前扫描序列,结果集 
int main()
{
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    f(i,0,maxn-1)f[i]=fac(i);
    while(scanf("%d%d",&n,&m)!=EOF){
        f(i,1,n)scanf("%d",&num[i]);
        mem(c1,0);
        mem(c2,0);
        f(i,0,num[1]){
            c1[i]=1.0/f[i];
        }
        for(int k=2;k<=n;k++){
            for(int i=0;i<=m;i++)
                for(int j=0;j<=num[k]&&i+j<=m;j++){
                    c2[i+j]+=c1[i]/f[j];
                }
                for(int i=0;i<=m;i++){
                    c1[i]=c2[i],c2[i]=0; 
                } 
        }
        pf("%.0lf\n",c1[m]*f[m]);
    }
}

 

相关推荐