poj 3046 Ant Counting (dp)
2016-10-25 21:39
330 查看
Ant Counting
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. 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 Sample Output 10 Hint INPUT DETAILS: Three types of ants (1..3); 5 ants altogether. How many sets of size 2 or size 3 can be made? OUTPUT DETAILS: 5 sets of ants with two members; 5 more sets of ants with three members Source USACO 2005 November Silver |
[Discuss]
题解:dp
f[i][j]表示前i中数中,共选j个的方案数。
f[i][j]=f[i][j]+f[i-1][j-k] k<=min(j,cnt[i])
需要用滚动数组,如果加前缀和优化的话可以将时间复杂度降为O(AT)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 100003
#define p 1000000
using namespace std;
int f[100]
,n,m,a,b,cnt
;
int main()
{
freopen("a.in","r",stdin);
scanf("%d%d%d%d",&n,&m,&a,&b);
for (int i=1;i<=m;i++) {
int x; scanf("%d",&x);
cnt[x]++; //f[i][0]=1;
}
/*f[0][0]=1;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
for (int k=0;k<=min(j,cnt[i]);k++)
f[i][j]=(f[i][j]+f[i-1][j-k])%p;*/
f[0][0]=1; f[1][0]=1;
for (int i=1;i<=n;i++)
{
memset(f[i%2],0,sizeof(f[i%2]));
for (int j=1;j<=m;j++)
for (int k=0;k<=min(j,cnt[i]);k++)
f[i%2][j]=(f[i%2][j]+f[(i-1)%2][j-k])%p;
f[i%2][0]=1;
}
int ans=0;
for (int i=a;i<=b;i++) ans=(ans+f[n%2][i])%p;
printf("%d\n",ans);
}
相关文章推荐
- [POJ 3046]Ant Counting[dp][优化]
- DP:Ant Counting(POJ 3046)
- O - Ant Counting(DP,滚动数组)poj 3046
- POJ - 3046 Ant Counting(多重集组合数DP)
- POJ 3046 Ant Counting 简单DP
- POJ 3046 Ant Counting(多重集组合数)(基础组合dp)
- poj 3046 Ant Counting (DP多重背包变形)
- poj 3046 Ant Counting (DP多重背包变形)
- POJ 3046 Ant Counting (dp 多重集组合数)
- POJ 3046 - Ant Counting(dp多种背包变型)
- POJ 3046 Ant Counting 简单DP 挑战程序设计实战习题
- POJ 3046 Ant Counting (dp)
- dp: POJ 3046 Ant Counting
- POJ-3046-Ant Counting
- POJ 题目3046 Ant Counting(母函数)
- POJ 3046 Ant Counting
- poj-3046-dp
- POJ 3046 Ant Counting(递推,和号优化)
- POJ3046——Ant Counting(dp算法)
- POJ 3046 DP