数蚂蚁

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

       (说白了就是问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. BIEE 创建一个简单的分析(2)

    步骤: 1.如果BIEE安装在本机,直接登录http://localhost:9704/analytics/ 点击右上方导航菜单中的“新建->分析” 2.选择上节创建的RPD文件中的SCOTT主 ...

  2. 【CF刷题】14-05-12

    Round 236 div.1 A:只需要每个点连接所有比他大的点,知道边用完为止. //By BLADEVIL #include <cmath> #include <cstdio& ...

  3. 克隆选择算法-python实现

    CSAIndividual.py import numpy as np import ObjFunction class CSAIndividual: ''' individual of clone ...

  4. bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]

    Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河.农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在 ...

  5. Codeforces 650A Watchmen

    传送门 time limit per test 3 seconds memory limit per test 256 megabytes input standard input output st ...

  6. 如何在 Arch Linux 的终端里设定 WiFi 网络

    如果你使用的是其他 Linux 发行版 而不是 Arch CLI,那么可能会不习惯在终端里设置 WiFi.尽管整个过程有点简单,不过我还是要讲一下.在这篇文章里,我将带领新手们通过一步步的设置向导,把 ...

  7. node相关的精典材料

    node.js电子书 了不起的Node.js 深入浅出Node.js node.js入门经典 node.js开发指南 node.js相关优秀博文 官网 Infoq深入浅出Node.js系列(进阶必读) ...

  8. vagrant 启动错误

    Stderr: VBoxManage.EXE: error: Failed to create the VirtualBox object!VBoxManage.EXE: error: Code E_ ...

  9. jquery uploadify 使用

    /*进度条框*/ .shangchuantishikuang { border: 7px solid #74A5BF; background-color: white; font-size: 14px ...

  10. 新浪微博客户端(11)-自定义checkBox

    在最后一个欢迎界面上添加一个CheckBox. // 2.添加4个UIImageView ; i < NEW_FEATURE_NUMS; i++) { UIImageView *imageVie ...