POJ Ant Counting DP
dp[i][j]表示前i种蚂蚁组成元素个数为j的集合有多少种。
则dp[i][j] = dp[i-1][j] + dp[i-1][j-1] + ... + dp[i-1][ max(0,j-a[i])];
直接算的话复杂度为O(TA^2)
状态的转移是一个区间内的数的和,所以再用一个数组f[i][j]记录dp[i][j]的前缀和。
dp[i][j] = f[i-1][j]-f[i-1][j-a[i]-1];
复杂度为O(TA)
同时可以用滚动数组优化空间复杂度。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm> using namespace std;
const int maxn = ;
const int mod = ;
int dp[maxn];
int da[];
int f[maxn];
int n,A,a,b;
int main()
{
while( scanf("%d%d%d%d", &n, &A, &a, &b) != EOF)
{
memset(da,,sizeof(da));
for(int i = ;i <= A; i++)
{
int x; scanf("%d", &x);
da[x]++;
}
dp[]=;
f[]=;
int maxv=;
for(int i = ; i <= n; i++)
{
maxv += da[i];
for(int i=;i<=maxv;i++)
f[i]=(f[i-]+dp[i])%mod;
for(int j=; j <= min(b,maxv); j++)
{
dp[j]=(f[j]-(j-da[i]<=?:f[j-da[i]-]))%mod;
}
}
int res=;
for(int i=a;i<=b;i++)
res=(res+dp[i])%mod;
printf("%d\n",res);
}
return ;
}
POJ Ant Counting DP的更多相关文章
- 【BZOJ1630/2023】[Usaco2007 Demo]Ant Counting DP
[BZOJ1630/2023][Usaco2007 Demo]Ant Counting 题意:T中蚂蚁,一共A只,同种蚂蚁认为是相同的,有一群蚂蚁要出行,个数不少于S,不大于B,求总方案数 题解:DP ...
- poj 3046 Ant Counting (DP多重背包变形)
题目:http://poj.org/problem?id=3046 思路: dp [i] [j] :=前i种 构成个数为j的方法数. #include <cstdio> #include ...
- POJ 3046 Ant Counting DP
大致题意:给你a个数字,这些数字范围是1到t,每种数字最多100个,求问你这些a个数字进行组合(不包含重复),长度为s到b的集合一共有多少个. 思路:d[i][j]——前i种数字组成长度为j的集合有多 ...
- poj 3046 Ant Counting
Ant Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4982 Accepted: 1896 Desc ...
- poj 3046 Ant Counting(多重集组合数)
Ant Counting Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- 【POJ - 3046】Ant Counting(多重集组合数)
Ant Counting 直接翻译了 Descriptions 贝西有T种蚂蚁共A只,每种蚂蚁有Ni只,同种蚂蚁不能区分,不同种蚂蚁可以区分,记Sum_i为i只蚂蚁构成不同的集合的方案数,问Sum_k ...
- BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁
2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 56 Solved: 16[S ...
- 1630/2023: [Usaco2005 Nov]Ant Counting 数蚂蚁
2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 85 Solved: 40[S ...
- bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁*&&bzoj1630[Usaco2007 Demo]Ant Counting*
bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁&&bzoj1630[Usaco2007 Demo]Ant Counting 题意: t个族群,每个族群有 ...
随机推荐
- python---str
Python转义字符 \ (在行尾时) 续行符 \\ 反斜杠符号 \' 单引号 \" 双引号 \a 响铃 \b 退格(Backspace) \e 转义 \000 空 \n 换行 \v 纵向制 ...
- reorderList使用
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" /> ...
- shell script练习
执行脚本的几种方式: 1. sh a.sh 或者 bash a.sh 调用的是 /bin/bash 进程执行的,所以脚本不需要执行权限. 2. 直接使用绝对路径执行, /home/script/a ...
- eclipse将编辑栏一分为二
今天无意中发现一个MyEclipse的功能,CTRL SHIF - 三个键一起按下时,同一个编辑窗口会一分为二.这样我们写下面代码需要参考前面代码的时候就很方便了.
- (String) 205.Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【转】JVM内存模型
http://longdick.iteye.com/blog/473866 图解JVM内存模型 博客分类: JVM JVM活动SUN /** * 转载请注明作者longdick http:/ ...
- php disk_free_space与disk_total_space实例介绍
php disk_free_space 函数与disk_total_space 函数教程,第一个函数是指函数返回的空间,以字节为单位,在指定的目录,而disk_total_space 函数返回的总空间 ...
- WCF使用小结:(1)WCF接收HTTP POST数据的处理方法
在WCF 4.0中,为我们创建Restful API有了更好的支持.通过定义UriTemplate,WebInvoke就可以快速开发API接口. 这里我记录一下HTTP POST数据时要如何接收POS ...
- 使用 Fiddler2 进行接口测试的方法
一 前言 部分业务需要进行接口测试,而接口测试的覆盖度稍有不全,可能就会造成包括启动崩溃在内的严重问题.目前本人所在的团队中业务大量使用了本地代码中直接 mock 数据进行测试,此种方法虽然可以测试到 ...
- 【练习】数据移动---parfile导出表中指定行:
要求: ①创建存放数据的文件: ②使用默认的bad文件生成方式: ③使用truncate选项方式. 1.准备条件: [oracle@host03 ~]$ mkdir datadump [oracle@ ...