[POJ 3046] Ant Counting
Posted evenbao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ 3046] Ant Counting相关的知识,希望对你有一定的参考价值。
[题目链接]
http://poj.org/problem?id=3046
[算法]
DP,注意用滚动数组优化空间
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXT 1010 #define MAXS 100010 const int P = 1e6; int i,j,T,A,S,B,sum,x,ans; int s[MAXS]; int cnt[MAXT]; int f[2][MAXS]; int main() { scanf("%d%d%d%d",&T,&A,&S,&B); for (i = 1; i <= A; i++) { scanf("%d",&x); cnt[x]++; } f[0][0] = 1; for (i = 1; i <= T; i++) { sum += cnt[i]; s[0] = f[(i - 1) & 1][0]; for (j = 1; j <= sum; j++) s[j] = (s[j - 1] + f[(i - 1) & 1][j]) % P; for (j = 0; j <= sum; j++) { if (j <= cnt[i]) f[i & 1][j] = s[j]; else f[i & 1][j] = s[j] - s[j - cnt[i] - 1] + P; f[i & 1][j] %= P; } } for (i = S; i <= B; i++) ans = (ans + f[T & 1][i]) % P; printf("%d ",ans); return 0; }
以上是关于[POJ 3046] Ant Counting的主要内容,如果未能解决你的问题,请参考以下文章
POJ 3046 Ant Counting ( 多重集组合数 && 经典DP )
bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁*&&bzoj1630[Usaco2007 Demo]Ant Counting*