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。但是发现这是裸动态数组。。

注意顺序即可

#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. //有三个家庭的ANT,共五只,分别编号为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

【BZOJ】1630: [Usaco2007 Demo]Ant Counting(裸dp/dp/生成函数)的更多相关文章

  1. bzoj 1630: [Usaco2007 Demo]Ant Counting【dp】

    满脑子组合数学,根本没想到dp 设f[i][j]为前i只蚂蚁,选出j只的方案数,初始状态为f[0][0]=1 转移为 \[ f[i][j]=\sum_{k=0}^{a[i]}f[i-1][j-k] \ ...

  2. 【BZOJ1630/2023】[Usaco2007 Demo]Ant Counting DP

    [BZOJ1630/2023][Usaco2007 Demo]Ant Counting 题意:T中蚂蚁,一共A只,同种蚂蚁认为是相同的,有一群蚂蚁要出行,个数不少于S,不大于B,求总方案数 题解:DP ...

  3. bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁*&&bzoj1630[Usaco2007 Demo]Ant Counting*

    bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁&&bzoj1630[Usaco2007 Demo]Ant Counting 题意: t个族群,每个族群有 ...

  4. bzoj1630 [Usaco2007 Demo]Ant Counting

    Description Bessie was poking around the ant hill one day watching the ants march to and fro while g ...

  5. bzoj1630/2023 [Usaco2007 Demo]Ant Counting

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1630 http://www.lydsy.com/JudgeOnline/problem.ph ...

  6. bzoj 2023: [Usaco2005 Nov]Ant Counting 数蚂蚁【生成函数||dp】

    用生成函数套路推一推,推完老想NTT--实际上把这个多项式乘法看成dp然后前缀和优化一下即可 #include<iostream> #include<cstdio> using ...

  7. BZOJ 2023 [Usaco2005 Nov]Ant Counting 数蚂蚁:dp【前缀和优化】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2023 题意: 有n个家族,共m只蚂蚁(n <= 1000, m <= 1000 ...

  8. BZOJ 1642: [Usaco2007 Nov]Milking Time 挤奶时间( dp )

    水dp 先按开始时间排序 , 然后dp. dp( i ) 表示前 i 个时间段选第 i 个时间段的最优答案 , 则 dp( i ) = max( dp( j ) ) + w_i ( 0 < j ...

  9. BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁(dp)

    题意 题目描述的很清楚...  有一天,贝茜无聊地坐在蚂蚁洞前看蚂蚁们进进出出地搬运食物.很快贝茜发现有些蚂蚁长得几乎一模一样,于是她认为那些蚂蚁是兄弟,也就是说它们是同一个家族里的成员.她也发现整个 ...

随机推荐

  1. 通过源码浅析Java中的资源加载

    前提 最近在做一个基础组件项目刚好需要用到JDK中的资源加载,这里说到的资源包括类文件和其他静态资源,刚好需要重新补充一下类加载器和资源加载的相关知识,整理成一篇文章. 理解类的工作原理 这一节主要分 ...

  2. ant design pro (六)样式

    一.概述 参看地址:https://pro.ant.design/docs/style-cn 基础的 CSS 知识或查阅属性,可以参考 MDN文档. 二.详细介绍 2.1.less Ant Desig ...

  3. 如何发布本地maven项目jar包部署到nexus私服?

    首先在我们的pom里面描述我们要部署的地址: <distributionManagement> <repository> <id>release</id> ...

  4. 如何防止SQL注入 http://zhangzhaoaaa.iteye.com/blog/1975932

    如何防止SQL注入 博客分类: 技术转载数据库 转自:http://021.net/vpsfaq/152.html -----解决方案--------------------------------- ...

  5. Android 系统 root 破解原理分析 (续)

    上文<Android系统root破解原理分析>介绍了Android系统root破解之后,应用程序获得root权限的原理.有一些网友提出对于root破解过程比较感兴趣,也提出了疑问.本文将会 ...

  6. IIS发布.net core mvc web站点

    这里只有操作步骤! 第一.查看IIS是否安装了 AspNetCoreModule,查看路径:IIS->模块 查看 安装步骤 下载网址:https://www.microsoft.com/net/ ...

  7. 数据库中varchar类型数据转换为numeric类型

    numeric有好几种选择,有整形.小数型等等.都是用cast来实现 前提:A表的ID字段是VARCHAR类型 .SELECT CAST(ID AS INTEGER) FROM A .SELECT C ...

  8. 非常简单的一个函数 竟然一直没有使用 find()

    find: 在非string类型的容器里,可以直接找出所对应的元素. find函数需要几个参数:迭代器,下标值,所要找的元素 vector<int> a; find(a.begin(),a ...

  9. C++14系列(2):C/C++的时间函数

    C++笔记開始 为了好好研究下C++14.顺便复习下曾经的C++知识.搞了个git(不断完好中): https://github.com/rododo/cpp14examples.git 里面会慢慢封 ...

  10. C# 改变无边框窗体尺寸大小的方法

    ; ; ; ; ; ; const int HTBOTTOMLEFT = 0x10; ; protected override void WndProc(ref Message m) { switch ...