记录一下一种推组合数前缀和的方法

Trick

设\(\sum_{i = 0}^m C_n^i = S(n, m)\)

\(S\)是可以递推的

  • \(S(n, m + 1) = S(n, m) + C_{n}^{m + 1}\)

就是加上最末尾的一项

  • \(S(n + 1, m) = 2S(n, m) - C_n^m\)

\(S(n, m)\)可以看做是杨辉三角上的一行,而\(S(n+1, m)\)是他的下一行

考虑组合数的递推公式,除了\(C[n][m]\)这一项之外都会被计算两次、

另外如果有多组询问的话可以用莫队实现

#include<bits/stdc++.h>
using namespace std;
int N, M, Lim, C[1001][1001], S[1001][1001];
int Make1() {
for(int i = 0; i <= Lim; i++) {
C[i][0] = C[i][i] = 1;
for(int j = 1; j < i; j++)
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
return C[N][M];
}
void Make2() {
/*for(int i = 0; i <= Lim; i++) {
S[i][0] = 1;
for(int j = 1; j <= i; j++)
S[i][j] = S[i][j - 1] + C[i][j];
}*/
for(int i = 0, base = 1; i <= Lim; i++, base <<= 1) {
S[i][i] = base;
for(int j = i + 1; j <= Lim; j++)
S[j][i] = 2 * S[j - 1][i] - C[j - 1][i];
}
}
void print(int (*a)[1001]) {
for(int i = 0; i <= Lim; i++, puts("")) {
for(int j = 0; j <= i; j++)
printf("%d ", S[i][j]);
}
}
main() {
Lim = 10;
//cin >> N >> M;
Make1();
Make2();
print(S); }

一种递推组合数前缀和的Trick的更多相关文章

  1. bzoj3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——递推 / 组合数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3398 对于这种有点巧妙的递推还是总是没有思路... 设计一个状态 f[i] 表示第 i 位置 ...

  2. BZOJ2339[HNOI2011]卡农——递推+组合数

    题目链接: [HNOI2011]卡农 题目要求从$S=\{1,2,3……n\}$中选出$m$个子集满足以下三个条件: 1.不能选空集 2.不能选相同的两个子集 3.每种元素出现次数必须为偶数次 我们考 ...

  3. Codeforces Round #271 (Div. 2)D(递推,前缀和)

    很简单的递推题.d[n]=d[n-1]+d[n-k] 注意每次输入a和b时,如果每次都累加,就做了很多重复性工作,会超时. 所以用预处理前缀和来解决重复累加问题. 最后一个细节坑了我多次: print ...

  4. [uva11174]村民排队 递推+组合数+线性求逆元

    n(n<=40000)个村民排成一列,每个人不能排在自己父亲的前面,有些人的父亲不一定在.问有多少种方案. 父子关系组成一个森林,加一个虚拟根rt,转化成一棵树. 假设f[i]表示以i为根的子树 ...

  5. HDU 4869 (递推 组合数取模)

    Problem Turn the pokers (HDU 4869) 题目大意 有m张牌,全为正面朝上.进行n次操作,每次可以将任意ai张反面,询问n次操作可能的状态数. 解题分析 记正面朝上为1,朝 ...

  6. The King’s Ups and Downs(HDU 4489,动态规划递推,组合数,国王的游戏)

    题意: 给一个数字n,让1到n的所有数都以波浪形排序,即任意两个相邻的数都是一高一低或者一低一高 比如:1324   4231,再比如4213就是错的,因为4高,2低,接下来1就应该比2高,但是它没有 ...

  7. Codeforces 631 (Div. 2) D. Dreamoon Likes Sequences 位运算^ 组合数 递推

    https://codeforces.com/contest/1330/problem/D 给出d,m, 找到一个a数组,满足以下要求: a数组的长度为n,n≥1; 1≤a1<a2<⋯&l ...

  8. UVa 12034 (递推) Race

    题意: 有n个人赛马,名次可能并列,求一共有多少种可能. 分析: 设所求为f(n),假设并列第一名有i个人,则共有C(n, i)种可能,接下来确定后面的名次,共有f(n-1)种可能 所以递推关系为: ...

  9. 【高精度递推】【HDU1297】Children’s Queue

    Children's Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. vue项目实践-前后端分离关于权限的思路

    前后端分离后权限的思路 最近看到许多关于权限的思路,但好像都是使用动态加载路由的方式,现在也分享下我在项目中使用的解决方案. 前后端分离关于权限的处理每个人都不一样,根据项目选择制定合适的方案就好 我 ...

  2. 【RL-TCPnet网络教程】第28章 RL-TCPnet之DNS应用

    第28章      RL-TCPnet之DNS应用 本章节为大家讲解RL-TCPnet的DNS应用,学习本章节前,务必要优先学习第27章的DNS基础知识.有了这些基础知识之后,再搞本章节会有事半功倍的 ...

  3. 树的简介及Java实现

    一.树的基本知识 树是一种数据结构,它是由n(n>=1)个有限结点组成一个具有层次关系的集合.把它叫做“树”是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的.它具有以下的特点:每个结 ...

  4. macOS 10.14虚拟机安装教程

    windows10下安装vmware14.0以及macOS 10.14图文详解 工具/原料   windows10 vmware 14.0 macOS 10.14懒人版 mac补丁unlocker工具 ...

  5. [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  7. [Swift]LeetCode334. 递增的三元子序列 | Increasing Triplet Subsequence

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  8. [Swift]LeetCode669. 修剪二叉搜索树 | Trim a Binary Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  9. [Swift]LeetCode713. 乘积小于K的子数组 | Subarray Product Less Than K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  10. [Swift]LeetCode826. 安排工作以达到最大收益 | Most Profit Assigning Work

    We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith ...