跳到正文

剪贴板

剪贴板 yxg1rzad

原文
剪贴板信息

剪贴板信息

收录
更新

long long 取模乘法

C++
#define mul(x,y,m) ((x*y-(long long)((long double)x/m*y)*m+m)%m)

为啥不 int128

barrett 约减

C++
namespace barrett{
	long long m,p;
	inline void init(long long mod){
		m=((__int128)1<<64)/mod;
		p=mod;
	}
	inline long long mod(long long x){
		long long res=x-((__int128(x)*m)>>64)*p;
		return res>=p?res-p:res;
	}
}

IO优化

C++
const int IN_LIMIT=1e7;
char buf[IN_LIMIT];//fread(buf,1,IN_LIMIT,stdin)
char*pc=buf;
#define getchar() *pc++
inline int read(){
	int num=0;
	char c;
	while((c=getchar())<0x2d);
	do
	{
		num=(num<<1)+(num<<3)+(c^0x30);
	}
	while((c=getchar())>=0x30);
	return num;
}
C++
char out[OUT_LIMIT+20],rev[100];
int Len(0);
#define putchar(ch) out[Len++]=ch
#define flush() fwrite(out,1,Len,stdout),Len=0
inline void write(int x){
    int len(0);
    do{
        rev[len++]=(x%10)^0x30;
        x/=10;
    }while(x);
    while(len)
    {
        out[Len++]=rev[--len];
    }
    if(Len>OUT_LIMIT){
        flush();
    }
}
C++
#ifdef ONLINE_JUDGE
#include<sys/mman.h>
#include<unistd.h>
char *pc=(char*)mmap(NULL,lseek(0,0,SEEK_END),PROT_READ,MAP_PRIVATE,0,0);
#define getchar() *pc++
#endif
inline int read(){
    int num=0;
    char c;
    while((c=getchar())<0x2d);
    do
    {
        num=(num<<1)+(num<<3)+(c^0x30);
    }
    while((c=getchar())>=0x30);
    return num;
}
C++
namespace FastIO
{
// ------------------------------
// #define IN_HAS_NEG
// #define OUT_HAS_NEG
// #define CHK_EOF
// #define DISABLE_MMAP
// ------------------------------
#if __cplusplus < 201400
#error Please use C++14 or higher.
#endif
#if __cplusplus > 201700
#define INLINE_V inline
#else
#define INLINE_V
#endif
#if ( defined(LOCAL) || defined (_WIN32) ) && !defined(DISABLE_MMAP)
#define DISABLE_MMAP
#endif
#ifndef DISABLE_MMAP
#include<sys/mman.h>
#endif
#ifdef LOCAL
    inline char gc() { return getchar(); }
    inline void pc(char c) { putchar(c); }
#else
#ifdef DISABLE_MMAP
    INLINE_V constexpr int _READ_SIZE = 1 << 18;
    INLINE_V static char _read_buffer[_READ_SIZE], *_read_ptr = nullptr, *_read_ptr_end = nullptr;
    inline char gc()
    {
        if ( __builtin_expect(_read_ptr == _read_ptr_end, false) )
        {
            _read_ptr = _read_buffer;
            _read_ptr_end = _read_buffer + fread(_read_buffer, 1, _READ_SIZE, stdin);
#ifdef CHK_EOF
            if ( __builtin_expect(_read_ptr == _read_ptr_end, false) ) return EOF;
#endif
        }
        return *_read_ptr++;
    }
#else
    INLINE_V static const char *_read_ptr = (const char *)mmap(nullptr, INT_MAX, 1, 2, 0, 0);
    inline char gc() { return *_read_ptr++; }
#endif
    INLINE_V constexpr int _WRITE_SIZE = 1 << 18;
    INLINE_V static char _write_buffer[_WRITE_SIZE], *_write_ptr = _write_buffer;
    inline void pc(char c)
    {
        *_write_ptr++ = c;
        if ( __builtin_expect(_write_buffer + _WRITE_SIZE == _write_ptr, false) )
        {
            fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout);
            _write_ptr = _write_buffer;
        }
    }
    INLINE_V struct _auto_flush
    {
        ~_auto_flush() { fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout); }
    }	_auto_flush;
#endif
#ifdef CHK_EOF
    inline bool _isdigit(char c) { return ( c & 16 ) && c != EOF; }
    inline bool _isgraph(char c) { return c > 32 && c != EOF; }
#else
    inline bool _isdigit(char c) { return c & 16; }
    inline bool _isgraph(char c) { return c > 32; }
#endif
    template < class T >
    INLINE_V constexpr bool _is_integer = numeric_limits < T >::is_integer;
    template < class T >
    INLINE_V constexpr bool _is_signed = numeric_limits < T >::is_signed;
    template < class T >
    INLINE_V constexpr bool _is_unsigned = _is_integer < T > && !_is_signed < T >;
    template <> INLINE_V constexpr bool _is_integer < __int128 > = true;
    template <> INLINE_V constexpr bool _is_integer < __uint128_t > = true;
    template <> INLINE_V constexpr bool _is_signed < __int128 > = true;
    template <> INLINE_V constexpr bool _is_unsigned < __uint128_t > = true;
#undef INLINE_V
    inline void read(char &c) { do c = gc(); while ( !_isgraph(c) ); }
    inline void read_cstr(char *s)
    {
        char c = gc(); while ( !_isgraph(c) ) c = gc();
        while ( _isgraph(c) ) *s++ = c, c = gc();
        *s = 0;
    }
    inline void read(string &s)
    {
        char c = gc(); s.clear(); while ( !_isgraph(c) ) c = gc();
        while ( _isgraph(c) ) s.push_back(c), c = gc();
    }
#ifdef IN_HAS_NEG
    template < class T, enable_if_t < _is_signed < T >, int > = 0 >
    inline void read(T &x)
    {
        char c = gc(); bool f = true; x = 0;
        while ( !_isdigit(c) ) { if ( c == 45 ) f = false; c = gc(); }
        if ( f ) while ( _isdigit(c) ) x = x * 10 + ( c & 15 ), c = gc();
        else     while ( _isdigit(c) ) x = x * 10 - ( c & 15 ), c = gc();
    }
    template < class T, enable_if_t < _is_unsigned < T >, int > = 0 >
#else
    template < class T, enable_if_t < _is_integer < T >, int > = 0 >
#endif
    inline void read(T &x)
    {
        char c = gc(); while ( !_isdigit(c) ) c = gc();
        x = 0; while ( _isdigit(c) ) x = x * 10 + ( c & 15 ), c = gc();
    }
    inline void write(char c) { pc(c); }
    inline void write_cstr(const char *s) { while ( *s ) pc(*s++); }
    inline void write(const string &s) { for ( char c : s ) pc(c); }
#ifdef OUT_HAS_NEG
    template < class T, enable_if_t < _is_signed < T >, int > = 0 >
    inline void write(T x)
    {
        char buffer[numeric_limits < T >::digits10 + 1]; int digits = 0;
        if ( x >= 0 )  do buffer[digits++] =  ( x % 10 ) | 48, x /= 10; while ( x );
        else { pc(45); do buffer[digits++] = -( x % 10 ) | 48, x /= 10; while ( x ); }
        while ( digits ) pc(buffer[--digits]);
    }
    template < class T, enable_if_t < _is_unsigned < T >, int > = 0 >
#else
    template < class T, enable_if_t < _is_integer < T >, int > = 0 >
#endif
    inline void write(T x)
    {
        char buffer[numeric_limits < T >::digits10 + 1]; int digits = 0;
        do buffer[digits++] = ( x % 10 ) | 48, x /= 10; while ( x );
        while ( digits ) pc(buffer[--digits]);
    }
    template < int N > struct _tuple_io_helper
    {
        template < class ...T >
        static inline void _read(tuple < T... > &x)
        { _tuple_io_helper < N - 1 >::_read(x), read(get < N - 1 > (x)); }
        template < class ...T >
        static inline void _write(tuple < T... > &x)
        { _tuple_io_helper < N - 1 >::_write(x), pc(32), write(get < N - 1 > (x)); }
    };
    template <> struct _tuple_io_helper < 1 >
    {
        template < class ...T >
        static inline void _read(tuple < T... > &x) { read(get < 0 > (x)); }
        template < class ...T >
        static inline void _write(tuple < T... > &x) { write(get < 0 > (x)); }
    };
    template < class ...T >
    inline void read(tuple < T... > &x) { _tuple_io_helper < sizeof...(T) >::_read(x); }
    template < class ...T >
    inline void write(const tuple < T... > &x) { _tuple_io_helper < sizeof...(T) >::_write(x); }
    template < class T1, class T2 >
    inline void read(pair < T1, T2 > &x) { read(x.first), read(x.second); }
    template < class T1, class T2 >
    inline void write(const pair < T1, T2 > &x) { write(x.first), pc(32), write(x.second); }
    template < class T1, class ...T2 >
    inline void read(T1 &x, T2 &...y) { read(x), read(y...); }
    template < class ...T >
    inline void read_cstr(char *x, T *...y) { read_cstr(x), read_cstr(y...); }
    template < class T1, class ...T2 >
    inline void write(const T1 &x, const T2 &...y) { write(x), write(y...); }
    template < class ...T >
    inline void write_cstr(const char *x, const T *...y) { write_cstr(x), write_cstr(y...); }
    template < class T >
    inline void print(const T &x) { write(x); }
    inline void print_cstr(const char *x) { write_cstr(x); }
    template < class T1, class ...T2 >
    inline void print(const T1 &x, const T2 &...y) { print(x), pc(32), print(y...); }
    template < class ...T >
    inline void print_cstr(const char *x, const T *...y) { print_cstr(x), print_cstr(y...); }
    inline void println() {}
    inline void println_cstr() {}
    template < class ...T >
    inline void println(const T &...x) { print(x...), pc(10); }
    template < class ...T >
    inline void println_cstr(const T *...x) { print_cstr(x...), pc(10); }
}

快速sqrt

C++
namespace fast_sqrt{
    double Sqrt(double X){
        float x=X;
        float xhalf=0.5*x;
        int tmp=*(int*)&x;
        tmp=0x5f375a86-(tmp>>1);
        x=*(float*)&tmp;
        x=x*(1.5-xhalf*x*x);
        return 1.0/x;
    }
}
C++
namespace fast_sqrt{
    #include<xmmintrin.h>
    double Sqrt(double a){
        float b=a;
        __m128 in=_mm_load_ss(&b);
        __m128 out=_mm_rsqrt_ss(in);
        _mm_store_ss(&b, out);
        return a*b;
    }
}
C++
namespace fast_sqrt{
    double Sqrt(double x){
        union P{
            double x;
            unsigned long i[2];
        };
        double a;
        union P x0;
        a=x/2;
        x0.x=x;
        x0.i[1]=((x0.i[1]&(0xffefffff>>1))>>1)+(512<<20);
        x=x0.x/2+a/x0.x;
        x=(x/2+a/x);
        x=(x/2+a/x);
        x=(x/2+a/x);
        x=(x/2+a/x);
        return x;
    }
}

快速mod

C++
namespace fast_mod{
    long long m=((__int128)1<<64)/mod,p=mod;
    inline long long Mod(long long x){
        return x-((__int128(x)*m)>>64)*p;
    }
}
using fast_mod::Mod;

快速gcd

C++
int gcd(int a,int b){
    int az=__builtin_ctz(a),bz=__builtin_ctz(b),z=(az>bz)?bz:az,t;
    b>>=bz;
    while(a)
    {
        a>>=az;
        t=a-b;
        az=__builtin_ctz(t);
        b=a<b?a:b;a=t<0?-t:t;
    }
    return b<<z;
}

快速 log

C++
inline unsigned int LOG(unsigned int x){unsigned int ret;__asm__ __volatile__("bsrl %1, %%eax":"=a"(ret):"m"(x));return ret;}