1 Star 2 Fork 0

rlmnsk/算法与数据结构

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MLong.sublime-snippet 2.82 KB
一键复制 编辑 原始数据 按行查看 历史
rlmnsk 提交于 2023-04-10 14:12 . MLong
<snippet>
<content><![CDATA[
template<class T>
T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
constexpr i64 mul(i64 a, i64 b, i64 p) {
i64 res = a * b - i64(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template<i64 P>
struct MLong {
i64 x;
MLong() : x(0) {}
MLong(i64 x) : x(norm(x % P)) {}
explicit operator int() const {
return x;
}
explicit operator i64() const {
return x;
}
i64 norm(i64 x) const {
x += (x >= P ? -P : (x < 0 ? P : 0));
return x;
}
// assume -P <= x < 2P
MLong operator-() const {
return MLong(norm(P - x));
}
MLong inv() const {
assert(x != 0);
return power(*this, P - 2);
}
MLong &operator*=(const MLong &rhs) {
x = i64(__int128(rhs.x) * x % P);
return *this;
}
MLong &operator+=(const MLong &rhs) {
x = mul(x, rhs.x, P);
return *this;
}
MLong &operator-=(const MLong &rhs) {
x = norm(x - rhs.x);
return *this;
}
MLong &operator/=(const MLong &rhs) {
return *this *= rhs.inv();
}
friend MLong operator*(const MLong &lhs, const MLong &rhs) {
MLong res = lhs;
res *= rhs;
return res;
}
friend MLong operator+(const MLong &lhs, const MLong &rhs) {
MLong res = lhs;
res += rhs;
return res;
}
friend MLong operator-(const MLong &lhs, const MLong &rhs) {
MLong res = lhs;
res -= rhs;
return res;
}
friend MLong operator/(const MLong &lhs, const MLong &rhs) {
MLong res = lhs;
res /= rhs;
return res;
}
friend std::istream &operator>>(std::istream &is, MLong &a) {
i64 v;
is >> v;
a = MLong(v);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const MLong &a) {
return os << a.x;
}
friend bool operator==(const MLong &lhs, const MLong &rhs) {
return lhs.x == rhs.x;
}
friend bool operator!=(const MLong &lhs, const MLong &rhs) {
return lhs.x != rhs.x;
}
friend bool operator<(const MLong &lhs, const MLong &rhs) {
return lhs.x < rhs.x;
}
friend bool operator>(const MLong &lhs, const MLong &rhs) {
return lhs.x > rhs.x;
}
};
constexpr i64 P = 1e18;
using Z = MLong<P>;
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<!-- <tabTrigger>hello</tabTrigger> -->
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/rlmnsk/algorithm-and-data-structure.git
git@gitee.com:rlmnsk/algorithm-and-data-structure.git
rlmnsk
algorithm-and-data-structure
算法与数据结构
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385