zoukankan      html  css  js  c++  java
  • Codeforces 553A. Kyoya and Colored Balls

    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 i before 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.

    由于第i种颜色的最后一个球肯定比后面颜色的最后一个球先放,所以对于最后一个球,可以先挑出来,则得到1,2,3,4,...,k,然后再从1到k剩下的球一个一个颜色考虑,设h(i)为填到第i个颜色时的方案数,S(n)为∑ci(1=<i<=n),易知边界值h(1) = 1,然后对于每个已经填好的i-1个球,第i个颜色能填的地方就是前面S(i-1)+1个空,相当于S(i-1)+1个不同的箱子中放Ci-1个相同的球,所以

       h(i)  = h(i-1) * C(S(i-1)+c[i]-1,S(i-1)) = h(i-1) * C(S[i]-1,S[i-1])

    递推即可

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    
    #define SIZE 1005
    #define MOD 1000000007
    
    LL h[SIZE] = {0};
    LL num[SIZE],s[SIZE] = {0};
    LL n;
    
    LL Extended_Euclid(LL a,LL b,LL &x,LL &y)
    {
        LL t,d;
        if(!b)
        {
            x=1;
            y=0;
            return a;
        }
        d=Extended_Euclid(b,a%b,x,y);
        t=x;
        x=y;
        y=t-a/b*y;
        return d;
    }
    
    LL Inv(LL a,LL b)
    {
        LL x,y;
        Extended_Euclid(a,b,x,y);
        return (x%b+b)%b;
    }
    
    LL C(LL n,LL m,LL mod)
    {
        if(m>n) return 0;
        LL ans=1,i,a,b;
        for(i=1;i<=m;i++)
        {
            a=(n+1-i)%mod;
            b=Inv(i%mod,mod);
            ans=ans*a%mod*b%mod;
        }
        return ans;
    }
    
    LL C1(LL n,LL m,LL mod)
    {
        if(m==0) return 1;
        return C(n%mod,m%mod,mod)*C1(n/mod,m/mod,mod)%mod;
    }
    
    int main(){
      // freopen("test.in","r",stdin);
      ios::sync_with_stdio(false);
      cin >> n;
    
      for (LL i=1;i<=n;i++){
        cin >> num[i];
        s[i] = s[i-1] + num[i];
        if (i > 1)
          h[i] = h[i-1] * C(s[i]-1,s[i-1],MOD) % MOD;
        else
          h[i] = 1;
      }
      cout << h[n];
      return 0;
    }
    View Code
  • 相关阅读:
    8.10
    今日头条笔试题 1~n的每个数,按字典序排完序后,第m个数是什么?
    Gym 100500B Conference Room(最小表示法,哈希)
    CodeForces 438D The Child and Sequence(线段树)
    UVALIVE 6905 Two Yachts(最小费用最大流)
    Gym Conference Room (最小表示法,哈希)
    hdu 2389 Rain on your Parade(二分图HK算法)
    Codeforces Fox And Dinner(最大流)
    zoj 3367 Counterfeit Money(dp)
    ZOJ3370. Radio Waves(2-sat)
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7570661.html
Copyright © 2011-2022 走看看