各种奇怪的小东西

缺省源

#include<bits/stdc++.h>
//#include <ext/pb_ds/hash_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
namespace infinities{
    #define fint register int
    #define ls(i) (i << 1)
    #define rs(i) ((i << 1) | 1)
    #define pii pair<int, int>
    #define im int mid = (l + r) >> 1
    #define INT __int128
    #define ll long long
    #define ui unsigned int
    #define lc ch[now][0]
    #define rc ch[now][1]
    const int mod = 998244353, INF = 998244353, maxn = 2e6 + 7;
    namespace FastIO{//10M
        const int SS = 1e7; char num_[50]; int cnt_;
        inline int xchar(){static char buf[SS]; static int len = 0, pos = 0; if(pos == len)pos = 0, len = fread(buf, 1, SS, stdin); if(pos == len)exit(0); return buf[pos++];}
        inline int read(){fint x = 0, f = 1, ch = getchar(); while(ch < '0' || ch > '9'){if(ch == '-')f = -1; ch = getchar();}while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();} return x * f;}
        inline void write(int x){if(x == 0){putchar('0'); return;}if(x < 0)putchar('-'), x = -x; while(x)num_[++cnt_] = x % 10 ^ 48,x /= 10; while(cnt_)putchar(num_[cnt_--]);}
        inline void write(int x, char c){write(x), putchar(c);}
    }
    using namespace std;
    using namespace FastIO;
    namespace mystd{
        inline int qmin(int a, int b){return (a > b) ? b : a;} inline int qmax(int a, int b){return (a > b) ? a : b;}
        inline void chkmin(int &a, int b){a = min(a, b); return;} inline void chkmax(int &a, int b){a = max(a, b); return;}
        inline void qk(int &a, int b){a = (a + b) % mod;}
		inline int mul(int a, int b){return (1ll * a * b % mod + mod) % mod;}
        inline int qpow(int x, int k){fint res = 1; for(; k; k = (k >> 1), x = 1ll * x * x % mod)if(k & 1)res = 1ll * res * x % mod; return res;}
        inline void looked(int a[], int n){for(fint i = 1; i < n; i++)write(a[i], ' '); write(a[n], '\n');} inline void debug(){puts("qwq");} inline void YES(){puts("YES");} inline void Yes(){puts("Yes");} inline void yes(){puts("yes");} inline void NO(){puts("NO");} inline void No(){puts("No");} inline void no(){puts("no");}
    }
	//using namespace mystd;
	//using namespace __gnu_pbds;
	//gp_hash_table<int, int>hsh;
    void file(){freopen("a.in", "r", stdin), freopen("a.out", "w", stdout);}
    signed main(){
    	//file();
        return 0;
    }
}
signed main(){
    return infinities::main();
}

二进制函数

__builtin_ffs(unsigned int x)
返回从低位开始第一个1的位置。

__builtin_clz(unsigned int x)
返回从高位开始第一个1前0个数。

__builtin_ctz(unsigned int x)
返回从低位开始第一个1后0个数。

__builtin_popcount(unsigned int x)
返回1的个数。

函数后面加 ll 就是 (unsigned long long x)

__builtin_popcountll(unsigned long long x)

库内实现大概是开桶存(一般会开到 \(2^{16}\) 的桶)然后硬算,所以是 \(O(1)\) 的。有需要可以手动实现。

随机数

rand() 值域是 \([0,32767]\),有时不够用。

可以使用 mt19937,方法是 mt19937 rnd(114514),其中 rnd() 是你要的函数名,114514 是种子,可以用 time(0),这个随机数是在变量值域内随机,而且相当均匀。

pbds

使用时加入头文件

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>//用tree
#include<ext/pb_ds/hash_policy.hpp>//用hash
#include<ext/pb_ds/trie_policy.hpp>//用trie
#include<ext/pb_ds/priority_queue.hpp>//用priority_queue

可以用里面的哈希。他实现的平衡树用不好容易T爆。堆直接用标准库的基本就够用了。trie 手写简单的一匹。

用法:

#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
gp_hash_table<int, int> c;

然后使用方法和 map 基本一样。但是时间快很多(比同样 hash 实现的 unordered_map 也快两倍不止),平时手懒可以用用。

int 取模较快的加法:

a = a + b - mod, a += a >> 31 & mod;
posted @ 2023-02-10 11:56  infinities  阅读(21)  评论(0编辑  收藏  举报