分享1个高性能函数库
2026-02-15 19:36:41
发布于:浙江
这是第3期,优化了所有函数,新增lcm函数,还写了各个函数的用途。
#include <cstdio>
__float128 sqrt_f(__float128 x) { // 高精度浮点数平方根
if (x <= 0) return x < 0 ? -1.0 : 0.0;
__float128 r = x;
__float128 dp = 1e-35;
while (r * r > x ? r * r - x > dp : x - r * r > dp) {
__float128 r_new = 0.5 * (r + x / r);
if (r_new == r) break;
r = r_new;
}
return r;
}
__int128 pow_d(__int128 a, __int128 b) { // 整数次方
if (b < 0) return 0;
if (b == 0) return 1;
__int128 c = 1;
while (b > 0) {
if (b & 1) c *= a;
a *= a;
b >>= 1;
}
return c;
}
__float128 pow_f(__float128 a, __int128 b) { // 高精度浮点数次方
if (b == 0) return 1.0;
bool neg_exp = b < 0;
__int128 exp = neg_exp ? -b : b;
__float128 result = 1.0;
__float128 base = a;
while (exp > 0) {
if (exp & 1) result *= base;
base *= base;
exp >>= 1;
}
if (neg_exp) {
result = 1.0 / result;
}
return result;
}
__int128 gcd(__int128 a, __int128 b) { // 最大公约数
while (b) {
__int128 r = a % b;
a = b;
b = r;
}
return a;
}
__int128 lcm(__int128 a, __int128 b) { // 最小公倍数
return a / gcd(a, b) * b;
}
inline __int128 read_d() { // 整数快读
__int128 c;
while ((c = getchar_unlocked()) < '0' || c > '9');
__int128 x = c - '0';
while ((c = getchar_unlocked()) >= '0' && c <= '9') x = x * 10 + (c - '0');
return x;
}
inline __float128 read_f() { // 高精度浮点数快读
__int128 c = getchar_unlocked();
while (c != '-' && c != '+' && (c < '0' || c > '9') && c != '.')
c = getchar_unlocked();
bool neg = (c == '-');
if (c == '-' || c == '+') c = getchar_unlocked();
__float128 x = 0.0;
while (c >= '0' && c <= '9') {
x = x * 10.0 + (__float128)(c - '0');
c = getchar_unlocked();
}
if (c == '.') {
c = getchar_unlocked();
__float128 base = 0.1;
while (c >= '0' && c <= '9') {
x += (__float128)(c - '0') * base;
base *= 0.1;
c = getchar_unlocked();
}
}
if (c == 'e' || c == 'E') {
c = getchar_unlocked();
bool eneg = (c == '-');
if (c == '-' || c == '+') c = getchar_unlocked();
__int128 exp = 0;
while (c >= '0' && c <= '9') {
exp = exp * 10 + (c - '0');
c = getchar_unlocked();
}
if (eneg) exp = -exp;
__float128 scale = 1.0;
while (exp > 0) { scale *= 10.0; exp--; }
while (exp < 0) { scale *= 0.1; exp++; }
x *= scale;
}
return neg ? -x : x;
}
void write_d(__int128 x) { // 整数快写
if (x == 0) {
putchar_unlocked('0');
return;
}
if (x < 0) {
putchar_unlocked('-');
x = -x;
}
char s[40];
char* t = s + 39;
*t = '\0';
while (x) {
*--t = '0' + x % 10;
x /= 10;
}
while (*t) putchar_unlocked(*t++);
}
inline void write_f(__float128 x) { // 高精度浮点数快写
if (x == 0.0) { putchar_unlocked('0'); return; }
bool neg = x < 0;
if (neg) x = -x;
__int128 ip = (__int128)x;
__float128 fp = x - (__int128)x;
if (neg) putchar_unlocked('-');
write_d(ip);
putchar_unlocked('.');
__float128 xp = fp - (__int128)fp * 1.0;
__float128 ep = 1e-35;
while (xp > ep && fp > ep) {
fp *= 10.0;
xp = fp - (__int128)fp * 1.0;
}
write_d(fp);
}
inline void write_p(__float128 x, int max_decimal) { // 高精度浮点数快写(可控小数位数,max_decimal是小数位数)
if (x == 0.0) {
putchar_unlocked('0');
return;
}
bool neg = x < 0.0;
if (neg) x = -x;
__int128 int_part = (__int128)x;
__float128 frac_part = x - int_part;
if (neg) putchar_unlocked('-');
write_d(int_part);
if (max_decimal <= 0) return;
putchar_unlocked('.');
__int128 cnt = 0;
while (cnt < max_decimal && frac_part > 1e-35) {
frac_part *= 10.0;
__int128 digit = (__int128)frac_part;
putchar_unlocked('0' + digit);
frac_part -= digit;
cnt++;
}
while (cnt < max_decimal) {
putchar_unlocked('0');
cnt++;
}
}
inline void write_s(char a[], __int128 n) { // 字符串快写(n是字符串大小)
for (int i = 0;i < n;i ++) putchar_unlocked(a[i]);
}
int main() {
}
耗时两个月,想要1个点赞!
欢迎给出建议,或是点评!
全部评论 4
欢迎给出建议,或是点评
6天前 来自 浙江
1虽然看不懂但是感觉值得点赞
6天前 来自 江西
1谢谢
6天前 来自 浙江
1
为啥不加代码框,这咋看
6天前 来自 浙江
0加了
6天前 来自 浙江
1
你这个 sqrt 效率真的高吗……STL 库的 sqrt 比你这快多了
6天前 来自 广东
0在更新了
6天前 来自 浙江
1
































有帮助,赞一个