[题目链接]
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; }