题目:http://poj.org/problem?id=3046

多重集合的背包问题。

1.式子:考虑dp[ i ][ j ]能从dp[ i-1 ][ k ](max(0 , j - c[ i ] ) <= k <= j)转移来。

  对于j<=c[ i ],这就是前缀和一样,所以dp[ i ][ j ] = dp[ i ][ j-1 ] + dp[ i-1 ][ j ];

  对于j>c[ i ],加了dp[ i ][ j-1 ] + dp[ i-1 ][ j ]之后会多加了一项dp[ i-1 ][ j-c[ i ]-1 ],减掉即可。

2.意义:dp[ i-1 ][ j ]表示从前面组中选 j 个,dp[ i ][ j-1 ]表示从本组+前面组中选了 j-1 个,再在本组中选1个。

  有一个不合法的情况是dp[ i ][ j-1 ]中已经选了c[ i ]个本组的,就不能再在本组中选1个了。

  而 dp[ i ][ j-1 ]中已经选了c[ i ]个本组的 的方案数就是前面组中选了 j-1-c[ i ] 个的方案数(这样选 j-1 的时候就必须选c[ i ]个本组的了)。减掉即可。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=,M=1e5+,mod=1e6;
int n,m,c[N],l,r,dp[][M],ans;
int main()
{
scanf("%d%d%d%d",&n,&m,&l,&r);int x;
for(int i=;i<=m;i++)
{
scanf("%d",&x);c[x]++;
}
dp[][]=dp[][]=;
for(int i=;i<=n;i++)
{
int u=(i&),v=!u;
for(int j=;j<=r;j++)
{
dp[u][j]=(dp[v][j]+dp[u][j-])%mod;
if(j>c[i])dp[u][j]=((dp[u][j]-dp[v][j-c[i]-])%mod+mod)%mod;
}
}
int u=(n&);
for(int j=l;j<=r;j++)(ans+=dp[u][j])%=mod;
printf("%d",ans);
return ;
}

poj 3046 Ant Counting——多重集合的背包的更多相关文章

  1. poj 3046 Ant Counting

    Ant Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4982   Accepted: 1896 Desc ...

  2. poj 3046 Ant Counting(多重集组合数)

    Ant Counting Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  3. poj 3046 Ant Counting (DP多重背包变形)

    题目:http://poj.org/problem?id=3046 思路: dp [i] [j] :=前i种 构成个数为j的方法数. #include <cstdio> #include ...

  4. POJ 3046 Ant Counting DP

    大致题意:给你a个数字,这些数字范围是1到t,每种数字最多100个,求问你这些a个数字进行组合(不包含重复),长度为s到b的集合一共有多少个. 思路:d[i][j]——前i种数字组成长度为j的集合有多 ...

  5. POJ 3046 Ant Counting ( 多重集组合数 && 经典DP )

    题意 : 有 n 种蚂蚁,第 i 种蚂蚁有ai个,一共有 A 个蚂蚁.不同类别的蚂蚁可以相互区分,但同种类别的蚂蚁不能相互区别.从这些蚂蚁中分别取出S,S+1...B个,一共有多少种取法. 分析 :  ...

  6. POJ 3046 Ant Counting(递推,和号优化)

    计数类的问题,要求不重复,把每种物品单独考虑. 将和号递推可以把转移优化O(1). f[i = 第i种物品][j = 总数量为j] = 方案数 f[i][j] = sigma{f[i-1][j-k], ...

  7. 【POJ - 3046】Ant Counting(多重集组合数)

    Ant Counting 直接翻译了 Descriptions 贝西有T种蚂蚁共A只,每种蚂蚁有Ni只,同种蚂蚁不能区分,不同种蚂蚁可以区分,记Sum_i为i只蚂蚁构成不同的集合的方案数,问Sum_k ...

  8. BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁

    2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 56  Solved: 16[S ...

  9. poj3046 Ant Counting——多重集组合数

    题目:http://poj.org/problem?id=3046 就是多重集组合数(分组背包优化): 从式子角度考虑:(干脆看这篇博客) https://blog.csdn.net/viphong/ ...

随机推荐

  1. [算法]Plus One

    Question: Given a non-negative number represented as an array of digits, plus one to the number. The ...

  2. Spring Boot 中修改端口和上下文路径

    通过修改application.properties内容来改变访问的端口号和上下文路径(很简单!) spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.vi ...

  3. LeetCode——Max Consecutive Ones

    LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...

  4. jmeter-Foreach控制器与正则表达式

    使用正则表达式提取器匹配的id值有17个(参考上一篇) 如果我想对每个id值进行请求,这个时候就可以用到foreach控制器 添加 由于我正则表达式取值命名为orderid,这里就将orderid设置 ...

  5. Eclipse_下载地址

    1. http://www.eclipse.org/downloads/ http://www.eclipse.org/downloads/packages/ http://archive.eclip ...

  6. MySQL无法启动几种常见问题小结

    问题1:目录.文件权限设置不正确 MySQL的$datadir目录,及其下属目录.文件权限属性设置不正确,导致MySQL无法正常读写文件,无法启动. 错误信息例如: 复制代码 代码如下:[code] ...

  7. 【poj2553】The Bottom of a Graph(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2553 [题意] 给n个点m条边构成一幅图,求出所有的sink点并按顺序输出.sink点是指该点能到达的点反过来又能回到该点. [思路] ...

  8. 【Wannafly挑战赛9-B】数一数

    链接:https://www.nowcoder.net/acm/contest/71/B 题目就不贴了.. 设res[i]为第i行的最终结果,可以想到,res[i]为0或不为0.长度不是最短的字符串r ...

  9. 数据库设计系列之四--ER图

    逻辑设计是做什么? 1.将需求转化为数据库的逻辑模型 2.通过ER图的型式对逻辑模型进行展示 3.同所选用的具体的DBMS系统无关

  10. antd-iconfont for inner network

    npm install antd-iconfont --save npm install less less-loader --save-dev webpack.config.js const pat ...