Welcome to |

XiaoLe_MC

园龄:1年2个月粉丝:3关注:9

我的板子

读写优化

优化一
#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);

取模加速

// generated by Copilot
#include <iostream>
class BarrettModulo {
private:
uint64_t p; // The modulus
uint64_t m; // Precomputed value for optimization
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 中国大陆许可协议进行许可。

posted @   XiaoLe_MC  阅读(19)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起