• QAQ数论模板笔记√


    #include <cstdio>
    
    using namespace std;
    
    long long gcd(long long a, long long b) { // (a, b)
        return b ? gcd(b, a % b) : a;
    }
    
    long long exGcd(long long a, long long b, long long &x, long long &y) { // ax + by = 1
        if (b == 0) {
            x = 1, y = 0;
            return a;
        }
        long long d = exGcd(b, a % b, x, y);
        long long t = x;
        x = y;
        y = t - a / b * y;
        return d;
    }
    
    bool calcInv(long long a, long long p, long long &inv) { // Inv_a % p
        long long x, y;
        long long d = exGcd(a, p, x, y);
        if (1 % d == 0)    return inv = (x % p + p) % p, 1;
        else return 0;
    }
    
    bool calcLinear(long long a, long long b, long long c, long long &x, long long &y) { // ax + by = c
        long long _x = x, _y = y;
        long long d = exGcd(a, b, _x, _y);
        if (c % d) return 0;
        x = _x * c / d, y = _y * c / d;
        return 1;
    }
    
    class CRT { // x = ai mod mi
    public :
        bool merge(long long a, long long m, long long b, long long n, long long &r, long long &p) {
            long long g = gcd(m, n);
            long long c = b - a;
            if (c % g) return 0;
            c = (c % n + n) % n;
            c /= g, m /= g, n /= g;
            long long inv; calcInv(m, n, inv);
            c *= inv, c %= n, c *= m * g, c += a;
            p = m * n * g, r = (c % p + p) % p;
            return 1;
        }
    
        long long calc(long long *a, long long *m, long long n) {
            long long remain = a[1], p = m[1];
            for (int i = 2; i <= n; ++ i) {
                long long _r, _p;
                if (!merge(remain, p, a[i], m[i], _r, _p)) return -1;
                remain = _r, p = _p;
            }
            return (remain % p + p) % p;
        }
    } ;
    
    template <int SIZE> class calcPrimeNumbers { // calc the prime numbers below SIZE
    public :    
        int isNotPrime[SIZE + 10];
        int primes[SIZE + 10];
        int primeCnt;
    
        void sieve(int lim) {
            for (int i = 2; i <= lim; ++ i) {
                if (!isNotPrime[i]) primes[++ primeCnt] = i;
                for (int j = 1; j <= primeCnt && i * primes[j] <= lim; ++ j) {
                    isNotPrime[i * primes[j]] = 1;
                    if (i % primes[j] == 0) break ;
                }
            }
        }
    } ;
    
    long long mul(long long a, long long b, long long p) { // a * b % p;
        long long ret = 0;
        for (int i = 62; ~ i; -- i) 
            ret = (ret + ret) % p, b & (1ll << i) ? ret = (ret + a) % p : 0;
        return ret % p;
    }
    
    long long quickPower(long long a, long long b, long long p) { // a ^ b % p
        long long ret = 1;
        for ( ; b; b >>= 1, a = mul(a, a, p))
            b % 2 ? ret = mul(ret, a, p) : 0;
        return ret;
    }
    
    template <int SIZE> class gaussian { // gaussian elimination    
    public :
        double mat[SIZE + 10][SIZE + 10];
        double ans[SIZE + 10];
    
        void gauss(int n) {
            static int id[SIZE + 10];
            for (int i = 1; i <= n; ++ i) id[i] = i;
            for (int i = 1; i <= n; ++ i) {
                int now = i;
                for ( ; now <= n; ++ now) {
                    if (mat[id[now]][i] != 0) break ;
                }
                if (now == n + 1) continue ;
                swap(id[now], id[i]);
                for (int j = i + 1; j <= n; ++ j) {
                    int ii = id[i], ij = id[j];
                    double t = mat[ij][i] / mat[ii][i];
                    for (int k = n + 1; k >= i; -- k) {
                        mat[ij][k] = mat[ii][k] * t - mat[ij][k];
                    }
                }
            }
            for (int i = n; i; -- i) {
                double count = mat[id[i]][n + 1];
                for (int j = n; j > i; -- j) {
                    count -= mat[id[i]][j] * ans[j];
                }
                ans[i] = count / mat[id[i]][i];
            }
        }
    } ;
    
    template <int SIZE> class calcPhi { // calc the phi
    public :
        int isNotPrime[SIZE + 10], primes[SIZE + 10], primeCnt;
        int phi[SIZE + 10];
    
        void sieve(int lim) {
            phi[1] = 1;
            for (int i = 2; i <= lim; ++ i) {
                if (!isNotPrime[i]) {
                    primes[++ primeCnt] = i;
                    phi[i] = i - 1;
                }
                for (int j = 1; j <= primeCnt && i * primes[j] <= lim; ++ j) {
                    isNotPrime[i * primes[j]] = 1;
                    if (i % primes[j] == 0) {
                        phi[i * primes[j]] = phi[i] * primes[j];
                        break ;
                    }
                    else {
                        phi[i * primes[j]] = phi[i] * (primes[j] - 1);
                    }
                }
            }
        }
    } ;
    
    int main() {
        return 0;
    }
    by yjl

      %%%yjl

  • 相关阅读:
    php -- php数组相关函数
    php -- 数组排序
    php -- in_array函数
    php -- 魔术方法 之 删除属性:__unset()
    无符号整型与有符号整型相运算规则
    N个节点的二叉树有多少种形态
    getopt_long
    typedef
    约瑟夫环问题算法(M)
    C语言基础
  • 原文地址:https://www.cnblogs.com/gc812/p/5795542.html
Copyright © 2020-2023  润新知