【BZOJ】1630:[Usaco 2007 Demo]Ant Counting(裸dp/dp/生成関数)
5659 ワード
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が爆発します.しかし、これは裸のダイナミック配列であることが分かった.の
順序に注意すればよい
接頭辞と最適化
Description
Bessie was poking around the ant hill one day watching the ants march to and fro while gathering food. She realized that many of the ants were siblings, indistinguishable from one another. She also realized the sometimes only one ant would go for food, sometimes a few, and sometimes all of them. This made for a large number of different sets of ants! Being a bit mathematical, Bessie started wondering. Bessie noted that the hive has T (1 <= T <= 1,000) families of ants which she labeled 1..T (A ants altogether). Each family had some number Ni (1 <= Ni <= 100) of ants. How many groups of sizes S, S+1, ..., B (1 <= S <= B <= A) can be formed? While observing one group, the set of three ant families was seen as {1, 1, 2, 2, 3}, though rarely in that order. The possible sets of marching ants were: 3 sets with 1 ant: {1} {2} {3} 5 sets with 2 ants: {1,1} {1,2} {1,3} {2,2} {2,3} 5 sets with 3 ants: {1,1,2} {1,1,3} {1,2,2} {1,2,3} {2,2,3} 3 sets with 4 ants: {1,2,2,3} {1,1,2,2} {1,1,2,3}1 set with 5 ants:{1,1,2,2,3}Your job is to count the number of possible sets of ants given the data above.//3つの家庭のANTがあって、全部で5匹で、それぞれ1,2,2,1,3と番号付けて、今それを2つの集合と3の集合に分けて、何種類の分法があります
Input
* Line 1: 4 space-separated integers: T, A, S, and B * Lines 2..A+1: Each line contains a single integer that is an ant type present in the hive
Output
* Line 1: The number of sets of size S..B (inclusive) that can be created. A set like {1,2} is the same as the set {2,1} and should not be double-counted. Print only the LAST SIX DIGITS of this number, with no leading zeroes or spaces.
Sample Input
3 5 2 3
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
10
OUTPUT DETAILS:
5 sets of ants with two members; 5 more sets of ants with three members
HINT
Source
Silver
題意、あなたに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
Bessie was poking around the ant hill one day watching the ants march to and fro while gathering food. She realized that many of the ants were siblings, indistinguishable from one another. She also realized the sometimes only one ant would go for food, sometimes a few, and sometimes all of them. This made for a large number of different sets of ants! Being a bit mathematical, Bessie started wondering. Bessie noted that the hive has T (1 <= T <= 1,000) families of ants which she labeled 1..T (A ants altogether). Each family had some number Ni (1 <= Ni <= 100) of ants. How many groups of sizes S, S+1, ..., B (1 <= S <= B <= A) can be formed? While observing one group, the set of three ant families was seen as {1, 1, 2, 2, 3}, though rarely in that order. The possible sets of marching ants were: 3 sets with 1 ant: {1} {2} {3} 5 sets with 2 ants: {1,1} {1,2} {1,3} {2,2} {2,3} 5 sets with 3 ants: {1,1,2} {1,1,3} {1,2,2} {1,2,3} {2,2,3} 3 sets with 4 ants: {1,2,2,3} {1,1,2,2} {1,1,2,3}1 set with 5 ants:{1,1,2,2,3}Your job is to count the number of possible sets of ants given the data above.//3つの家庭のANTがあって、全部で5匹で、それぞれ1,2,2,1,3と番号付けて、今それを2つの集合と3の集合に分けて、何種類の分法があります
Input
* Line 1: 4 space-separated integers: T, A, S, and B * Lines 2..A+1: Each line contains a single integer that is an ant type present in the hive
Output
* Line 1: The number of sets of size S..B (inclusive) that can be created. A set like {1,2} is the same as the set {2,1} and should not be double-counted. Print only the LAST SIX DIGITS of this number, with no leading zeroes or spaces.
Sample Input
3 5 2 3
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
10
OUTPUT DETAILS:
5 sets of ants with two members; 5 more sets of ants with three members
HINT
Source
Silver