题目大意:蚂蚁牙黑,蚂蚁牙红:有A只蚂蚁,来自T个家族,分别记为ant[i]个。同一个家族的蚂蚁长得一样,但是不同家族的蚂蚁牙齿颜色不同。任取n只蚂蚁(S <= n <= B),求能组成几种集合?

状态:dp[i][j]:前i种中选j个可以组成的种数

决策:第i种选k个,k<=ant[i] && j-k>=0

转移:dp[i][j]=Σdp[i-1][j-k]

#include<stdio.h>
#include<string.h>
int dp[1005][10050];
int num[10050];
int main(){
int n,m,a,b,x;
while(scanf("%d%d%d%d",&n,&m,&a,&b)!=EOF){
memset(num,0,sizeof(num));
for(int i=0;i<m;i++){
scanf("%d",&x);
num[x]++;
}
memset(dp,0,sizeof(dp));
for(int i=0;i<=num[1];i++) dp[1][i]=1;
for(int i=2;i<=n;i++){
for(int j=0;j<=b;j++){
for(int k=0;k<=num[i];k++){
if(j>=k) {
dp[i][j]+=dp[i-1][j-k];
dp[i][j]%=1000000;
}
}
}
}
int ans=0;
for(int i=a;i<=b;i++){
ans+=dp[n][i];
ans%=1000000;
}
printf("%d\n",ans);
}
return 0;
}

还有一种

dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-ant[i]-1]

#include<iostream>
using namespace std;
#define MOD 1000000
int T, A, S, B;
int ant[1005];
int dp[2][100000];
int ans;
int main()
{
scanf("%d%d%d%d", &T, &A, &S, &B);
for (int i = 1; i <= A; i++)
{
int aa;
scanf("%d", &aa);
ant[aa]++;
}
dp[0][0] = dp[1][0] = 1;
for (int i = 1; i <= T; i++)
for (int j = 1; j <= B; j++)
if (j - ant[i] - 1 >= 0) dp[i % 2][j] = (dp[(i - 1) % 2][j] + dp[i % 2][j - 1] - dp[(i - 1) % 2][j - ant[i] - 1] + MOD) % MOD; //在取模时若出现了减法运算则需要先+Mod再对Mod取模,防止出现负数(如5%4-3%4为负数)
else dp[i % 2][j] = (dp[(i - 1) % 2][j] + dp[i % 2][j - 1]) % MOD;
for (int i = S; i <= B; i++)
ans = (ans + dp[T % 2][i]) % MOD;
printf("%d\n", ans);
return 0;
}

POJ 3046的更多相关文章

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

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

  2. poj 3046 Ant Counting——多重集合的背包

    题目:http://poj.org/problem?id=3046 多重集合的背包问题. 1.式子:考虑dp[ i ][ j ]能从dp[ i-1 ][ k ](max(0 , j - c[ i ] ...

  3. DP:Ant Counting(POJ 3046)

    数蚂蚁 题目大意:一只牛想数蚂蚁,蚂蚁分成很多组,每个组里面有很多只蚂蚁,现在问你有多少种组合方式 (说白了就是问1,1,1,...,2...,3...,4...)这些东西有多少种排列组合方式 这一道 ...

  4. poj 3046 Ant Counting

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

  5. POJ 3046 Ant Counting DP

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

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

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

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

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

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

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

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

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

随机推荐

  1. 调用WebService报错404问题 (转载)

    我想在MVC4的项目添加一个webservice文件,访问没问题,但是最后调用方法就报404错误. 但是如果我全新ASP.NET 空Web应用程序 然后再添加一个webservice文件,就一切OK. ...

  2. [SharePoint]如何去掉欢迎导航的信息?

    在我们实际的开发过程中,经常会把SharePoint中的一些已有的feature去掉,如欢迎信息下面的“我的设置”,“对本页面进行个性化设置”.详细见下图: Figure 1  常见的欢迎信息的界面 ...

  3. python爬取斗鱼B总直播弹幕

    在某群中看到关于弹幕爬取的需求,又因为斗鱼比较OP,就以这个作为切入点. 如果你想了解如何获取弹幕,我的这个例子就可以让你豁然开朗,对于哪些没有开发弹幕的直播或视频平台,就需要用抓包工具获取请求,然后 ...

  4. Python 学习笔记(十一)Python语句(二)

    For 循环语句 基础知识 for循环可以遍历任何序列的项目,如一个列表或者一个字符串. 语法: for 循环规则: do sth >>> for i in "python ...

  5. plsql中特殊字符的处理

    --去除换行chr(10) update zhzl_address t set t.add_administration_num=replace(t.add_administration_num,ch ...

  6. Spring 整合Mybatis dao原始方法

    先看一下项目图,基本就理解了整合的内容 这次主角不再是Mybats的配置文件SqlMapConfig.xml了,而是Spring的applicationContext.xml applicationC ...

  7. MySQL高可用之MGR安装测试

    Preface     We've learned the machenism of MGR yesterday,Let's configurate an environment and have s ...

  8. 浅析MySQL主从复制技术(异步复制、同步复制、半同步复制)

      Preface       As we all know,there're three kinds of replication in MySQL nowadays.Such as,asynchr ...

  9. IDEA中使用单元测试@Test等,提示没有 Junit.jar包

    1.File-->Project Structure-->Modules-->右侧Dependencies-->+号-->JARs or directories... 2 ...

  10. java对象转map

    /** * java对象转map * @param obj * @return * @throws IllegalAccessException * @throws IllegalArgumentEx ...