bzoj 1630: [Usaco2007 Demo]Ant Counting【dp】
2018-05-09 17:26
393 查看
满脑子组合数学,根本没想到dp
设f[i][j]为前i只蚂蚁,选出j只的方案数,初始状态为f[0][0]=1
转移为
\[
f[i][j]=\sum_{k=0}^{a[i]}f[i-1][j-k]
\]
\[
f[i][j]=\sum_{k=max(j-a[i],0)}^{j}f[i-1][k]
\]
这样显然时空都不行,用滚动数组优化空间,前缀和优化时间即可
#include<iostream> #include<cstdio> using namespace std; const int N=1005,mod=1000000; int n,m,a ,l,r,f[N*100],s ,sum; int read() { int r=0,f=1; char p=getchar(); while(p>'9'||p<'0') { if(p=='-') f=-1; p=getchar(); } while(p>='0'&&p<='9') { r=r*10+p-48; p=getchar(); } return r*f; } int main() { n=read(),m=read(),l=read(),r=read(); for(int i=1;i<=m;i++) { int x=read(); a[x]++; } for(int i=0;i<=a[1];i++) s[i]=1; for(int i=1;i<=n;i++) { sum+=a[i]; for(int j=0;j<=sum;j++) f[j]=(s[j]-((j-a[i]-1)<0?0:s[j-a[i]-1])+mod)%mod; s[0]=f[0]; for(int j=1;j<=sum+a[i+1];j++) s[j]=(s[j-1]+f[j])%mod; } printf("%d\n",(s[r]-s[l-1]+mod)%mod); return 0; }
相关文章推荐
- bzoj1630 [Usaco2007 Demo]Ant Counting
- bzoj1630 [Usaco2007 Demo]Ant Counting
- bzoj1630/2023 [Usaco2007 Demo]Ant Counting
- 【BZOJ1630/2023】[Usaco2007 Demo]Ant Counting DP
- [BZOJ1630]=[BZOJ2023][Usaco2007 Demo]Ant Counting
- 【bzoj2023/1630】[Usaco2005 Nov]Ant Counting 数蚂蚁 dp
- Bzoj[2023/1630]: [Usaco2005 Nov]Ant Counting 数蚂蚁 DP+滚动数组
- BZOJ 1694 [Usaco2007 Demo]Grazing on the Run DP
- [BZOJ1630/2023][Usaco2005 Nov]Ant Counting 数蚂蚁(dp)
- 【BZOJ】1630: [Usaco2007 Demo]Ant Counting(裸dp/dp/生成函数)
- 【bzoj2023/1630】[Usaco2005 Nov]Ant Counting 数蚂蚁 (DP+前缀和+滚动数组)
- [BZOJ1628]=[BZOJ1683][Usaco2007 Demo]City skyline
- [单调栈] BZOJ 1113 [Poi2008]海报PLA & BZOJ 1628 [Usaco2007 Demo]City skyline
- 【bzoj1708】[USACO2007 Oct]Money奶牛的硬币 背包dp
- 【bzoj1613】【Usaco2007 Jan】Running贝茜的晨练计划(dp) 题解&代码
- 【BZOJ】1628 [Usaco2007 Demo]City skyline 单调栈
- 【BZOJ1710】[Usaco2007 Open]Cheappal 廉价回文【区间DP】
- bzoj1710[Usaco2007 Open]Cheappal 廉价回文 dp
- bzoj 1710: [Usaco2007 Open]Cheappal 廉价回文【区间dp】
- BZOJ 1625: [Usaco2007 Dec]宝石手镯( dp )