Post

c++小技巧

c++小技巧
1
#include<bits/stdc++.h>
1
2
ios::sync_with_stdio(false);
cin.tie(nullptr);

string

截取字符串

1
string c = s.substr(pos, len);

翻转字符串

1
reverse(s.begin(), s.end());

字符串区间删除

1
2
s.erase(pos,n);  //删除从pos开始的n个字符
s.erase(8);  //如果pos不是迭代器位置,则删除该位置及之后的所有字符

位运算

1
2
3
x & 1 == 1  //x是奇数

int u = x >> k & 1;   

杂项

ASCII

  • ‘0’: 48
  • ‘A’: 65
  • ‘a’: 97

数学

求多个组合数的一种方法

阶乘 fact[N] 逆元 inv[N]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ll fact[N], inv[N], invfact[N];
ll C(int n, int k){
    if(k < 0 && k > n){
        return 0;
    }
    return fact[n] * invfact[k] % mod * invfact[n - k] % mod;
}
int main(){
    fact[0] = invfact[0] = 1;
    for(int i = 1; i < N; i ++){
        inv[i] = (i == 1) ? 1 : mod - (mod / i) * inv[mod % i] % mod;  //线性求逆元  //1~n的逆元
        fact[i] = fact[i - 1] * i % mod;
        invfact[i] = invfact[i - 1] * inv[i] % mod;
    }
}
This post is licensed under CC BY 4.0 by the author.

Trending Tags