位运算+模版测试

P1001 A+B Problem

Matthew192 @ 2022-04-26 15:36:27

试一试今天新写的模版,看看能不能用。大家可以参考使用哦~

#include <bits/stdc++.h>

using namespace std;

inline namespace fastmath{
    typedef long long ll;
    typedef unsigned long long ull;
    typedef __uint128_t L;

    inline ll ABS(ll n);
    inline ll ADD(ll a, ll b);
    inline ll SUB(ll a, ll b);
    inline ll MUL(ll a, ll b);
    inline ll DIV(ll a, ll b);
    inline ll POW(ll a, ll b, ll mod);
    inline ull MOD(ull a, ull b);

    inline ll ABS(ll n){
        return (n ^ (n >> 63)) - (n >> 63);
    }

    inline ll ADD(ll a, ll b){
        if (a < 0 && b < 0){
            return -ADD(-a, -b);
        }
        if (a < 0){
            return SUB(b, -a);
        }
        if (b < 0){
            return SUB(a, -b);
        }
        while (b > 0){
            ll carry = a & b;
            a ^= b;
            b = carry << 1;
        }
        return a;
    }

    inline ll SUB(ll a, ll b){
        if (a < 0 && b < 0){
            return -SUB(-a, -b) + 1;
        }
        if (a < 0){
            return ADD(-a, b);
        }
        if (b < 0){
            return ADD(a, -b);
        }
        while (b != 0){
            ll carry = (~a) & b;
            a = a ^ b;
            b = carry << 1;
        }
        return a;
    }

    inline ll MUL(ll a, ll b){
        ll x = ABS(a), y = ABS(b), res = 0;
        bool neg = false;
        if (min(a, b) < 0 && max(a, b) >= 0){
            neg = true;
        }
        while(y > 0){
            if ((y & 1) == 1){
                res += x;
            }
            y >>= 1; x <<= 1;
        }
        if (neg){
            return (~(res) + 1);
        }
        else{
            return res;
        }
    }

    inline ll DIV(ll a, ll b){
        if (a < 0 && b < 0){
            return DIV(-a, -b);
        }
        if (a < 0){
            return -DIV(-a, b);
        }
        if (b < 0){
            return -DIV(a, -b);
        }
        int tmp = 1, res = 0;
        while (b <= a){
            b <<= 1;
            tmp <<= 1;
        }
        while (tmp > 1){
            b >>= 1;
            tmp >>= 1;
            if (a >= b){
                a -= b;
                res += tmp;
            }
        }
        return res;
    }

    inline ll POW(ll base, ll exponent, ll mod=LLONG_MAX){
        ll x = 1;
        ll y = base;
        while (exponent > 0){
            if (MOD(exponent, 2) == 1){
                x = MOD((x * y), mod);
            }
            y = MOD((y * y), mod);
            exponent >>= 1;
        }
        return MOD(x, mod);
    }

    struct FastMod{
        ull b, m;
        FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
        ull reduce(ull a) {
            ull q = (ull)((L(m) * a) >> 64);
            ull r = a - q * b;
            return r >= b ? r - b : r;
        }
    };

    FastMod F(2);

    inline ull MOD(ull a, ull b){
        F = FastMod(b);
        return F.reduce(a);
    }
}

#include <bits/stdc++.h>

using namespace std;

inline namespace FastIO{
    const int bufferSize = 1 << 15;
    inline namespace FastInput{
        // INPUT
        char inputBuffer[bufferSize]; int iPosition, iLength;

        char next_char(){
            if (iPosition == iLength){
                iPosition = 0; iLength = fread(inputBuffer, 1, bufferSize, stdin);
                if (!iLength) return EOF;
            }
            return inputBuffer[iPosition++];
        }

        void read_string(string &x){
            char ch; while(isspace(ch = next_char())){};
            do {
                x += ch;
            } while(!isspace(ch = next_char()) && ch!= '\n');
        }

        template<class T> void read_int(T &x){ // read int OR long long
            char ch; int sign = 1;
            while (!isdigit(ch = next_char())){
                if (ch == '-') sign *= -1;
            }
            x = ch - '0';
            while (isdigit(ch = next_char())){
                x = x * 10 + (ch - '0');
            }
            x *= sign;
        }

        template<class T, class... Ts> void read_int(T& t, Ts&... ts){
            read_int(t); read_int(ts...);
        }
    }
    inline namespace fastOutput{
        // OUTPUT (call initO() at start)
        char outBuffer[bufferSize], numberBuffer[100]; int outPosition;
        void flushOut(){
            fwrite(outBuffer, 1, outPosition, stdout);
            outPosition = 0;
        }

        void write_char(char c){
            if (outPosition == bufferSize){
                flushOut();
            }
            outBuffer[outPosition++] = c;
        }

        void write_string(string s){
            for (char c : s){
                write_char(c);
            }
        }

        template<class T> void write_int(T x, char after = '\0'){
            if (x < 0){
                write_char('-');
                x = x * -1;
            }
            int length = 0;
            for (; x >= 10; x /= 10){
                numberBuffer[length ++] = '0' + (x % 10);
            }
            write_char('0' + x);
            for (int i = length - 1; i >= 0; i --){
                write_char(numberBuffer[i]);
            }
            if (after){
                write_char(after);
            }
        }

        void initO(){
            assert(atexit(flushOut) == 0);
        }
    }
}

int main(){
    initO();
    int a, b;
    read_int(a, b);
    write_int(ADD(a, b), '\n');
    return 0;
}

by seld59 @ 2022-04-26 15:40:54

%%%


by Matthew192 @ 2022-04-26 15:44:29

呵呵 忽然发现 #include 了两遍,但是不影响结果 :)


by Pretharp @ 2022-04-26 15:49:13


by Pretharp @ 2022-04-26 15:49:37

lz 可以考虑写个使用教程,用博客保存一下


by rxjdasiwzl @ 2022-04-26 15:49:38

不懂就问,这个有什么用?


by rxjdasiwzl @ 2022-04-26 15:50:30

让你的代码凭空多一个 log 吗


by Coros_Trusds @ 2022-04-26 16:03:52

@rxjdasiwzl ?说啥呢,多个 log 是什么鬼/yiw


by rxjdasiwzl @ 2022-04-26 16:08:51

@Coros_Trusds 乘法不是龟速乘吗?然后加法搞 (111\cdots1)_2+(1)_2 就跑满一个 log 了吧。


by Coros_Trusds @ 2022-04-26 16:12:53

@rxjdasiwzl 肯定是特定的地点用吧。。。


by ud2_ @ 2022-04-26 16:23:51

SUB 是什么运算?为什么 SUB(-1, -1) == 1


| 下一页