POJ 3046 Ant Counting(递推,和号优化)
计数类的问题,要求不重复,把每种物品单独考虑。
将和号递推可以把转移优化O(1)。
f[i = 第i种物品][j = 总数量为j] = 方案数
f[i][j] = sigma{f[i-1][j-k],(k = [0,min(j,c[i])])}
把和号展开
f[i][j] : j-0,j-1,...,j-a[i]
f[i][j-1] : j-1,j-2,...,j-a[i]-1
中间部分是一样的可以避免重复计算。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
//#include<bits/stdc++.h>
using namespace std; int T, A, S, B;
const int maxt = 1e3+, maxa = 1e5+, mod = 1e6;
int f[][maxa];
int c[maxt]; //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
scanf("%d%d%d%d",&T,&A,&S,&B);
for(int i = A; i--;){
int x; scanf("%d",&x);
c[x]++;
}
f[][] = f[][] = ; //不选方案数为1
for(int i = ; i <= T; i++){
int a = i&, b = a^;
for(int j = ; j <= B; j++){
if(j-c[i]>){
f[a][j] = ( f[a][j-] + f[b][j] - f[b][j--c[i]] ) % mod;
}else {
f[a][j] = ( f[a][j-] + f[b][j] ) % mod;
}
}
}
int sum = , *F = f[T&];
for(int i = S; i <= B; i++){
sum = (sum + F[i]) % mod;
}
printf("%d\n",sum<?sum+mod:sum);
return ;
}
POJ 3046 Ant Counting(递推,和号优化)的更多相关文章
- poj 3046 Ant Counting(多重集组合数)
Ant Counting Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- POJ 3046 Ant Counting ( 多重集组合数 && 经典DP )
题意 : 有 n 种蚂蚁,第 i 种蚂蚁有ai个,一共有 A 个蚂蚁.不同类别的蚂蚁可以相互区分,但同种类别的蚂蚁不能相互区别.从这些蚂蚁中分别取出S,S+1...B个,一共有多少种取法. 分析 : ...
- poj 3046 Ant Counting
Ant Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4982 Accepted: 1896 Desc ...
- poj 3046 Ant Counting (DP多重背包变形)
题目:http://poj.org/problem?id=3046 思路: dp [i] [j] :=前i种 构成个数为j的方法数. #include <cstdio> #include ...
- poj 3046 Ant Counting——多重集合的背包
题目:http://poj.org/problem?id=3046 多重集合的背包问题. 1.式子:考虑dp[ i ][ j ]能从dp[ i-1 ][ k ](max(0 , j - c[ i ] ...
- POJ 3046 Ant Counting DP
大致题意:给你a个数字,这些数字范围是1到t,每种数字最多100个,求问你这些a个数字进行组合(不包含重复),长度为s到b的集合一共有多少个. 思路:d[i][j]——前i种数字组成长度为j的集合有多 ...
- POJ 1664 放苹果 (递推)
题目链接:http://poj.org/problem?id=1664 dp[i][j]表示i个盘放j个苹果的方案数,dp[i][j] 可以由 dp[i - 1][j] 和 dp[i][j - i] ...
- HOJ 2148&POJ 2680(DP递推,加大数运算)
Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...
- POJ 2506 Tiling(递推+大整数加法)
http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. #includ ...
随机推荐
- mysql设置最大连接数量
mysql>mysql -uuser -ppassword(命令行登录MySQL) mysql>show variables like 'max_connections';(查可以看当前的 ...
- IE浏览器不支持Promise对象
1. 安装babel-polyfill插件转换 npm install --save-dev babel-polyfill 2. 在webpack中引入babel-polyfill 在webpack. ...
- Windows 在目录中搜索哪个文件中有指定字符串
findstr /s /i "string" *.* 表示,当前目录以及子目录下的所有文件中查找"string"这个字符串. *.*表示所有类型的文件. /s ...
- 你的php
最开始学习做网页,用的是HTML,现在开始php了,那么要想用php,首先你得安装对不对,那么问题来了,你上哪安去啊(一看小编就是东北人),那么小编给各位提供了两个链接(不要告诉其他人哦)https: ...
- Testing Round #12 B
Description A restaurant received n orders for the rental. Each rental order reserve the restaurant ...
- 转 rman-08120 以及查询隐含参数
rman-08120 We need RMAN to automatically purge archivelogs from the FRA once they are applied to the ...
- HDU 4622 Reincarnation Hash解法详解
今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...
- Kotlin容器
1. 容器 可变/不可变 List<out T> 只读list; MutableList<T>; Set<out T>/MutableSet<T> Ma ...
- SQL中改变列的数据类型
一.该列非主键.无default约束 直接更新: alter table 表名 alter column 列名 数据类型 二.该列为主键列.无default约束 (1)删除主键 alter table ...
- ACdream 1430——SETI——————【后缀数组,不重叠重复子串个数】
SETI Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statist ...