codeforces 553A A. Kyoya and Colored Balls(组合数学+dp)

本文介绍了一道关于排列组合的问题,即如何计算特定颜色顺序下,不同颜色球的所有可能排列方式的数量。通过使用动态规划的方法,并结合组合数学中的排列组合公式,给出了具体的实现算法。

题目链接:

A. Kyoya and Colored Balls

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color ibefore drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen.

Input

The first line of input will have one integer k (1 ≤ k ≤ 1000) the number of colors.

Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≤ ci ≤ 1000).

The total number of balls doesn't exceed 1000.

Output

A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007.

Examples
input
3
2
2
1
output
3
input
4
1
2
3
4
output
1680


题意:

k种颜色的求,第i种颜色的球有a[i]个,现在要求第i种球的最后一个一定在第i+1种球的最后一个的前边,问有多少种拿法;

思路:

dp[i]表示前i种球的拿法的方案数,再拿第i+1种球的时候,先取出一个放在最后,然后剩下a[i+1]-1个球,前边有sum个球,形成sum+1个空挡,相当于把a[i+1]-1个相同的小球放在sum+1个不同的盒子中,看这里总结的球盒汇总;

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>
 
using namespace std;
 
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
 
typedef  long long LL;
 
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}
 
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=3e6+10;
const int maxn=1e3+20;
const double eps=1e-12;

int a[maxn];
LL dp[maxn];
LL pow_mod(LL x,LL y)
{
    LL s=1,base=x;
    while(y)
    {
        if(y&1)s=s*base%mod;
        base=base*base%mod;
        y>>=1;
    }
    return s;
}
LL C(int x,int y)
{
    LL s1=1,s2=1;
    for(int i=x;i>x-y;i--)s1=s1*i%mod;
    for(int i=1;i<=y;i++)s2=s2*i%mod;
    return s1*pow_mod(s2,mod-2)%mod;
}
int main()
{
    int n;
    read(n);
    For(i,1,n)read(a[i]);
    int sum=a[1];
    dp[1]=1;
    for(int i=2;i<=n;i++)
    {
        dp[i]=dp[i-1]*C(a[i]-1+sum,sum)%mod;
        sum+=a[i];
    }
    print(dp[n]);
    return 0;
}

  

转载于:https://www.cnblogs.com/zhangchengc919/p/5789213.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值