acm常用小东西

STL::algorithm

函数大全
https://wenku.baidu.com/view/fd36a329e87101f69e3195a4.html 有一些笔误:(

默认的仿函数

要使用STL内建的仿函数,必须包含头文件。而头文件中包含的仿函数分类包括

运算

加:plus
减:minus
乘:multiplies
除:divides
模取:modulus
否定:negate

比较

等于:equal_to
不等于:not_equal_to
大于:greater
大于等于:greater_equal
小于:less
小于等于:less_equal

逻辑运算

逻辑与:logical_and
逻辑或:logical_or
逻辑否:logical_not

使用频率比较高的函数

运算类

求和 c++ accumulate(beg,end,0,multiplies<ll>());#include <numeric>
__gcd在里面,作为运算子传递的时候,要加模板参数__gcd
下一个排列 next_permutation
上一个排列 prev_permutation

判断类

判断是否排序c++ is_sorted(beg,end,cmp);
判断是否字典序小于c++ lexicographical_compare(beg1,end1,beg2,end2,cmp)

统计类

统计元素个数 c++ count(beg,end,val)
二分下界lower_bound(beg,end,val,cmp)
二分上界upper_bound(beg,end,val,cmp)

填充类

fill(beg,end,val)
fill_n(beg,n,val)
ll i = 10;generate(v.begin(),v.end(),[&](){i++;return i*i;});

其他

排序 c++ sort(beg,end,cmp)
旋转c++ rotate(beg,mid,end)
翻转c++ reverse(beg,end)
重排数组,使得第kth位置刚好是第kth大数 c++ nth_element(v.begin(),v.begin() + kth,v.end(),cmp);
随机打乱 random_shuffle(beg,end)
前缀和partial_sum(v.begin(),v.end(),v.begin());
差分 adjacent_difference(v.begin(),v.end(),v.begin());

乘法逆元

ll f[5000000 + 100];
void pre(ll n,ll mod)
  {
  	 f[1] = 1;
  	 for (ll i = 2;i <= n;i++)
  	   f[i] = (mod - mod / i) * f[mod % i] % mod;
  }
ll inv(ll n,ll mod)
  {
  	if (n == 0)
  	  return 0;
  	else if (n == 1)
  	  return 1;
  	else
	  return (mod - mod / n) * inv(mod % n,mod) % mod;
  }

int128 + 快读快写

namespace io
  {
    template <typename T>
    inline void _w(T x) { if (x > 9) _w(x / 10); putchar(x % 10 + 48); }
    template <typename T>
    inline void w(T x, char c = 0) { if(x < 0) putchar('-'), x = -x; _w(x); if (c) putchar(c); }
    template <typename T>
    inline T r() 
	  {
        T x = 0; T f = 1; char c = getchar();
        for (; !isdigit(c); c = getchar()) if (c == '-') f = -1;
        for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
        x *= f;
        return x;
	 }
};

随机数

#include <random>
#include <chrono>
int r(int star,int fini) {
  	static std::mt19937 rand_num(std::chrono::system_clock::now().time_since_epoch().count()); 
	std::uniform_int_distribution<int> dist(star,fini);  // 给定范围
	return dist(rand_num);
}

快速幂,O1快读乘

ll mul(ll x,ll y,ll mod = mod)
  {
  	x %= mod;
  	y %= mod;
  	ll ret = (x * y-(ll)(((long double)x * y + 0.5) / mod) * mod) % mod;
	return ret < 0 ? ret + mod : ret;
  }
ll qp(ll a,ll b = mod - 2,ll mod = mod)
  {
  	ll ret = 1;
  	a %= mod;
  	while (b)
  	  {
  	  	if (b & 1)
  	  	  ret = ret * a % mod;
  	  	b >>= 1;
  	  	a = a * a % mod;
		}
	return ret;
   } 
posted @ 2021-08-30 15:37  XDU18清欢  阅读(48)  评论(0)    收藏  举报