Codeforces 214D Numbers【思维+Dp】

351 篇文章 2 订阅

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

Furik loves writing all sorts of problems, especially such that he can't solve himself. You've got one of his problems, the one Furik gave to Rubik. And Rubik asks you to solve it.

There is integer n and array a, consisting of ten integers, indexed by numbers from 0 to 9. Your task is to count the number of positive integers with the following properties:

  • the number's length does not exceed n;
  • the number doesn't have leading zeroes;
  • digit i (0 ≤ i ≤ 9) occurs in the number at least a[i] times.
Input

The first line contains integer n (1 ≤ n ≤ 100). The next line contains 10 integers a[0]a[1]...a[9] (0 ≤ a[i] ≤ 100) — elements of array a. The numbers are separated by spaces.

Output

On a single line print the remainder of dividing the answer to the problem by 1000000007 (109 + 7).

Examples
input
1
0 0 0 0 0 0 0 0 0 1
output
1
input
2
1 1 0 0 0 0 0 0 0 0
output
1
input
3
1 1 0 0 0 0 0 0 0 0
output
36
Note

In the first sample number 9 meets the requirements.

In the second sample number 10 meets the requirements.

In the third sample numbers 10, 110, 210, 120, 103 meet the requirements. There are other suitable numbers, 36 in total.


题目大意:


让我们计算有多少种构造方式 ,使得数字串(不能有前导0)长度小于等于n,并且0~9的每个数字至少使用了a【i】个。


思路:


我们设定Dp【i】【j】表示长度为i,从数字9开始倒着插入数字,现在插到数字j的构造方案数。

那么有:

Dp【i】【j】=ΣDp【i-k】【j+1】*C(j,k);


注意一下j==0的情况,特判一下不能有前导0即可。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define LL long long int
const LL mod = 1000000000+7;
const LL N = 300000+5;
const LL M = 3e5+3;
LL fac[1000005];            //阶乘
LL inv_of_fac[1000005];        //阶乘的逆元
LL qpow(LL x,LL n)
{
    LL ret=1;
    for(; n; n>>=1)
    {
        if(n&1) ret=ret*x%mod;
        x=x*x%mod;
    }
    return ret;
}
void init()
{
    fac[1]=1;
    for(int i=2; i<=M; i++)
        fac[i]=fac[i-1]*i%mod;
    inv_of_fac[M]=qpow(fac[M],mod-2);
    for(int i=M-1; i>=0; i--)
        inv_of_fac[i]=inv_of_fac[i+1]*(i+1)%mod;
}
LL C(LL a,LL b)
{
    if(b>a) return 0;
    if(b==0) return 1;
    return fac[a]*inv_of_fac[b]%mod*inv_of_fac[a-b]%mod;
}
int a[15];
LL dp[150][12];
int main()
{
    init();
    int n;
    while(~scanf("%d",&n))
    {
        memset(dp,0,sizeof(dp));
        for(int i=0;i<=9;i++)scanf("%d",&a[i]);
        for(int i=a[9];i<=n;i++)dp[i][9]=1;
        for(int i=8;i>=0;i--)
        {
            for(int j=0;j<=n;j++)
            {
                for(int k=a[i];k<=j;k++)
                {
                    if(i!=0)dp[j][i]=(dp[j][i]+dp[j-k][i+1]*C(j,k))%mod;
                    else if(i==0&&j>k)
                    {
                        dp[j][i]=(dp[j][i]+dp[j-k][i+1]*C(j-1,k))%mod;
                    }
                }
            }
        }
        LL ans=0;
        for(int i=0;i<=n;i++)ans=(ans+dp[i][0])%mod;
        printf("%I64d\n",ans);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值