数蚂蚁

  题目大意:一只牛想数蚂蚁,蚂蚁分成很多组,每个组里面有很多只蚂蚁,现在问你有多少种组合方式

       (说白了就是问1,1,1,...,2...,3...,4...)这些东西有多少种排列组合方式

  这一道题我一开始想着去用矩阵乘法去做了,结果怎么想怎么不对,后来想着,如果对1,2,3,这些看成背包会怎么样呢?最后结果就压在最后一个背包就可以了

  这么一想就懂了,其实就是要你找到递推关系,直接画一个矩阵拉几个箭头就很容易地看出来,对于一个矩阵,dp[i][j]等于dp[i-1][k] j-f[i]<=k<j的所有之和

  因为我们是一个格子一个格子地数的,所以会有重复的计算,那么就弄一个队列区间维护长度就可以了,每一次循环减掉一开始的值,增加j的值

  状态转移方程

    dp[1][k] = 1 0<=k<=f[1]

    dp[i][j]= ∑dp[i-1][k]  j-f[i]<=k<j&& i<=f_sum

  这题直接用滚动数组也是很快的

  

 #include <stdio.h>
#include <stdlib.h>
#define MAX_N 1001
#define MAX_A 100
#define M 1000000 static int families[MAX_N];
static int dp1[MAX_N *MAX_A];
static int dp2[MAX_N *MAX_A]; void Search(const int, const int, const int); int main(void)
{
int families_sum, ants_sum, S, E, i, tmp;
while (~scanf("%d%d%d%d", &families_sum, &ants_sum, &S, &E))
{
for (i = ; i <= ants_sum; i++)
{
scanf("%d", &tmp);
families[tmp]++;
}
Search(families_sum, S, E);
}
return ;
} void Search(const int families_sum, const int S, const int E)
{
int i, j, L, now_amx, ans = ;
int *exchange = NULL, *now = dp2, *prev = dp1; now[] = ;
for (i = ; i <= families[]; i++)//基准情况
prev[i] = ;
now_amx = families[];
for (i = ; i <= families_sum; i++)
{
now_amx += families[i];
for (j = , L = ; j <= families[i]; j++)//先处理L<families[i]的情况
{
now[j] = (prev[j] + L) % M;
L += prev[j] % M;
}
for (;j <= now_amx; j++)
{
L -= prev[j - families[i] - ];
now[j] = (prev[j] + L) % M;
L += prev[j] % M;
}
exchange = prev; prev = now; now = exchange;
}
for (i = S; i <= E; i++)
ans = (ans + prev[i]) % M;
printf("%d\n", ans);
}

   

DP:Ant Counting(POJ 3046)的更多相关文章

  1. poj 3046 Ant Counting

    Ant Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4982   Accepted: 1896 Desc ...

  2. poj 3046 Ant Counting(多重集组合数)

    Ant Counting Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  3. 【POJ - 3046】Ant Counting(多重集组合数)

    Ant Counting 直接翻译了 Descriptions 贝西有T种蚂蚁共A只,每种蚂蚁有Ni只,同种蚂蚁不能区分,不同种蚂蚁可以区分,记Sum_i为i只蚂蚁构成不同的集合的方案数,问Sum_k ...

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

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

  5. BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁

    2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 56  Solved: 16[S ...

  6. 1630/2023: [Usaco2005 Nov]Ant Counting 数蚂蚁

    2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 85  Solved: 40[S ...

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

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

  8. DFS----Lake Counting (poj 2386)

    Lake Counting(POJ No.2386) Description Due to recent rains, water has pooled in various places in Fa ...

  9. poj 3046 Ant Counting (DP多重背包变形)

    题目:http://poj.org/problem?id=3046 思路: dp [i] [j] :=前i种 构成个数为j的方法数. #include <cstdio> #include ...

随机推荐

  1. 【kAri OJ604】圣哲的树

    时间限制 1000 ms 内存限制 65536 KB 题目描述 果园大咖圣哲有12个棵树,其中有且仅有一个是有病的,有病的树比真的或轻或重,给出3次天平测量重量的结果,每次告知左侧和右侧的树各有哪几个 ...

  2. BZOJ-1925 地精部落 烧脑DP+滚动数组

    1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...

  3. 学习笔记--函数式线段树(主席树)(动态维护第K极值(树状数组套主席树))

    函数式线段树..资瓷 区间第K极值查询 似乎不过似乎划分树的效率更优于它,但是如果主席树套树状数组后,可以处理动态的第K极值.即资瓷插入删除,划分树则不同- 那么原理也比较易懂: 建造一棵线段树(权值 ...

  4. BZOJ4590 自动刷题机

    Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题机刷题的方式非常简单:首先会瞬间得出题目的正确做法,然后开始写 ...

  5. POJ2823 Sliding Window

    Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 53086   Accepted: 15227 Case Time Limi ...

  6. Code Review Engine Learning

    相关学习资料 https://www.owasp.org/index.php/Code_review https://www.owasp.org/images/8/8e/OWASP_Code_Revi ...

  7. HDU #3333

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Descript ...

  8. jQuery的插入

    append(content) 概述 : 向每个匹配的元素内部追加内容. 这个操作与对指定的元素执行appendChild方法,将它们添加到文档中的情况类似 append(function(index ...

  9. jquery------添加jQuery对象方法

    my.js $(document).ready(function(){ (function($){ $.fn.swapClass=function(class1,class2){ if(this.ha ...

  10. jquery------导入jquery UI要使用的文件

    JQuery版本下载地址:http://jquery.com/download/ JQuer UI两个主要下载地址:http://jqueryui.com/download/  只需要里面的jquer ...