读写优化
优化一
| #ifdef _WIN32 |
| #define getchar _getchar_nolock |
| #define putchar _putchar_nolock |
| #else |
| #define getchar getchar_unlocked |
| #define putchar putchar_unlocked |
| #endif |
| template <typename T> inline void rd(T &x){ |
| x = 0; int f = 1; char ch = getchar(); |
| while(!isdigit(ch)){ if(ch == '-') f = -1; ch = getchar();} |
| while(isdigit(ch)) x = (x<<1) + (x<<3) + (ch^48), ch = getchar(); |
| x *= f; |
| } |
| template <typename T> inline void wt(T x){ |
| if(x < 0) putchar('-'), x = -x; |
| if(x / 10 > 0) wt(x / 10); putchar(x % 10 + '0'); |
| } |
优化二
| constexpr int B = 1 << 20; |
| char buf[B], *p1 = buf, *p2 = buf, obuf[B], *O = obuf; |
| #define gt() (p1==p2 && (p2=(p1=buf)+fread(buf, 1, B, stdin), p1==p2) ? EOF : *p1++) |
| template <typename T> inline void rd(T &x){ |
| x = 0; int f = 0; char ch = gt(); |
| for(; !isdigit(ch); ch = gt()) f ^= ch == '-'; |
| for(; isdigit(ch); ch = gt()) x = (x<<1) + (x<<3) + (ch^48); |
| x = f ? -x : x; |
| } |
| #define pt(ch) (O-obuf==B && (fwrite(obuf, 1, B, stdout), O=obuf), *O++ = (ch)) |
| template <typename T> inline void wt(T x){ |
| if(x < 0) pt('-'), x = -x; |
| if(x > 9) wt(x / 10); pt(x % 10 ^ 48); |
| } |
| #define fw fwrite(obuf, 1, O - obuf, stdout) |
注意在代码末尾加上 fw
| fwrite(obuf, 1, O - obuf, stdout) |
取模
| inline int mod(int x){ return (x % M + M) % M; } |
| inline int mul(initializer_list<int> Mul){ |
| int res = 1; |
| for(int v : Mul) res = (ll)res * v % M; |
| return res; |
| } |
| inline void add(initializer_list<int> Add){ |
| int res = 1; |
| for(int v : Add) res = res + v > M ? res + v - M : res + v; |
| return res; |
| } |
用的时候写个大括号直接往里写上所有数即可:like:
| int ans = mul({2, 3, 500000004}); |
| int res = add({1, 2, 3, 4, 5, 6}); |
| int okk = mod(-114514); |
取模加速
| |
| #include <iostream> |
| |
| class BarrettModulo { |
| private: |
| uint64_t p; |
| uint64_t m; |
| |
| public: |
| BarrettModulo(uint64_t modulus) : p(modulus) { |
| m = ((__int128)1 << 64) / p; |
| } |
| |
| uint64_t operator()(uint64_t x) const { |
| return x - ((__int128(x) * m) >> 64) * p; |
| } |
| }; |
| |
| int main() { |
| BarrettModulo Mod(998244353); |
| uint64_t result = Mod(998244354); |
| |
| std::cout << "Result: " << result << std::endl; |
| |
| return 0; |
| } |
本文作者:XiaoLe_MC
本文链接:https://www.cnblogs.com/xiaolemc/p/18363531
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步