代码模板
// Code By Jason331
#include <bits/stdc++.h>
using namespace std;
#define heap priority_queue
#define uset unordered_set
#define umap unordered_map
// #include <bits/extc++.h>
// #define map gp_hash_table
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned int u32;
typedef unsigned long long ull;
typedef long double lf;
typedef __int128_t i128;
typedef __float128 llf;
#define CONST __attribute__((const))
#define PURE __attribute__((pure))
#define PACKED __attribute__((packed))
#define START __attribute__((constructor))
#define told __builtin_expect
signed main()
{
return 0;
}
快读
膜拜(目前)天下第一快读:link
本人常用的快读模板:
namespace fast_read
{
inline char getc()
{
static constexpr int SIZ = 1 << 16;
static char buf[SIZ],*begin = buf,*end = buf;
if(begin == end)
end = (begin = buf) + fread(buf,1,SIZ,stdin);
return *begin++;
}
inline void read() {}
template <class T>
inline void read(T &x)
{
char c;
for(c = getc();c < '0';c = getc());
for(x = 0;c >= '0';c = getc())
x = x * 10 + (c ^ '0');
}
template <class T,class... types>
inline void read(T &x,types&... other)
{
read(x);
read(other...);
}
}火车头
警告:洛谷无法使用,也不可以在赛场上使用。
#pragma GCC optimize(2,3,"-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,-fpeephole2,-ftree-vrp,-ftree-pre,-fipa-sra,-fgcse-lm,-fgcse,inline,Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")快速除法 / 取模(Barrett 约减)
以下所说的除法中,指的是向下取整的除法,即 C++ 中整数与整数之间的除法。
Barrett 约减通过实现固定除数的除法来实现固定模数的取模。
算法思想
假设我们的除数是 ,我们知道对于任意实数 ,有
当我们选取 为 的正整数次幂时,我们便可以通过位移来实现以 为除数的除法。于是我们只需要预处理 ,就可以只用乘法和位移实现除法了。
误差分析
但是 C++ 中的除法向下取整,显然会有误差,我们需要选择合适的 使得误差在可以接受的范围内。
在使用向下取整的除法时, 相对于 的误差为 ,将这个误差带入上面的式子可以得到: 相对于 的误差为 。
那么在使用这个式子计算 时,误差应该是 ,由取整可以知道,误差必定是 的倍数。
一般实现时选取 ,这样就能保证取模结果最多误差 ,只需要在计算完后判断结果 ,如果 ,将 赋值为 即可。
实现
需要用到 __int128。
#define PURE __attribute__((pure))
typedef __int128 i128;
constexpr int R = 75;
struct mode//Barrett
{
i128 __R,mod;
mode(){}
mode(int _mod)
{
mod = _mod;
__R = ((i128)1 << R) / _mod;
}
mode(const mode &a)
{
__R = a.__R;
mod = a.mod;
}
PURE friend const ll operator %(i128 x,mode xmod)
{
ll ans = x - (xmod.__R * x >> R) * xmod.mod;
ans = ans - (xmod.__R * ans >> R) * xmod.mod;
//会发现这里因为 __int128 太大了,可能误差不止一个 mod,所以需要做两次。
return ans >= xmod.mod?ans - xmod.mod:ans;
}
template <typename T>
PURE friend const ll operator %(T x,mode xmod)
{
int ans = x - (xmod.__R * x >> R) * xmod.mod;
return ans >= xmod.mod?ans - xmod.mod:ans;
}
};属性(attribute)
C++ 中的属性(attribute)是