一种递推组合数前缀和的Trick
记录一下一种推组合数前缀和的方法
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的更多相关文章
- bzoj3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——递推 / 组合数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3398 对于这种有点巧妙的递推还是总是没有思路... 设计一个状态 f[i] 表示第 i 位置 ...
- BZOJ2339[HNOI2011]卡农——递推+组合数
题目链接: [HNOI2011]卡农 题目要求从$S=\{1,2,3……n\}$中选出$m$个子集满足以下三个条件: 1.不能选空集 2.不能选相同的两个子集 3.每种元素出现次数必须为偶数次 我们考 ...
- Codeforces Round #271 (Div. 2)D(递推,前缀和)
很简单的递推题.d[n]=d[n-1]+d[n-k] 注意每次输入a和b时,如果每次都累加,就做了很多重复性工作,会超时. 所以用预处理前缀和来解决重复累加问题. 最后一个细节坑了我多次: print ...
- [uva11174]村民排队 递推+组合数+线性求逆元
n(n<=40000)个村民排成一列,每个人不能排在自己父亲的前面,有些人的父亲不一定在.问有多少种方案. 父子关系组成一个森林,加一个虚拟根rt,转化成一棵树. 假设f[i]表示以i为根的子树 ...
- HDU 4869 (递推 组合数取模)
Problem Turn the pokers (HDU 4869) 题目大意 有m张牌,全为正面朝上.进行n次操作,每次可以将任意ai张反面,询问n次操作可能的状态数. 解题分析 记正面朝上为1,朝 ...
- The King’s Ups and Downs(HDU 4489,动态规划递推,组合数,国王的游戏)
题意: 给一个数字n,让1到n的所有数都以波浪形排序,即任意两个相邻的数都是一高一低或者一低一高 比如:1324 4231,再比如4213就是错的,因为4高,2低,接下来1就应该比2高,但是它没有 ...
- 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 ...
- UVa 12034 (递推) Race
题意: 有n个人赛马,名次可能并列,求一共有多少种可能. 分析: 设所求为f(n),假设并列第一名有i个人,则共有C(n, i)种可能,接下来确定后面的名次,共有f(n-1)种可能 所以递推关系为: ...
- 【高精度递推】【HDU1297】Children’s Queue
Children's Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- HBuilder git合作-从Git Hub Clone项目
1.Clone项目 打开”Git Respository"视图,选“Clone a Git Respository" 2.为了能正确pull项目,所有队员都必须做以下配置(其始只是 ...
- 企业IT管理员IE11升级指南【14】—— IE11代理服务器配置
企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...
- [Swift]LeetCode77. 组合 | Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- [Swift]LeetCode112. 路径总和 | Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [Swift]LeetCode373. 查找和最小的K对数字 | Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- [Swift]LeetCode447. 回旋镖的数量 | Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- [Swift]LeetCode959. 由斜杠划分区域 | Regions Cut By Slashes
In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space. Th ...
- linux系统安装cdcfordb2udb
最近接触到db2数据库实时复制的解决方案InfoSphere CDC(Change Database Capture) .主要是通过读取源端的日志信息对目标端进行数据的增删改,从而尽量减少对源端资源的 ...
- zookeeper实现项目初始化缓存以及同步监听
Spring-利用InitializingBean接口和zookeeper实现项目初始化缓存以及同步监听 1.先贴出几个需要用到的工具类 ZkClientUtils import com.ithzk. ...
- Python内置函数(37)——len
英文文档: len(s) Return the length (the number of items) of an object. The argument may be a sequence (s ...