Kuro-orzz's Library

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

View on GitHub

:warning: NumberTheory/Math/Primality_test.h

Depends on

Code

#include "../../template.h"
#include "Binary_exponentiation.h"

// Fermat's little theorem
bool isPrime(ll n) {
    if (n < 7) return n == 2 || n == 3 || n == 5;
    for (int i = 0; i < 5; i++) {
        ll a = rand() % (n-3) + 2;
        if (binPow(a, n-1, n) != 1) 
            return false;
    }
    return true;
}
#line 2 "template.h"

#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define MOD (ll)(1e9+7)
#define all(x) (x).begin(),(x).end()
#define unique(x) x.erase(unique(all(x)), x.end())
#define INF32 ((1ull<<31)-1)
#define INF64 ((1ull<<63)-1)
#define inf (ll)1e18

#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

const int mod = 998244353;

void solve();

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    // cin.exceptions(cin.failbit);
    // int t; cin >> t;
    // while(t--)
        solve();
    cerr << "\nTime run: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << '\n';
    return 0;
}
#line 2 "NumberTheory/Math/Binary_exponentiation.h"

using u128 = __uint128_t;

ll binMul(ll a, ll b, ll M) {
    a = a % M;
    ll res = 0;
    while (b) {
        if (b & 1) res = (res + a) % M;
        a = a * 2 % M;
        b /= 2;
    }
    return res;
}

ll binPow(ll a, ll b, ll M) {
    a %= M;
    ll res = 1;
    while (b) {
        if (b & 1) res = (u128)res * a % M;
        a = (u128)a * a % M;
        b /= 2;
    }
    return res;
}
#line 3 "NumberTheory/Math/Primality_test.h"

// Fermat's little theorem
bool isPrime(ll n) {
    if (n < 7) return n == 2 || n == 3 || n == 5;
    for (int i = 0; i < 5; i++) {
        ll a = rand() % (n-3) + 2;
        if (binPow(a, n-1, n) != 1) 
            return false;
    }
    return true;
}
Back to top page