排列组合
分类加法计数原理
完成一个工程可以有 n 类办法,代表第 i 类方法的数目。那么完成这件事共有种不同的方法。
分步乘法计数原理
完成一个工程需要分 n 个步骤,代表第 i 个步骤的不同方法数目。那么完成这件事共有 种不同的方法。
排列与组合基础
排列数
从 n 个不同元素中,任取 (, 与 均为自然数,下同)个元素按照一定的顺序排成一列,叫做从个不同元素中取出个元素的一个排列;从个不同元素中取出 个元素的所有排列的个数,叫做从个不同元素中取出 m 个元素的排列数,用符号 或者是 表示。 排列的计算公式如下:
组合数
从 n 个不同元素中,任取个元素组成一个集合,叫做从个不同元素中取出个元素的一个组合;从 n 个不同元素中取出个元素的所有组合的个数,叫做从不同元素中取出的组合数,用符号 读作「n 选 m」。
组合数计算公式
组合数也被称为「二项式系数」,Day二项式定理3将会阐述其中的联系。
插板法
插板法(Stars and bars)是用于求一类给相同元素分组的方案数的一种技巧,也可以用于求一类线性不定方程的解的组数。
二项式定理
二项式定理阐明了一个展开式的系数:
详细内容见Day 3
杨辉三角
杨辉三角轴对称
杨辉三角数等于肩上两数之和:
其他性质
- 每行数字左右对称,由1开始逐渐变大。
- 第n行的数字有n项。
- 前n行共个数。
- 第n行的m个数可表示为
- 的展开式中的各项系数依次对应杨辉三角的第(n+1)行中的每一项。
- 第n行数字的和为
集合
| 符号 | 释义 |
|---|---|
| A ∪ B | 并集: 在 A 或/和 B 里 |
| A ∩ B | 交集: 在 A 和 B 里 |
| A ⊆ B | 子集:A 含有 B 的一些(或所有)元素 |
| A ⊂ B | 真子集:A 含有 B 的一些元素 |
| A ⊄ B | 非子集:A 不是 B 的子集 |
| A ⊇ B | 超集:A 有 B 的所有元素或更多 |
| A ⊃ B | 真超集:A 有 B 的所有元素及更多 |
| A ⊅ B | 非超集:A 不是 B 的超集 |
| a ∈ A | a 是 A 的元素 |
| b ∉ A | b 不是 A 的元素 |
| ∅ | 空集 |
| ∀ | 对于所有 |
| ∃ | 存在 |
| : | 使得 |
| 整数集 |
题目
solve
OpenJudge
01 递归求和
#include<bits/stdc++.h>
using namespace std;
int f(int x){
if(x==0)return 0;
int r;
cin>>r;
return f(x-1)+r;
}
int main(){
int n;
cin>>n;
cout<<f(n);
return 0;
}02 递归求阶乘
#include<bits/stdc++.h>
using namespace std;
int f(int x){
if(x==0)return 1;
return f(x-1)*x;
}
int main(){
int n;
cin>>n;
cout<<f(n);
return 0;
}03 菲波那契数列
#include<bits/stdc++.h>
using namespace std;
int f(int x){
if(x==1 || x==2)return 1;
return f(x-1)+f(x-2);
}
int main(){
int n,t;
cin>>t;
while(t--){
cin>>n;
cout<<f(n)<<endl;
}
return 0;
}04 在不下降数组中查找X
二分法模板
以在一个升序数组中查找一个数为例。
它每次考察数组当前部分的中间元素,如果中间元素刚好是要找的,就结束搜索过程;如果中间元素小于所查找的值,那么左侧的只会更小,不会有所查找的元素,只需到右侧查找;如果中间元素大于所查找的值同理,只需到左侧查找。
int binary_search(int l, int r, int key){
int ret = -1; // 未搜索到数据返回-1下标
int mid;
while(l <= r) {
mid = (l+r)/2;
if (arr[mid] < key)
l = mid + 1;
else if (arr[mid] > key)
r = mid - 1;
else {
ret = l;//输出第一个值
break;
}
}
return ret;
}前人的智慧
lower_bound(arr,arr+n,key)-a;
upper_bound(arr,arr+n,key)-a;P.S.代码多用这个,风险低
AC代码
#include<iostream>
using namespace std;
int a[100005];
int main() {
int n,t,f,ret;
cin>>n>>t;
for(int i=0; i<n; i++) {
cin>>a[i];
}
while(t--) {
cin>>f;
ret=lower_bound(a,a+n,f)-a;
if(a[ret]!=f)cout<<-1<<endl;
else cout<<ret<<endl;
}
return 0;
}05 Pell数列简单版
#include<iostream>
using namespace std;
int t;
int f(int x){
if(x==1)return 1;
if(x==2)return 2;
t=(2*f(x-1)+f(x-2))%32767;
return t;
}
int main() {
int n,t;
cin>>t;
while(t--){
cin>>n;
cout<<f(n)<<endl;
}
return 0;
}06 求最大公约数问题
欧几里得算法
递归式:
Stein算法
递归式:
前人的智慧
gcd(a,b)//仅限C++17,不推荐使用
代码
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
int main() {
int a,b;
cin>>a>>b;
if(a<b)swap(a,b);
cout<<gcd(a,b);
return 0;
}07 矩形切割
#include<bits/stdc++.h>
using namespace std;
int cnt=0;
int l,w;
int f(int a,int b){
if(a==0 || b==0)return 0;
if(b>a)swap(a,b);
return f(a-b,b)+1;
}
int main(){
cin>>l>>w;
cout<<f(w,l);
return 0;
}08 数字三角形1
#include<bits/stdc++.h>
using namespace std;
int w;
int a[105][105];
int c;
int main(){
cin>>w;
for(int i=1;i<=w;i++){
for(int j=1;j<=i;j++){
cin>>c;
if(i==1)a[i][j]=a[i][j-1]+c;
else if(j==1)a[i][j]=a[i-1][j]+c;
else a[i][j]=max(a[i-1][j],a[i-1][j-1])+c;
}
}
int maxn=-1;
for(int j=1;j<=w;j++){
maxn=max(maxn,a[w][j]);
}
cout<<maxn;
return 0;
}09 最长不下降子序列
思路辨析
设表示以为结尾的最长不下降子序列的长度,则所求为。
计算时,尝试将接到其他的最长不下降子序列后面,以更新答案。于是可以写出这样的状态转移方程:
递归代码
for (int i = 2; i <= n; i++) {
d[i] = 1;
for (int j = 1; j < i; j++){
if (a[j] <= a[i]) {//满足条件
d[i] = max(d[i], d[j] + 1);
ans = max(ans, d[i]);
}
}
}AC代码
#include<bits/stdc++.h>
using namespace std;
int a[15],d[15];
int main() {
int n,ans=-1;
cin>>n;
d[0]=1;
for(int i=0; i<n; i++)cin>>a[i];
for (int i = 1; i < n; i++) {
d[i] = 1;
for (int j = 0; j < i; j++) {
if (a[j] <= a[i]) {
d[i] = max(d[i], d[j] + 1);
ans = max(ans, d[i]);
}
}
}
cout<<ans;
return 0;
}10 汉诺塔问题
详细解析见:信息学奥赛一本通关P558
#include<bits/stdc++.h>
using namespace std;
int sum=0;
void hanoi(int n,char x,char y, char z){
sum++;
if(n==1)printf("%c->1->%c\n",x,z);
else{
hanoi(n-1,x,z,y);
printf("%c->%d->%c\n",x,n,z);
hanoi(n-1,y,x,z);
}
}
int main() {
int n;
char x,y,z;
cin>>n>>x>>y>>z;
hanoi(n,x,z,y);
return 0;
}Made By Yuyhang
