题目链接:

http://codeforces.com/problemset/problem/213/B

B. Numbers

time limit per test 2 seconds
memory limit per test 256 megabytes
#### 问题描述
> 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.
#### 输入
> 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.
#### 输出
> On a single line print the remainder of dividing the answer to the problem by 1000000007 (109 + 7).
#### 样例
> **sample input**
> 3
> 1 1 0 0 0 0 0 0 0 0
>
> **sample output**
> 36

note

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

题意

给你0到9这十个数字,第i个数至少要用a[i]次,问能拼成的长度小于等于n的正整数(不能有前导零)

题解

dp[i][len]表示利用i到9的数字能拼成的长度为len的所有可能数。

状态转移方程:dp[i][len]=sigma(dp[i+1][len-k]*C[len][k])。

相当于是在用i+1到9凑成的长度为len-k的数字串里面塞进去k个i的所有可能数。用乘法原理可知去掉已经统计出来的len-k,我们要处理的就是从len里面选k个位置来放i。

注意:由于前导零不用考虑,而且只要统计正整数,所以我们在放0的时候,是不能让零放在第一位的,对于0我们可以特殊处理一下。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; typedef __int64 LL; const int maxn = 111;
const int mod = 1e9 + 7; int dig[22],n;
LL dp[22][maxn]; LL C[maxn][maxn];
void pre() {
memset(C, 0, sizeof(C));
C[0][0] = 1;
for (int i = 1; i < maxn; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++) {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
C[i][j] %= mod;
}
}
} int main() {
pre();
scanf("%d", &n);
for (int i = 0; i < 10; i++) {
scanf("%d", &dig[i]);
}
memset(dp, 0, sizeof(dp));
dp[10][0] = 1;
for (int i = 9; i > 0; i--) {
for (int j = 0; j < maxn; j++) {
for (int k = dig[i]; k <=j; k++) {
dp[i][j] += dp[i + 1][j - k] * C[j][k];
dp[i][j] %= mod;
}
}
}
for (int j = 0; j < maxn; j++) {
for (int k = dig[0]; k < j; k++) {
dp[0][j] += dp[1][j - k] * C[j - 1][k];
dp[0][j] %= mod;
}
}
LL ans = 0;
for (int j = 1; j <= n; j++) {
ans += dp[0][j];
ans %= mod;
}
printf("%I64d\n", ans);
return 0;
}

Codeforces Round #131 (Div. 1) B. Numbers dp的更多相关文章

  1. Codeforces Round #131 (Div. 2) B. Hometask dp

    题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...

  2. Codeforces Round #131 (Div. 2) E. Relay Race dp

    题目链接: http://codeforces.com/problemset/problem/214/E Relay Race time limit per test4 secondsmemory l ...

  3. Codeforces Round #260 (Div. 1) A - Boredom DP

    A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...

  4. Codeforces Round #131 (Div. 2)

    A. System of Equations \(a\)的范围在\(\sqrt n\)内,所以暴力枚举即可. B. Hometask 需要被2.5整除,所以末位必然为0,如果0没有出现,则直接返回-1 ...

  5. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

  6. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  7. Codeforces Round #539 (Div. 2) 异或 + dp

    https://codeforces.com/contest/1113/problem/C 题意 一个n个数字的数组a[],求有多少对l,r满足\(sum[l,mid]=sum[mid+1,r]\), ...

  8. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

  9. Codeforces Round #202 (Div. 1) D. Turtles DP

    D. Turtles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/problem/B ...

随机推荐

  1. Web Service 中返回DataSet结果的几种方法

    Web Service 中返回DataSet结果的几种方法: 1)直接返回DataSet对象    特点:通常组件化的处理机制,不加任何修饰及处理:    优点:代码精减.易于处理,小数据量处理较快: ...

  2. C# @Page指令中的AutoEventWireup,CodeBehind,Inherits

    AutoEventWireup 如果 Page 指令的 AutoEventWireup 属性被设置为 true(或者如果缺少此属性,因为它默认为 true) ,该页框架将自动调用页事件,即 Page_ ...

  3. [leetcode]_Same Tree

    第一次遇见Tree的题,拿到心慌,网上查了解题思路.写完就三行.. 最近努力学习一句话,学会喜欢自己. 题目:give two tree , you must judge if they are th ...

  4. XP极限编程

    13个核心实战  团队协作(Whole Team)  规划策略(The Planning Game) 软件发布计划(ReleasePlanning) 周期开发计划(IterationPlanning) ...

  5. 【转】8种Nosql数据库系统对比

    导读:Kristóf Kovács 是一位软件架构师和咨询顾问,他最近发布了一片对比各种类型nosql数据库的文章.文章由敏捷翻译 – 唐尤华编译.如需转载,请参见文后声明. 虽然SQL数据库是非常有 ...

  6. php中的一些小细节(1)

    <?php $a=false; echo $a; //var_dump($a); ?> 输出结果为:     (即为空): 为什么会这样子? 查看官网对echo的相关资料得出: 结论:ec ...

  7. (转)Android属性设置android:noHistory="true"

    设置 android:noHistory="true"后,该Activity在statck中不留历史痕迹.默认的值是false. 举例说明,假设有三个Activity分别是:A,B ...

  8. Learning Scrapy笔记(七)- Scrapy根据Excel文件运行多个爬虫

    摘要:根据Excel文件配置运行多个爬虫 很多时候,我们都需要为每一个单独的网站编写一个爬虫,但有一些情况是你要爬取的几个网站的唯一不同之处在于Xpath表达式不同,此时要分别为每一个网站编写一个爬虫 ...

  9. VMware共享目录设置

    1.保证虚拟机中已经成功安装了 VMware Tools (非常关键) 2.打开VMware,并使虚拟机处于关机状态,然后请按图中箭头所示进行操作 这样就大功告成了,此时进入虚拟机, 执行命令 cd  ...

  10. 在SQL Server 2012中新建用户

    一.问题描述 在最开始装SQL Server 2012时我选择的是Windows身份认证方式,现在想添加一个用户采用SQL Server身份验证. 二.具体思路 1.新建用户 2.将新建的用户添加到相 ...