题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2023

题意:

  有n个家族,共m只蚂蚁(n <= 1000, m <= 100000)。

  每个家族有cnt[i]只蚂蚁,并且同一家族中的蚂蚁无差别。

  从窝里爬出来x只蚂蚁的方案数为f(x)。

  给定a,b,让你求 ∑ f(a to b) MOD 1000000。

题解:

  表示状态:

    dp[i][j] = combinations

    i:第i个家族已经考虑过了

    j:目前出来了j只蚂蚁

  找出答案:

    ans = ∑ dp[n][a to b]

  如何转移:

    dp[i][j] = ∑ dp[i-1][j-k] (0 <= k <= cnt[i], j-k >= 0)

    即:dp[i][j] = ∑ dp[i-1][max(0,j-cnt[i]) to j];

  边界条件:

    dp[0][0] = 1

    others = 0

  优化:

    (1)裸dp时间复杂度为O(n * m^2) = 10^13,绝对炸了。。。

      所以前缀和优化:求 ∑ dp[i-1][max(0,j-cnt[i]) to j]。

    (2)裸dp空间复杂度为 n*m(Byte) = 95 MB > 64 MB,又炸了咋办。。。

      滚动数组。因为dp[i][j]只会用到dp[i-1][...]。

AC Code:

 // state expression:
// dp[i][j] = combinations
// i: considering ith group
// j: j ants have been outside
//
// find the answer:
// sigma dp[n][a to b]
//
// transferring:
// dp[i][j] = sigma dp[i-1][j-k] (0 <= k <= cnt[i], j-k >= 0)
//
// boundary:
// dp[0][0] = 1
// others = 0
#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 1005
#define MAX_M 100005
#define MOD 1000000 using namespace std; int n,m,a,b;
int ans=;
int cnt[MAX_N];
int dp[][MAX_M];
int sum[][MAX_M]; void read()
{
cin>>n>>m>>a>>b;
memset(cnt,,sizeof(cnt));
int temp;
for(int i=;i<m;i++)
{
cin>>temp;
cnt[temp]++;
}
} int cal_mod(int x)
{
return (x%MOD+MOD)%MOD;
} int cal_sum(int k,int x,int y)
{
if(x==) return cal_mod(sum[k&][y]);
return cal_mod(sum[k&][y]-sum[k&][x-]);
} void update_sum(int k,int x)
{
if(x==) sum[k&][x]=cal_mod(dp[k&][x]);
else sum[k&][x]=cal_mod(sum[k&][x-]+dp[k&][x]);
} void solve()
{
memset(dp,,sizeof(dp));
dp[][]=;
for(int i=;i<=m;i++)
{
sum[][i]=;
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
dp[i&][j]=cal_sum(i-,max(,j-cnt[i]),j);
update_sum(i,j);
}
}
for(int i=a;i<=b;i++)
{
ans=cal_mod(ans+dp[n&][i]);
}
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}

BZOJ 2023 [Usaco2005 Nov]Ant Counting 数蚂蚁:dp【前缀和优化】的更多相关文章

  1. bzoj 2023: [Usaco2005 Nov]Ant Counting 数蚂蚁【生成函数||dp】

    用生成函数套路推一推,推完老想NTT--实际上把这个多项式乘法看成dp然后前缀和优化一下即可 #include<iostream> #include<cstdio> using ...

  2. 1630/2023: [Usaco2005 Nov]Ant Counting 数蚂蚁

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

  3. BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁(dp)

    题意 题目描述的很清楚...  有一天,贝茜无聊地坐在蚂蚁洞前看蚂蚁们进进出出地搬运食物.很快贝茜发现有些蚂蚁长得几乎一模一样,于是她认为那些蚂蚁是兄弟,也就是说它们是同一个家族里的成员.她也发现整个 ...

  4. 【bzoj2023/1630】[Usaco2005 Nov]Ant Counting 数蚂蚁 dp

    题解: 水题 f[i][j] 前i种用了j个,前缀和优化就可以了

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

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

  6. bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁*&&bzoj1630[Usaco2007 Demo]Ant Counting*

    bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁&&bzoj1630[Usaco2007 Demo]Ant Counting 题意: t个族群,每个族群有 ...

  7. bzoj1630 / bzoj2023 [Usaco2005 Nov]Ant Counting 数蚂蚁

    Description     有一天,贝茜无聊地坐在蚂蚁洞前看蚂蚁们进进出出地搬运食物.很快贝茜发现有些蚂蚁长得几乎一模一样,于是她认为那些蚂蚁是兄弟,也就是说它们是同一个家族里的成员.她也发现整个 ...

  8. BZOJ 3385: [Usaco2004 Nov]Lake Counting 数池塘

    题目 3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec  Memory Limit: 128 MB Description     农夫 ...

  9. 【noi 2.6_9289】Ant Counting 数蚂蚁{Usaco2005 Nov}(DP)

    题意:有M个家族的蚂蚁,各Ni只(互相相同).问选出 l~r 只的不同方案数. 解法:很基础的一种DP,不要被"排列组合"所迷惑了啊~我之前接触过这个类型,可惜又忘了,一定要记住! ...

随机推荐

  1. spring springmvc js websocket 监听

    第一步:web.xml中支持异步.所有的filter及servlet <filter> <filter-name>characterEncoding</filter-na ...

  2. js文件/图片从电脑里面拖拽到浏览器上传文件/图片

    1.效果展示 2.html 代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!DOCTYPE html> <html lang=& ...

  3. 编写自己的UDTF

    1. UDTF介绍 UDTF(User-Defined Table-Generating Functions) 用来解决 输入一行输出多行(On-to-many maping) 的需求. 2. 编写自 ...

  4. HDU4126Genghis Khan the Conqueror(最小生成树+并查集)

    Genghis Khan the Conqueror Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 327680/327680 K ...

  5. sh脚本——#!/bin/bash

    #!/bin/bash是指此脚本使用/bin/bash来解释执行. 其中,#!是一个特殊的表示符,其后,跟着解释此脚本的shell路径. bash只是shell的一种,还有很多其它shell,如:sh ...

  6. icmp的程序(ping的实现)

    code来源于<网络编程与分层协议设计> chap7 ICMP协议程序设计 ----没有理解,没有编译,只是敲了出来 ping.h #define ICMP_ECHOREPLY 0#def ...

  7. mysql 修改表引擎方法

    修改表引擎方法 方法1:修改mysql.ini配置文件,重启mysql服务生效 修改my.ini,在[mysqld]下加上default-storage-engine=INNODB 其中红色字体部分是 ...

  8. AngularJs + html 5 实现 裁剪上传

    直接上代码 directive.js app.directive('fileUploadersm', function () { return { restrict: 'E', transclude: ...

  9. nanoporetech/nanonet

    nanoporetech/nanonet CodeIssues 7Pull requests 0Projects 0Wiki Insights  First generation RNN baseca ...

  10. Ionic + AngularJS angular-translate 国际化本地化解决方案

    欢迎访问我们的网站,网站上有更多关于技术性的交流:http://www.ncloud.hk/技术分享/ionic-plus-angularjs-angular-translate-国际化本地化解决方案 ...