引言
众所周知,OI/XCPC 中的数学题难度通常由算法难度本身和思维难度相关。但这样会带来一些问题,比如某道数学题的思维难度本身并不难,却因为套了一个 NTT 之类的模板就变成了紫题甚至黑题。而这对数学出题组实在是太不友善了。跟这种制度在一起,怎么能出出来好的签到题呢。
为此,我们认为有必要为选手提供一份小常数算法模板,从而避免上述情况的发生。同时考虑到有的选手可能还是用自己的模板写起来不烧心,所以我们希望能在提供模板的同时,仍然保留选手自己写模板的权利。
于是在这样的想法下,Mathematical Algorithms Library for OI.(MAL) 诞生了。
原理
根据洛谷的交互库说明,选手程序 main.cpp 会与题目提供的 interactive_lib.cpp 共同编译、链接。
在本地可以类似视作:
g++ -std=c++14 -O2 interactive_lib.cpp main.cpp -o main传统的交互题中,会将 main() 函数放在 interactive_lib.cpp 中,所以选手无需也无法在选手程序 main.cpp 中编写主函数。
由上述交互题的编译原理,只要我们不在 interactive_lib.cpp 中编写主函数,那么选手仍然需要在自己的代码中编写主函数。但此时交互库中的函数只需在选手代码中进行一次声明便可直接使用。
下文以 T793310 MAL接口测试 为例。
选手只需在自己代码头文件部分之后复制下面的声明:
namespace mal {
namespace remote {
// 高精度整数:内部存十进制串,算法在交互库里。
struct BigInt {
std::string s;
BigInt(const std::string& x = "0") : s(x) {}
BigInt(long long x) : s(std::to_string(x)) {}
std::string to_string() const;
std::string to_string(int base) const;
bool is_zero() const;
int sign() const;
unsigned long long bit_length() const;
};
BigInt operator+(const BigInt& a, const BigInt& b);
BigInt operator-(const BigInt& a, const BigInt& b);
BigInt operator*(const BigInt& a, const BigInt& b);
BigInt operator/(const BigInt& a, const BigInt& b);
BigInt operator%(const BigInt& a, const BigInt& b);
BigInt operator-(const BigInt& a);
BigInt operator<<(const BigInt& a, std::size_t bits);
BigInt operator>>(const BigInt& a, std::size_t bits);
bool operator==(const BigInt& a, const BigInt& b);
bool operator!=(const BigInt& a, const BigInt& b);
bool operator<(const BigInt& a, const BigInt& b);
bool operator>(const BigInt& a, const BigInt& b);
bool operator<=(const BigInt& a, const BigInt& b);
bool operator>=(const BigInt& a, const BigInt& b);
BigInt abs(const BigInt& a);
BigInt pow(const BigInt& a, unsigned long long e);
BigInt sqrt(const BigInt& a);
BigInt nroot(const BigInt& a, unsigned long long k);
std::ostream& operator<<(std::ostream& os, const BigInt& x);
std::istream& operator>>(std::istream& is, BigInt& x);
// 高精度小数:内部存十进制串 + 二进制有效位数。
struct BigFloat {
std::string s;
int p;
BigFloat(const std::string& x = "0", int p_ = 256) : s(x), p(p_) {}
BigFloat(long long x, int p_ = 256) : s(std::to_string(x)), p(p_) {}
std::string to_string() const;
std::string to_string(int digits) const;
bool is_zero() const;
int sign() const;
int precision() const;
};
BigFloat operator+(const BigFloat& a, const BigFloat& b);
BigFloat operator-(const BigFloat& a, const BigFloat& b);
BigFloat operator*(const BigFloat& a, const BigFloat& b);
BigFloat operator/(const BigFloat& a, const BigFloat& b);
BigFloat operator-(const BigFloat& a);
bool operator==(const BigFloat& a, const BigFloat& b);
bool operator!=(const BigFloat& a, const BigFloat& b);
bool operator<(const BigFloat& a, const BigFloat& b);
bool operator>(const BigFloat& a, const BigFloat& b);
bool operator<=(const BigFloat& a, const BigFloat& b);
bool operator>=(const BigFloat& a, const BigFloat& b);
BigFloat abs(const BigFloat& a);
BigFloat exp(const BigFloat& x);
BigFloat log(const BigFloat& x);
BigFloat sqrt(const BigFloat& x);
BigFloat pow(const BigFloat& x, long long e);
BigFloat pi(int p);
BigFloat ln2(int p);
std::ostream& operator<<(std::ostream& os, const BigFloat& x);
std::istream& operator>>(std::istream& is, BigFloat& x);
} // namespace remote
using remote::BigInt;
using remote::BigFloat;
using remote::abs;
using remote::exp;
using remote::log;
using remote::nroot;
using remote::pi;
using remote::pow;
using remote::ln2;
using remote::sqrt;
} // namespace mal便可直接使用一个常数优秀的高精度整数/小数库。具体来说,选手只需要继续编写如下代码:
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;
std::cin >> T;
while (T--) {
std::string op;
std::cin >> op;
if (op == "+" || op == "-" || op == "*" || op == "/") {
std::string a, b;
std::cin >> a >> b;
mal::BigInt x(a), y(b);
if (op == "+") std::cout << (x + y) << '\n';
else if (op == "-") std::cout << (x - y) << '\n';
else if (op == "*") std::cout << (x * y) << '\n';
else std::cout << (x / y) << '\n';
} else if (op == "nroot") {
std::string a;
long long k;
std::cin >> a >> k;
std::cout << mal::nroot(mal::BigInt(a), (unsigned long long)k) << '\n';
} else {
std::string x;
std::cin >> x;
mal::BigFloat v(x);
if (op == "exp") std::cout << mal::exp(v).to_string(30) << '\n';
else std::cout << mal::log(v).to_string(30) << '\n';
}
}
return 0;
}遂可通过此题。
而选手如果实在不想用这个,也可以自己编写高精度库。
这是 GPT 编写的纯手写高精度库代码的运行记录,未使用 MAL,且经过多轮常数优化,可以认为速度非常优秀。
而这是使用上述 MAL 库代码的运行记录,可以看到速度相较于前者几乎持平甚至超越。
同时最重要的是,后者的代码长度几乎是前者的 。由此也隐含了可以出非人类题而不用担心 std 超出洛谷代码长度限制的想法。
需要注意的是,使用 MAL 辅助出题时需要选择“交互题”标签(由上文介绍的原理显然),但这并不代表题目要以交互题的方式表述,上文的高精度模板题就是一个例子。
如果你对我们的具体模板或者实际使用的交互库代码感兴趣,可以前往我们的 Github 仓库 查看。下文直接给出选手可使用的模板、使用过程中需要声明的函数接口、每个函数对应的功能。
高精度库
todo.
多项式库
todo.
数论库
todo.