compro_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub siro53/compro_library

:heavy_check_mark: test/mytest/math/arithmetic-regressions.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include "../../../template/template.cpp"
#include "../../../math/segment-sieve.hpp"
#include "../../../math/is-prime.hpp"
#include "../../../math/barrett-reduction.hpp"
#include "../../../math/binom.hpp"
#include "../../../modint/modint.hpp"
#include "../../../modint/dynamic-modint.hpp"
#include "../../../math/F2.hpp"

void regression_test() {
    for(int l = 0; l <= 300; l++) for(int width : {0, 1, 2, 30, 100}) {
        SegmentSieve s(l, l + width);
        for(int x = l; x < l + width; x++) assert(s[x] == is_prime(x));
    }
    SegmentSieve big(10000000000LL, 10000000100LL);
    for(long long x = 10000000000LL; x < 10000000100LL; x++) assert(big[x] == is_prime(x));
    BarrettReduction one;
    for(unsigned x : {0U, 1U, 123U, UINT_MAX}) {
        assert(one.quo(x) == x && one.rem(x) == 0);
        assert(one.quorem(x) == make_pair(x, 0U));
    }
    assert(one.rem(ULLONG_MAX) == 0);
    assert(pow_mod(2, 0, 1) == 0 && pow_mod(-2, 3, 5) == 2);
    Binomial<ModInt<7>> b;
    assert(b.inv(-2).val() == 3 && b.inv(2).val() == 4);
    DynamicModInt::set_mod(7);
    for(int i = 0; i < 7; i++) assert((-DynamicModInt(i) + DynamicModInt(i)).val() == 0);
    BitMatrix<2> mat(2, 2);
    mat[0][0] = mat[1][1] = true;
    auto solution = linear_equation_F2(mat, vector<int>{1, 0});
    assert(solution.first == 2 && solution.second == vector<int>({1, 0}));
}

int main() {
    regression_test();
    INT(a, b);
    print(a + b);
}
#line 1 "test/mytest/math/arithmetic-regressions.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 1 "template/template.cpp"
// #region Macros
#include <bits/stdc++.h>
using namespace std;
// input output utils
namespace siro53_io {
    // https://maspypy.github.io/library/other/io_old.hpp
    struct has_val_impl {
        template <class T>
        static auto check(T &&x) -> decltype(x.val(), std::true_type{});

        template <class T> static auto check(...) -> std::false_type;
    };

    template <class T>
    class has_val : public decltype(has_val_impl::check<T>(std::declval<T>())) {
    };

    // debug
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void dump(const T t) {
        cerr << t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void dump(const T t) {
        cerr << t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void dump(const T &t) {
        cerr << t.val();
    }
    void dump(__int128_t n) {
        if(n == 0) {
            cerr << '0';
            return;
        } else if(n < 0) {
            cerr << '-';
            n = -n;
        }
        string s;
        while(n > 0) {
            s += (char)('0' + n % 10);
            n /= 10;
        }
        reverse(s.begin(), s.end());
        cerr << s;
    }
    void dump(const string &s) { cerr << s; }
    void dump(const char *s) {
        int n = (int)strlen(s);
        for(int i = 0; i < n; i++) cerr << s[i];
    }
    template <class T1, class T2> void dump(const pair<T1, T2> &p) {
        cerr << '(';
        dump(p.first);
        cerr << ',';
        dump(p.second);
        cerr << ')';
    }
    template <class T> void dump(const vector<T> &v) {
        cerr << '{';
        for(int i = 0; i < (int)v.size(); i++) {
            dump(v[i]);
            if(i < (int)v.size() - 1) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(const set<T> &s) {
        cerr << '{';
        for(auto it = s.begin(); it != s.end(); it++) {
            dump(*it);
            if(next(it) != s.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class Key, class Value> void dump(const map<Key, Value> &mp) {
        cerr << '{';
        for(auto it = mp.begin(); it != mp.end(); it++) {
            dump(*it);
            if(next(it) != mp.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class Key, class Value>
    void dump(const unordered_map<Key, Value> &mp) {
        cerr << '{';
        for(auto it = mp.begin(); it != mp.end(); it++) {
            dump(*it);
            if(next(it) != mp.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(const deque<T> &v) {
        cerr << '{';
        for(int i = 0; i < (int)v.size(); i++) {
            dump(v[i]);
            if(i < (int)v.size() - 1) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(queue<T> q) {
        cerr << '{';
        while(!q.empty()) {
            dump(q.front());
            if((int)q.size() > 1) cerr << ',';
            q.pop();
        }
        cerr << '}';
    }

    void debug_print() { cerr << endl; }
    template <class Head, class... Tail>
    void debug_print(const Head &h, const Tail &...t) {
        dump(h);
        if(sizeof...(Tail)) dump(' ');
        debug_print(t...);
    }
    // print
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void print_single(const T t) {
        cout << t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void print_single(const T t) {
        cout << t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void print_single(const T t) {
        cout << t.val();
    }
    void print_single(__int128_t n) {
        if(n == 0) {
            cout << '0';
            return;
        } else if(n < 0) {
            cout << '-';
            n = -n;
        }
        string s;
        while(n > 0) {
            s += (char)('0' + n % 10);
            n /= 10;
        }
        reverse(s.begin(), s.end());
        cout << s;
    }
    void print_single(const string &s) { cout << s; }
    void print_single(const char *s) {
        int n = (int)strlen(s);
        for(int i = 0; i < n; i++) cout << s[i];
    }
    template <class T1, class T2> void print_single(const pair<T1, T2> &p) {
        print_single(p.first);
        cout << ' ';
        print_single(p.second);
    }
    template <class T> void print_single(const vector<T> &v) {
        for(int i = 0; i < (int)v.size(); i++) {
            print_single(v[i]);
            if(i < (int)v.size() - 1) cout << ' ';
        }
    }
    template <class T> void print_single(const set<T> &s) {
        for(auto it = s.begin(); it != s.end(); it++) {
            print_single(*it);
            if(next(it) != s.end()) cout << ' ';
        }
    }
    template <class T> void print_single(const deque<T> &v) {
        for(int i = 0; i < (int)v.size(); i++) {
            print_single(v[i]);
            if(i < (int)v.size() - 1) cout << ' ';
        }
    }
    template <class T> void print_single(queue<T> q) {
        while(!q.empty()) {
            print_single(q.front());
            if((int)q.size() > 1) cout << ' ';
            q.pop();
        }
    }

    void print() { cout << '\n'; }
    template <class Head, class... Tail>
    void print(const Head &h, const Tail &...t) {
        print_single(h);
        if(sizeof...(Tail)) print_single(' ');
        print(t...);
    }

    // input
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void input_single(T &t) {
        cin >> t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void input_single(T &t) {
        cin >> t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void input_single(T &t) {
        cin >> t;
    }
    void input_single(__int128_t &n) {
        string s;
        cin >> s;
        if(s == "0") {
            n = 0;
            return;
        }
        bool is_minus = false;
        if(s[0] == '-') {
            s = s.substr(1);
            is_minus = true;
        }
        n = 0;
        for(int i = 0; i < (int)s.size(); i++) n = n * 10 + (int)(s[i] - '0');
        if(is_minus) n = -n;
    }
    void input_single(string &s) { cin >> s; }
    template <class T1, class T2> void input_single(pair<T1, T2> &p) {
        input_single(p.first);
        input_single(p.second);
    }
    template <class T> void input_single(vector<T> &v) {
        for(auto &e : v) input_single(e);
    }
    void input() {}
    template <class Head, class... Tail> void input(Head &h, Tail &...t) {
        input_single(h);
        input(t...);
    }
}; // namespace siro53_io
#ifdef DEBUG
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debug_print(__VA_ARGS__)
#else
#define debug(...) (void(0))
#endif
// io setup
struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;
using namespace siro53_io;
// types
using ll = long long;
using i128 = __int128_t;
// input macros
#define INT(...)                                                               \
    int __VA_ARGS__;                                                           \
    input(__VA_ARGS__)
#define LL(...)                                                                \
    ll __VA_ARGS__;                                                            \
    input(__VA_ARGS__)
#define STRING(...)                                                            \
    string __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define CHAR(...)                                                              \
    char __VA_ARGS__;                                                          \
    input(__VA_ARGS__)
#define DBL(...)                                                               \
    double __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define LD(...)                                                                \
    long double __VA_ARGS__;                                                   \
    input(__VA_ARGS__)
#define UINT(...)                                                              \
    unsigned int __VA_ARGS__;                                                  \
    input(__VA_ARGS__)
#define ULL(...)                                                               \
    unsigned long long __VA_ARGS__;                                            \
    input(__VA_ARGS__)
#define VEC(name, type, len)                                                   \
    vector<type> name(len);                                                    \
    input(name);
#define VEC2(name, type, len1, len2)                                           \
    vector name(len1, vector<type>(len2));                                     \
    input(name);
// other macros
// https://trap.jp/post/1224/
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < int(n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(RALL(v))
#define UNIQUE(v)                                                              \
    sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()), v.shrink_to_fit()
#define REV(v) reverse(ALL(v))
#define SZ(v) ((int)(v).size())
#define MIN(v) (*min_element(ALL(v)))
#define MAX(v) (*max_element(ALL(v)))
// util const
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// util functions
void Case(int i) { cout << "Case #" << i << ": "; }
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
template <class T> inline bool chmax(T &a, T b) {
    return (a < b ? a = b, true : false);
}
template <class T> inline bool chmin(T &a, T b) {
    return (a > b ? a = b, true : false);
}
template <class T, int dim>
auto make_vector_impl(vector<int>& sizes, const T &e) {
    if constexpr(dim == 1) {
        return vector(sizes[0], e);
    } else {
        int n = sizes[dim - 1];
        sizes.pop_back();
        return vector(n, make_vector_impl<T, dim - 1>(sizes, e));
    }
}
template <class T, int dim>
auto make_vector(const int (&sizes)[dim], const T &e = T()) {
    vector<int> s(dim);
    for(int i = 0; i < dim; i++) s[i] = sizes[dim - i - 1];
    return make_vector_impl<T, dim>(s, e);
}
vector<int> iota_gen(int n, int start = 0) {
    vector<int> ord(n);
    iota(ord.begin(), ord.end(), start);
    return ord;
}
template<typename T>
vector<int> ord_sort(const vector<T>& v, bool greater = false) {
    auto ord = iota_gen((int)v.size());
    sort(ALL(ord), [&](int i, int j) {
        if(greater) return v[i] > v[j];
        return v[i] < v[j];
    });
    return ord;
}
// #endregion Macros
#line 2 "math/segment-sieve.hpp"

#line 7 "math/segment-sieve.hpp"

class SegmentSieve {
  public:
    explicit SegmentSieve(long long L, long long R)
        : L(L), R(R), sq(std::sqrt((long double)R) + 1) {
        assert(0 <= L && L <= R);
        small.assign(sq, true);
        large.assign(R - L, true);
        for(long long n = L; n < R && n < 2; n++) large[n - L] = false;
        for(long long i = 2; i < sq && i <= (R - 1) / i; i++) {
            if(!small[i]) continue;
            for(long long j = i * i; j < sq; j += i) small[j] = false;
            long long first = L / i + (L % i != 0);
            if(first > (R - 1) / i) continue;
            long long start = std::max(first * i, i * i);
            for(long long j = start; j < R;) {
                large[j - L] = false;
                if(R - 1 - j < i) break;
                j += i;
            }
        }
    }
    bool operator[](long long k) const {
        assert(L <= k && k < R);
        return large[k - L];
    }

  private:
    long long L, R;
    long long sq;
    std::vector<bool> small, large;
};
#line 2 "math/is-prime.hpp"

#line 4 "math/is-prime.hpp"

#line 2 "math/pow_mod.hpp"

constexpr long long pow_mod(long long x, long long k, long long m) {
    long long res = 1 % m;
    long long mul = (x >= 0 ? x % m : x % m + m);
    while(k) {
        if(k & 1) res = (__int128_t)res * mul % m;
        mul = (__int128_t)mul * mul % m;
        k >>= 1;
    }
    return res;
}
#line 6 "math/is-prime.hpp"

/*
ref: Fast Primality Testing for Integers That Fit into a Machine Word
Michal Foriˇsek and Jakub Janˇcina
*/

constexpr bool is_prime(int n) {
    if(n <= 1) return false;
    if(n == 2 or n == 7 or n == 61) return true;
    if((n & 1) == 0) return false;
    long long d = n - 1;
    while((d & 1) == 0) d >>= 1;
    constexpr std::array<int, 3> bases = {2, 7, 61};
    for(int a : bases) {
        long long t = d;
        long long y = pow_mod(a, t, n);
        while(t != n - 1 && y != 1 && y != n - 1) {
            (y *= y) %= n;
            t <<= 1;
        }
        if(y != n - 1 && (t & 1) == 0) return false;
    }
    return true;
}

constexpr bool is_prime(long long n) {
    if(n <= 1) return false;
    if(n == 2) return true;
    if((n & 1) == 0) return false;
    long long d = n - 1;
    while((d & 1) == 0) d >>= 1;
    constexpr std::array<long long, 7> bases = {
        2, 325, 9375, 28178, 450775, 9780504, 1795265022
    };
    for(long long a : bases) {
        a %= n;
        if(a == 0) continue;
        long long t = d;
        long long y = pow_mod(a, t, n);
        while(t != n - 1 && y != 1 && y != n - 1) {
            y = (__int128_t)y * y % n;
            t <<= 1;
        }
        if(y != n - 1 && (t & 1) == 0) return false;
    }
    return true;
}
#line 2 "math/barrett-reduction.hpp"

#line 4 "math/barrett-reduction.hpp"

class BarrettReduction {
public:
    BarrettReduction(): BarrettReduction(1) {}
    BarrettReduction(unsigned int m)
        : m(m), m_inv((unsigned long long)(-1) / m + 1) {}
    unsigned int mul(unsigned int a, unsigned int b) const {
        unsigned long long z = (unsigned long long)a * b;
        unsigned long long v =  (unsigned long long)(((__uint128_t)z * m_inv) >> 64);
        unsigned int result = (unsigned int)(z - v * m);
        if(m <= result) result += m;
        return result;
    }
    unsigned int quo(unsigned int a) const {
        if(m == 1) return a;
        unsigned int result = (unsigned int)(((__uint128_t)a * m_inv) >> 64);
        return result;
    }
    unsigned int rem(unsigned long long a) const {
        if(m == 1) return 0;
        unsigned long long v = (unsigned long long)(((__uint128_t)a * m_inv) >> 64);
        unsigned int result = (unsigned int)(a - v * m);
        if(m <= result) result += m;
        return result;
    }
    std::pair<unsigned int, unsigned int> quorem(unsigned int a) const {
        if(m == 1) return {a, 0};
        unsigned long long v =  (unsigned long long)(((__uint128_t)a * m_inv) >> 64);
        unsigned int r = (unsigned int)((unsigned long long)a - v * m);
        if(m <= r) r += m;
        return {v, r};
    }
private:
    unsigned int m;
    unsigned long long m_inv;
};
#line 2 "math/binom.hpp"

#line 6 "math/binom.hpp"

template <class mint> class Binomial {
  public:
    explicit Binomial(): Binomial(1) {}
    explicit Binomial(int MAX) : f(MAX, mint(1)), f_inv(MAX, mint(1)) {
        for(int i = 1; i < MAX; i++) f[i] = f[i-1] * mint(i);
        f_inv[MAX - 1] = f[MAX - 1].inv();
        for(int i = MAX - 2; i >= 1; i--) {
            f_inv[i] = f_inv[i + 1] * mint(i + 1);
        } 
    }
    void extend() {
        int n = (int)f.size();
        f.resize(n * 2);
        f_inv.resize(n * 2);
        for(int i = n; i < n * 2; i++) f[i] = f[i - 1] * mint(i);
        f_inv[n * 2 - 1] = f[n * 2 - 1].inv();
        for(int i = n * 2 - 2; i >= n; i--) {
            f_inv[i] = f_inv[i + 1] * mint(i + 1);
        }
    }
    mint fac(int n) {
        if(n < 0) return mint(0);
        while(n >= (int)f.size()) extend();
        return f[n];
    }
    mint fac_inv(int n) {
        if(n < 0) return mint(0);
        while(n >= (int)f_inv.size()) extend();
        return f_inv[n];
    }
    mint inv(int n) {
        if(n < 0) return mint(n).inv();
        assert(n != 0);
        while(n >= (int)f_inv.size()) extend();
        return (f_inv[n] * f[n - 1]);
    }
    mint binom(int n, int k) {
        if(n < k || n < 0 || k < 0) return mint(0);
        return (fac(n) * fac_inv(k) * fac_inv(n - k));
    }
    mint binom_naive(long long n, long long k) {
        if(n < k || n < 0 || k < 0) return mint(0);
        mint res(1);
        k = std::min(k, n - k);
        for(int i = 0; i < k; i++) res *= inv(i + 1) * mint(n - i);
        return res;
    }
    mint perm(int n, int k) {
        if(n < k || n < 0 || k < 0) return mint(0);
        return (fac(n) * fac_inv(n - k));
    }
    mint hom(int n, int k) {
        if(n < 0 || k < 0) return mint(0);
        return (k == 0 ? mint(1) : binom(n + k - 1, k));
    }

  private:
    std::vector<mint> f, f_inv;
};
#line 2 "modint/modint.hpp"

#line 6 "modint/modint.hpp"

template <int mod> class ModInt {
  public:
    ModInt() : x(0) {}
    ModInt(long long y)
        : x(y >= 0 ? y % umod() : (umod() - (-y) % umod()) % umod()) {}
    unsigned int val() const { return x; }
    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= umod()) x -= umod();
        return *this;
    }
    ModInt &operator-=(const ModInt &p) {
        if((x += umod() - p.x) >= umod()) x -= umod();
        return *this;
    }
    ModInt &operator*=(const ModInt &p) {
        x = (unsigned int)(1ULL * x * p.x % umod());
        return *this;
    }
    ModInt &operator/=(const ModInt &p) {
        *this *= p.inv();
        return *this;
    }
    ModInt operator-() const { return ModInt(-(long long)x); }
    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
    bool operator==(const ModInt &p) const { return x == p.x; }
    bool operator!=(const ModInt &p) const { return x != p.x; }
    ModInt inv() const {
        long long a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            std::swap(a -= t * b, b);
            std::swap(u -= t * v, v);
        }
        return ModInt(u);
    }
    ModInt pow(unsigned long long n) const {
        ModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }
    friend std::ostream &operator<<(std::ostream &os, const ModInt &p) {
        return os << p.x;
    }
    friend std::istream &operator>>(std::istream &is, ModInt &a) {
        long long t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }
    static constexpr int get_mod() { return mod; }

  private:
    unsigned int x;
    static constexpr unsigned int umod() { return mod; }
};
#line 2 "modint/dynamic-modint.hpp"

#line 6 "modint/dynamic-modint.hpp"

#line 8 "modint/dynamic-modint.hpp"

// verify: https://atcoder.jp/contests/arc104/submissions/38888866
class DynamicModInt {
  public:
    DynamicModInt() : x(0) {}
    DynamicModInt(long long y)
        : x(y >= 0
                ? y % (long long)mod
                : ((long long)mod - (-y) % (long long)mod) % (long long)mod) {}
    static void set_mod(unsigned int m) {
        assert(m > 0);
        mod = m;
        bt = BarrettReduction(m);
    }
    unsigned int val() const { return x; }
    DynamicModInt &operator+=(const DynamicModInt &p) {
        if((x += p.x) >= mod) x -= mod;
        return *this;
    }
    DynamicModInt &operator-=(const DynamicModInt &p) {
        if((x += (mod - p.x)) >= mod) x -= mod;
        return *this;
    }
    DynamicModInt &operator*=(const DynamicModInt &p) {
        x = bt.mul(x, p.x);
        return *this;
    }
    DynamicModInt &operator/=(const DynamicModInt &p) {
        *this *= p.inv();
        return *this;
    }
    DynamicModInt operator-() const { return DynamicModInt(-(long long)x); }
    DynamicModInt operator+(const DynamicModInt &p) const {
        return DynamicModInt(*this) += p;
    }
    DynamicModInt operator-(const DynamicModInt &p) const {
        return DynamicModInt(*this) -= p;
    }
    DynamicModInt operator*(const DynamicModInt &p) const {
        return DynamicModInt(*this) *= p;
    }
    DynamicModInt operator/(const DynamicModInt &p) const {
        return DynamicModInt(*this) /= p;
    }
    bool operator==(const DynamicModInt &p) const { return x == p.x; }
    bool operator!=(const DynamicModInt &p) const { return x != p.x; }
    DynamicModInt inv() const {
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            std::swap(a -= t * b, b);
            std::swap(u -= t * v, v);
        }
        assert(a == 1 && "gcd(x, mod) must be '1'.");
        return DynamicModInt(u);
    }
    DynamicModInt pow(long long n) const {
        assert(n >= 0);
        DynamicModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }
    friend std::ostream &operator<<(std::ostream &os, const DynamicModInt &p) {
        return os << p.x;
    }
    friend std::istream &operator>>(std::istream &is, DynamicModInt &a) {
        long long t;
        is >> t;
        a = DynamicModInt(t);
        return (is);
    }
    static unsigned int get_mod() { return mod; }

  private:
    unsigned int x;
    static inline unsigned int mod;
    static inline BarrettReduction bt;
};
#line 2 "math/F2.hpp"

#line 7 "math/F2.hpp"

// verify: https://atcoder.jp/contests/abc276/submissions/60797388

template<int MAX_WIDTH>
class BitMatrix {
public:
    int H, W;

    BitMatrix(): H(1), W(1), mat(1) {
        assert(W <= MAX_WIDTH);
    }

    BitMatrix(int H, int W): H(H), W(W), mat(H) {
        assert(W <= MAX_WIDTH);
    }

    inline std::bitset<MAX_WIDTH>& operator[](int i) { return mat[i]; }

    // 掃き出し法
    // https://drken1215.hatenablog.com/entry/2019/03/20/202800
    int row_reduction(std::vector<int>& b) {
        assert((int)b.size() == H);
        int rank = 0;
        for(int j = 0; j < W; j++) {
            int pivot = -1;
            for(int i = rank; i < H; i++) {
                if(mat[i][j]) {
                    pivot = i;
                    break;
                }
            }
            if(pivot == -1) continue;
            std::swap(mat[pivot], mat[rank]);
            std::swap(b[pivot], b[rank]);
            for(int i = 0; i < H; i++) {
                if(i != rank and mat[i][j]) {
                    mat[i] ^= mat[rank];
                    b[i] ^= b[rank];
                }
            }
            rank++;
        }
        return rank;
    }

    // 掃き出し法
    // https://drken1215.hatenablog.com/entry/2019/03/20/202800
    int row_reduction() {
        std::vector<int> b(H, 0);
        return row_reduction(b);
    }

private:
    std::vector<std::bitset<MAX_WIDTH>> mat;
};

// {rank, Ax = bの解のうち1つ} を返す
// 解なしの場合は {-1, {}} を返す
template<int MAX_WIDTH>
std::pair<int, std::vector<int>> linear_equation_F2(BitMatrix<MAX_WIDTH> A, std::vector<int> b) {
    int H = A.H, W = A.W;
    int rank = A.row_reduction(b);
    // 解の存在チェック
    for(int i = rank; i < H; i++) if(b[i]) {
        return {-1, {}};
    }
    std::vector<int> ret(W);
    for(int i = 0; i < rank; i++) {
        int p = -1;
        for(int j = 0; j < W; j++) {
            if(A[i][j]) {
                p = j;
                break;
            }
        }
        if(p == -1) continue;
        ret[p] = b[i];
    }
    return {rank, ret};
}
#line 10 "test/mytest/math/arithmetic-regressions.test.cpp"

void regression_test() {
    for(int l = 0; l <= 300; l++) for(int width : {0, 1, 2, 30, 100}) {
        SegmentSieve s(l, l + width);
        for(int x = l; x < l + width; x++) assert(s[x] == is_prime(x));
    }
    SegmentSieve big(10000000000LL, 10000000100LL);
    for(long long x = 10000000000LL; x < 10000000100LL; x++) assert(big[x] == is_prime(x));
    BarrettReduction one;
    for(unsigned x : {0U, 1U, 123U, UINT_MAX}) {
        assert(one.quo(x) == x && one.rem(x) == 0);
        assert(one.quorem(x) == make_pair(x, 0U));
    }
    assert(one.rem(ULLONG_MAX) == 0);
    assert(pow_mod(2, 0, 1) == 0 && pow_mod(-2, 3, 5) == 2);
    Binomial<ModInt<7>> b;
    assert(b.inv(-2).val() == 3 && b.inv(2).val() == 4);
    DynamicModInt::set_mod(7);
    for(int i = 0; i < 7; i++) assert((-DynamicModInt(i) + DynamicModInt(i)).val() == 0);
    BitMatrix<2> mat(2, 2);
    mat[0][0] = mat[1][1] = true;
    auto solution = linear_equation_F2(mat, vector<int>{1, 0});
    assert(solution.first == 2 && solution.second == vector<int>({1, 0}));
}

int main() {
    regression_test();
    INT(a, b);
    print(a + b);
}
Back to top page