除了IO都很短。
IO
namespace IO {
char buf[1 << 20], *_now = buf, *_end = buf;
inline char getchar() {
return _now == _end && (_end = (_now = buf) + fread(buf, 1, 1 << 20, stdin), _now == _end) ? EOF : *_now++;
}
template <typename T>
inline void read(T &x) {
x = 0;
bool fl = 0;
char ch = getchar();
for (; !isdigit(ch); fl |= (ch == '-'), ch = getchar());
for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar());
x = (fl ? -x : x);
}
template <typename T>
inline void write(T x) {
if (x < 0) x = -x, putchar('-');
static short stk[50], top = 0;
for (; x; stk[++top] = x % 10, x /= 10);
for (; top; top--) putchar(stk[top] | 48);
}
inline char read(char &ch) { return ch = getchar(); }
inline void write(char ch) { putchar(ch); }
template <typename type, typename... T>
inline void read(type &x, T &...y) { read(x), read(y...); }
template <typename type, typename... T>
inline void write(type x, T... y) { write(x), write(' '), write(y...), (sizeof...(y) == 1 && (write('\n'), 0)); }
} // namespace IO
using namespace IO;树状数组
template <class T>
struct BIT {
T c[kMaxN] = {0}, sz = 任意数值, z = 0;
T query(LL x) {//查询
return x ? query(x - (x & -x)) + c[x] : z;
}
void update(LL x, T s) {//修改
x <= sz && (update(x + (x & -x), s), c[x] += s);
}
};快速幂
template <typename T>
T qpow(T a, T n, T kM, T r = 1) {
for (T i = 1; i <= n; n & i && (r = r * a % kM), a = a * a % kM, i <<= 1);
return r;
}Barrett约减
namespace Barrett {
const int kM = 1e9 + 7;
const __int128 mu = -1ull / kM;
inline LL Mod(LL x) {
LL r = x - (mu * x >> 64) * kM;
return r >= kM ? r - kM : r;
}
} using namespace Barrett;