【BZOJ】1630: [Usaco2007 Demo]Ant Counting(裸dp/dp/生成函数)
http://www.lydsy.com/JudgeOnline/problem.php?id=1630
题意,给你n种数,数量为m个,求所有的数组成的集合选长度l~r的个数
后两者待会写。。
裸dp其实应该会tle的额,但是数据弱?
d[i][j]表示前i种j长度的数量
d[i][j]=sum{d[i-1][j-k]} 1<=k<=a[i]
会爆mle。但是发现这是裸动态数组。。
注意顺序即可
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1005, md=1e6;
int a[N], n, m, f[N*100], l, r, ans; int main() {
read(n); read(m); read(l); read(r);
for1(i, 1, m) ++a[getint()];
for1(i, 0, a[1]) f[i]=1;
for1(i, 2, n) {
for3(j, r, 0)
for1(k, 1, a[i]) if(j<k) break; else f[j]=(f[j]+f[j-k])%md;
}
for1(i, l, r) ans=(ans+f[i])%md;
printf("%d", ans);
return 0;
}
然后是前缀和一优化
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1005, md=1e6;
int a[N], n, m, f[N*100], sum[N*100], l, r, ans; int main() {
read(n); read(m); read(l); read(r);
for1(i, 1, m) ++a[getint()];
f[0]=1;
for1(i, 1, n) {
sum[0]=1;
for1(j, 1, r) sum[j]=(sum[j-1]+f[j])%md;
for3(j, r, 1)
if(j<=a[i]) f[j]=sum[j]%md;
else f[j]=(sum[j]-sum[j-a[i]-1])%md;
}
for1(i, l, r) ans=(ans+f[i])%md;
printf("%d", ans);
return 0;
}
Description
Input
Output
Sample Input
1
2
2
1
3
INPUT DETAILS:
Three types of ants (1..3); 5 ants altogether. How many sets of size 2 or
size 3 can be made?
Sample Output
OUTPUT DETAILS:
5 sets of ants with two members; 5 more sets of ants with three members
HINT
Source
【BZOJ】1630: [Usaco2007 Demo]Ant Counting(裸dp/dp/生成函数)的更多相关文章
- bzoj 1630: [Usaco2007 Demo]Ant Counting【dp】
满脑子组合数学,根本没想到dp 设f[i][j]为前i只蚂蚁,选出j只的方案数,初始状态为f[0][0]=1 转移为 \[ f[i][j]=\sum_{k=0}^{a[i]}f[i-1][j-k] \ ...
- 【BZOJ1630/2023】[Usaco2007 Demo]Ant Counting DP
[BZOJ1630/2023][Usaco2007 Demo]Ant Counting 题意:T中蚂蚁,一共A只,同种蚂蚁认为是相同的,有一群蚂蚁要出行,个数不少于S,不大于B,求总方案数 题解:DP ...
- bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁*&&bzoj1630[Usaco2007 Demo]Ant Counting*
bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁&&bzoj1630[Usaco2007 Demo]Ant Counting 题意: t个族群,每个族群有 ...
- bzoj1630 [Usaco2007 Demo]Ant Counting
Description Bessie was poking around the ant hill one day watching the ants march to and fro while g ...
- bzoj1630/2023 [Usaco2007 Demo]Ant Counting
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1630 http://www.lydsy.com/JudgeOnline/problem.ph ...
- bzoj 2023: [Usaco2005 Nov]Ant Counting 数蚂蚁【生成函数||dp】
用生成函数套路推一推,推完老想NTT--实际上把这个多项式乘法看成dp然后前缀和优化一下即可 #include<iostream> #include<cstdio> using ...
- BZOJ 2023 [Usaco2005 Nov]Ant Counting 数蚂蚁:dp【前缀和优化】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2023 题意: 有n个家族,共m只蚂蚁(n <= 1000, m <= 1000 ...
- BZOJ 1642: [Usaco2007 Nov]Milking Time 挤奶时间( dp )
水dp 先按开始时间排序 , 然后dp. dp( i ) 表示前 i 个时间段选第 i 个时间段的最优答案 , 则 dp( i ) = max( dp( j ) ) + w_i ( 0 < j ...
- BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁(dp)
题意 题目描述的很清楚... 有一天,贝茜无聊地坐在蚂蚁洞前看蚂蚁们进进出出地搬运食物.很快贝茜发现有些蚂蚁长得几乎一模一样,于是她认为那些蚂蚁是兄弟,也就是说它们是同一个家族里的成员.她也发现整个 ...
随机推荐
- iOS中使用iCloud一些须要注意的地方(Xcode7.2)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) 在自己的App中怎样使用iCloud有非常多文章能够查阅,这里 ...
- HBase建立二级索引的一些解决方式
HBase的一级索引就是rowkey,我们仅仅能通过rowkey进行检索. 假设我们相对hbase里面列族的列列进行一些组合查询.就须要採用HBase的二级索引方案来进行多条件的查询. 常见的二级索引 ...
- post请求乱码
jsp页面中 <meta http-equiv="content-type" content="text/html; charset=UTF-8"& ...
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- jquery导航,按钮等特效 - apycom
http://apycom.com/
- IO多路复用之epoll
1.基本知识 epoll是在2.6内核中提出的,是之前的select和poll的增强版本.相对于select和poll来说,epoll更加灵活,没有描述符限制.epoll使用一个文件描述符管理多个描述 ...
- Linux下 sleep函数的注意事项
1. 休眠sleep(unsigned int)为线程内操作 所以如果不同线程,信号量SIGALRM是不能中断sleep(): 编写程序进行测试 //timercreate_demo.cpp #i ...
- Python 列表 min() 方法
描述 Python 列表 min() 方法返回列表元素中的最小值. 语法 min() 方法语法: min(L) 参数 L -- 要返回最小值的列表. 返回值 返回列表元素中的最小值. 实例 以下实例展 ...
- tp请求和响应
一.请求参数 use think\Request; 1.获取方法如下: http://w.tp.com/index/index/index/user/AAA $this->request-> ...
- 如何利用webmin在Linux主机中添加网站
Linux系统因其高效稳定而受到广大用户的推崇与青睐,然后其管理的复杂性也使很多用户望而却步,动弹不得.为了降低 Linux系统的管理难度,更有效方便的使用该系统,我司所有Linux主机或VPS系统均 ...