baisc.hpp

powMod

int powMod(int x, int n, int M)

Constraints

Complexity

floor and ceil

template<typename T>
T floor(T a, T n)
template<typename T>
T ceil(T a, T n)

Constraints

  • can be (unsigned) int, LL an so on

Complexity

FastIO class: input and output

T FastIO::read()
void FastIO::print()

Constraints

  • Never use it with cin and cout

Complexity

gcd

// Binary GCD: slightly faster than std::gcd
LL gcd(LL a, LL b)

Complexity

Reference

exGcd

template<typename T>
std::tuple<T, T, T> exGcd(T a, T b)
// auto [d, x, y] = exGcd(a, b)

where

Constraints

  • can be (unsigned) int, long long

Complexity

crt2

std::pair<LL, LL> crt2(LL a1, LL m1, LL a2, LL m2)
// auto [a, m] = crt2(a1, m1, a2, m2)

The Chinese Remainder Theorem shows that equivalence to Constraints

  • is in LL

Complexity

crt

std::pair<LL, LL> crt(const std::vector<std::pair<LL, LL>>& A)
// n = (int)A.size(), a[i] = A[i].first, m[i] = A[i].second,

use crt2 above times, we have

equivalence to Constraints

  • is in LL

Complexity

Dependence

  • crt2

spf (foundmental important)

std::vector<int> spf(int N)
// auto sp = spf(N)

where sp[x] is smallest prime factor of

Complexity

std::vector<int> spfS(int N)
// auto sp = spfS(N)

Complexity

non square factor

std::vector<int> nsf(int N);
// auto sf = nsf(N) // sf[i] = sf[i * j * j]

Complexity

Binom

It is a const singleton class with static const int number , and .

exmaple

Binom C;
std::cout << C(4, 2) << '\n'; // the answer is 6

BinomModp

It is a ~~singleton~~ template class, with typename valT

since valT::mod() may change in progress, It is not wise to use singleton

Members and Methods

  • fac, ifac, inv
  • if , 0 else.

Constraints

  • valT should be MInt, ModInt or ModLL defined in mod.hpp

Complexity

Lagrange

template<typename valT>
valT Lagrange(const std::vector<valT>& f, int m)

Calculate where is the Lagrange interpolation on

Complexity

Dependence

  • BinomModp

powSum

valT powSum(int n, int k, const std::vector<int>& sp)

, wheresp[x] is smallest prime factor of

Complexity

Dependence

  • Lagrange
  • spf

Matrix

It is a class template, contains matrix add, multiplication, and pow

Complexity

  • +:
  • *:
  • pow(A, n):

MEX

It is a class contains static const int and set , you can Insert/erase element to , and

Constraints

Complexity

MEXS

the maximal value should not too large

trans

transform vector<int> to vector<valT>

Complexity

BerlekampMassey(every useful)

static std::vector<valT> BerlekampMassey(const std::vector<valT>& a)

It return shortest recursive relational formula of .

Complexity

Example

  • BerlekampMassey({1, 2, 4, 8, 16}) = {2}
  • BerlekampMassey({1, 1, 2, 3, 5}) = {1, 1}