*【CodeForces - 214D 】Numbers (dp,组合数学)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/83657087

题干:

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.

解题报告:

    dp[i][j]表示:长度为i,用1~j数字所能构成的解的方案数。0的那一位单独处理就可以了、、

AC代码:(62ms)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 4e5 + 5;
const ll mod = 1000000000+7;

int n;
int a[15];
ll dp[150][22];
ll C[205][205];
void init() {
		C[0][0]=1;
	for(int i=1; i<=100; i++) {
		C[i][0]=1;
		for(int j=1; j<=i; j++) {
			C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
		}
	}
}
int main()
{
	init();
	cin>>n;
 	for(int i = 0; i<=9; i++) scanf("%d",a+i);
 	for(int i = a[1]; i<=n; i++) dp[i][1] = 1;//长度为i只包含1的 、
	for(int i = 1; i<=9; i++) {
		for(int j = 0; j<=n; j++) {
			for(int k = a[i]; k<=j; k++) {
				dp[j][i] = (dp[j][i] + dp[j-k][i-1]*C[j][k])%mod;
				
			}
		}
	}
	for(int i = 0; i<=n; i++) {
		for(int k = a[0]; k<i; k++) {
			dp[i][0] = (dp[i][0]+dp[i-k][9]*C[i-1][k])%mod;
		}
	}
	ll ans = 0;
	for(int i = 0; i<=n; i++) ans = (ans + dp[i][0]) % mod;
 	printf("%lld\n",ans);
	return 0 ;
 }

关于组合数的求法,还有一种92ms的,,打表到1e5才92ms、、、可以一试啊反正是On的、、

const ll mod = 1000000000+7;
const ll N = 300000+5;
const ll M = 3e5+3;
int n;
ll fac[1000005];            //阶乘
ll inv_of_fac[1000005];        //阶乘的逆元
int a[15];
ll dp[150][12];
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;
		//inv_of_fac[i]=qpow(fac[i],mod-2);//为什么不行啊 //也行
}
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;
}

附:

三套代码的dp思路以后看

#include <bits/stdc++.h>
using namespace std;
#define N 202
#define mod 1000000007

typedef long long LL;

int n, a[N], s[N];
LL c[N][N], f[N][N], ans;

void prepare() {
	c[0][0] = 1;
	for (int i = 1; i <= 200; i ++) {
		c[i][0] = 1;
		for (int j = 1; j <= i; j ++) c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod;
	}
}
inline LL h(int n, int k) {
	return c[n+k-1][k];
}
int main() {
	prepare();
	scanf("%d", &n);
	for (int i = 0; i <= 9; i ++) scanf("%d", &a[i]);
	for (int i = 1; i <= 9; i ++) s[i] = s[i-1] + a[i];
	int sum = s[9];
	f[0][0] = 1;
	for (int i = 1; i <= 9; i ++) {
		for (int j = s[i-1]; j <= n; j ++) {
			for (int k = a[i]; k <= n - j; k ++) {
				f[i][j+k] = (f[i][j+k] + f[i-1][j] * c[j+k][j]) % mod;
			}
		}
	}
	for (int zero = a[0]; zero <= n; zero ++) {
		for (int other = sum; other <= n - zero; other ++) {
			ans = (ans + f[9][other] % mod * h(other, zero)) % mod;
		}
	}
	printf("%I64d\n", ans);
	return 0;
}

代码2

#include<cstdio>
#include<cstring>
int f[15][105];
int d[15];
int n;
int mod=1000000007;
int c[105][105];
int main() {
	c[0][0]=1;
	for(int i=1; i<=100; i++) {
		c[i][0]=1;
		for(int j=1; j<=i; j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
	}
	scanf("%d",&n);
	int sum=0;
	int ans=0;
	for(int i=0; i<10; i++) {
		scanf("%d",d+i);
		sum+=d[i];
	}
	for(int i=1; i<=n; i++) {
		memset(f,0,sizeof(f));
		f[0][i]=1;
		for(int j=0; j<10; j++) {
			for(int k=0; k<=i; k++)
				if (f[j][k]) {
					for(int l=d[j]; l<=k; l++)
						f[j+1][k-l]=(f[j+1][k-l]+1ll*f[j][k]*c[k][l]%mod)%mod;
				}
		}
		ans=(ans+f[10][0])%mod;
	}
	n--;
	if (d[0]) {
		d[0]--;
	}
	for(int i=0; i<=n; i++) {
		memset(f,0,sizeof(f));
		f[0][i]=1;
		for(int j=0; j<10; j++) {
			for(int k=0; k<=i; k++)
				if (f[j][k]) {
					for(int l=d[j]; l<=k; l++)
						f[j+1][k-l]=(f[j+1][k-l]+1ll*f[j][k]*c[k][l]%mod)%mod;
				}
		}
		//printf("%d\n",f[10][0]);
		ans=(ans-f[10][0])%mod;
	}
	ans=(ans+mod)%mod;
	printf("%d\n",ans);
}

代码3:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

int a[10];
long long f[10][200];
long long C[300][300];
int main() {
	long long mod=1000000007;
	for (int i=1; i<=200; i++) {
		C[i][i]=C[i][0]=1;
	}
	C[0][0]=1;
	for (int i=2; i<=200; i++)
		for (int j=1; j<i; j++) {
			C[i][j]=C[i-1][j-1]+C[i-1][j];
			C[i][j]%=mod;
		}
	int n;
	cin>>n;
	int tot=0;
	for (int i=0; i<10; i++) {
		cin>>a[i];
		tot+=a[i];
	}
	memset(f,0,sizeof(f));
	for(int i=a[9]; i<=n; i++) {
		f[9][i]=1;
	}
	for (int i=8; i>0; i--) {
		if (a[i]==0) f[i][0]=f[i+1][0];
		for (int j=1; j<=n; j++)
			for (int k=a[i]; k<=j; k++) {
				f[i][j]+=f[i+1][j-k]*C[j][k];
				f[i][j]%=mod;
			}
	}
	for (int j=2; j<=n; j++)
		for (int k=a[0]; k<j; k++) {
			f[0][j]+=f[1][j-k]*C[j-1][k];
			f[0][j]%=mod;
		}
	// cout<<f[1][1]<<' '<<f[1][2]<<endl;
	long long ans=0;
	if (a[0]==0) {
		ans+=f[1][1];
	}
	for (int i=2; i<=n; i++) {
		ans+=f[0][i];
		ans%=mod;
	}
	cout<<ans%mod<<endl;
}

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/83657087