题目: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. cf780c

                                                                                             C. Andryush ...

  2. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  3. BFS - 广度优先搜索 - 邻接列表表示法

    2017-07-25 21:40:22 writer:pprp 在DFS的基础上加上了一个BFS函数 #include <iostream> #include <queue> ...

  4. PAT1063. Set Similarity (25)

    来自http://blog.csdn.net/tiantangrenjian/article/details/16868399 set_intersection 交集  set_union 并集  s ...

  5. 【Node.js】'readline' 逐行读取、写入文件内容

    [转]运用readline逐行读取的两种实现 效果图如下: 左边1.log 为源文件 右边1.readline.log为复制后的文件 下边为命令行输出 实现方式一: [javascript] view ...

  6. springboot处理session生命周期

    在使用springboot开发过程中发现用户登陆后60s后session就自动失效了,需要重新登陆,明明 application.yml  文件里已经配置了 server.session.timeou ...

  7. org.apache.log4j.Logger用法

    在应用程序中添加日志记录总的来说基于三个目的 :监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作:跟踪代码运行时轨迹,作为日后审计的依据:担当集成开发环境中的调试器的作用,向文 ...

  8. Pandas与SQL比较

    由于许多潜在的Pandas用户对SQL有一定的了解,因此本文章旨在提供一些如何使用Pandas执行各种SQL操作的示例. import pandas as pd url = 'tips.csv' ti ...

  9. ElasticSearch数据副本模型

    介绍 ES里面的每一个索引(Index)由多个shard组成,每一个shard有多个副本.这些副本被称为同步组.当增加或者删除文档时,这些副本之间必须保持同步,以便让所有副本都能包含相同的文档.如果同 ...

  10. GO学习笔记:函数Panic和Recover

    Go没有像Java那样的异常机制,它不能抛出异常,而是使用了panic和recover机制.一定要记住,你应当把它作为最后的手段来使用,也就是说,你的代码中应当没有,或者很少有panic的东西.这是个 ...