世界上最高性能的函数库——走起!
2026-03-12 20:22:06
发布于:浙江
该代码消耗了我2个月寿命,功能可以说是肥肠全面
好了,废话不多说,代码如下:
#include <cstdio>
#include <cstring>
#include <chrono>
#define lf __float128
#define ld __int128
#define For(a,b,c) for ((a) = (b);(a) <= (c);(a) ++)
lf pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
struct string {
char r1[1001];
// ==================== 构造函数 ====================
string(char c) {
memset(r1, 0, sizeof(r1));
r1[0] = c;
r1[1] = '\0';
}
string(const char* s = "") {
memset(r1, 0, sizeof(r1));
if (s == nullptr) {
r1[0] = '\0';
return;
}
int i = 0;
while (s[i] && i < 1000) {
r1[i] = s[i];
i++;
}
r1[i] = '\0';
}
string(const string& other) {
memset(r1, 0, sizeof(r1));
int i = 0;
while (other.r1[i] && i < 1000) {
r1[i] = other.r1[i];
i++;
}
r1[i] = '\0';
}
// ==================== 赋值运算符 ====================
string& operator=(const string& other) {
if (this != &other) {
int i = 0;
while (other.r1[i] && i < 1000) {
r1[i] = other.r1[i];
i++;
}
r1[i] = '\0';
}
return *this;
}
// ==================== 访问运算符 ====================
char& operator[](int i) { return r1[i]; }
const char& operator[](int i) const { return r1[i]; }
// ==================== 比较运算符 ====================
bool operator==(const string& other) const {
int i = 0;
while (r1[i] && other.r1[i] && r1[i] == other.r1[i]) i++;
return r1[i] == '\0' && other.r1[i] == '\0';
}
bool operator!=(const string& other) const {
return !(*this == other);
}
// ==================== 拼接运算符 ====================
string operator+(const string& other) const {
string res;
int i = 0, j = 0;
while (r1[i] && i < 1000) { res.r1[i] = r1[i]; i++; }
while (other.r1[j] && i < 1000) { res.r1[i] = other.r1[j]; i++; j++; }
res.r1[i] = '\0';
return res;
}
string& operator+=(const string& other) {
*this = *this + other;
return *this;
}
string& operator+=(const char* s) {
*this = *this + string(s);
return *this;
}
string& operator+=(char c) {
*this = *this + string(c);
return *this;
}
// ==================== 查询方法 ====================
int size() const {
int len = 0;
while (r1[len] != '\0') len++;
return len;
}
bool empty() const {
return r1[0] == '\0';
}
const char* c_str() const {
return r1;
}
// ==================== 修改方法 ====================
void clear() {
r1[0] = '\0';
};
void push_back(char c) {
int len = size();
if (len < 1000) {
r1[len] = c;
r1[len + 1] = '\0';
}
};
// ==================== 子串 ====================
string substr(int pos, int len = -1) const {
string res;
int sz = size();
if (pos < 0) pos = 0;
if (pos >= sz) return res;
if (len < 0 || pos + len > sz) len = sz - pos;
int i = 0;
while (i < len && pos + i < 1000) {
res.r1[i] = r1[pos + i];
i++;
}
res.r1[i] = '\0';
return res;
}
// ==================== 查找 ====================
int find(const string& sub) const {
int n = size(), m = sub.size();
if (m == 0) return 0;
if (m > n) return -1;
for (int i = 0; i <= n - m; i++) {
bool found = true;
for (int j = 0; j < m; j++) {
if (r1[i + j] != sub.r1[j]) {
found = false;
break;
}
}
if (found) return i;
}
return -1;
}
};
// ==================== 全局运算符 ====================
inline string operator+(const char* s, const string& str) {
return string(s) + str;
}
inline string operator+(char c, const string& str) {
return string(c) + str;
}
// ==================== 快读 ====================
inline ld read_d() {
ld x = 0, f = 1;
char c = getchar_unlocked();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar_unlocked(); }
while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = getchar_unlocked(); }
return x * f;
}
inline lf read_f() {
ld dp = read_d();
lf fp = 0, base = 0.1;
char c;
while ((c = getchar_unlocked()) >= '0' && c <= '9') {
fp += (c - '0') * base;
base *= 0.1;
}
return dp + fp;
}
inline string read_s() {
string a;
char b;
int cnt = 0;
while (cnt < 1000) {
b = getchar_unlocked();
if (b == EOF || b == ' ' || b == '\n' || b == '\r') break;
a.r1[cnt++] = b;
}
a.r1[cnt] = '\0'; // 确保结束符
return a;
}
inline void getline(string& s) {
string a;
char b;
int cnt = 0;
while (cnt < 1000) {
b = getchar_unlocked();
if (b == EOF || b == '\n' || b == '\r') break;
a.r1[cnt++] = b;
}
a.r1[cnt] = '\0';
s = a;
}
// ==================== 快写 ====================
void write_d(ld 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(lf x) {
if (x == 0.0) { putchar_unlocked('0'); return; }
if (x < 0) { putchar_unlocked('-'); x = -x; }
ld ip = (ld)x;
lf fp = x - (ld)x;
write_d(ip);
putchar_unlocked('.');
lf ep = 1e-35;
while (fp > ep) {
fp *= 10.0;
ld digit = (ld)fp;
putchar_unlocked('0' + digit);
fp -= digit;
}
}
inline void write_p(lf x, int max_decimal) {
if (x == 0.0) { putchar_unlocked('0'); return; }
if (x < 0) { putchar_unlocked('-'); x = -x; }
ld int_part = (ld)x;
lf frac_part = x - int_part;
write_d(int_part);
if (max_decimal <= 0) return;
putchar_unlocked('.');
for (int i = 0; i < max_decimal; i++) {
frac_part *= 10.0;
ld digit = (ld)frac_part;
putchar_unlocked('0' + digit);
frac_part -= digit;
}
}
inline void write_s(string a) {
for (int i = 0; i < a.size(); i++) putchar_unlocked(a.r1[i]);
}
// ==================== 数学函数 ====================
void swap_d(ld& a, ld& b) {
ld c = a; a = b; b = c;
}
void swap_f(lf& a, lf& b) {
lf c = a; a = b; b = c;
}
void swap_c(char& a, char& b) {
char c = a; a = b; b = c;
}
void reverse(string& s) {
int len = s.size();
for (int i = 0; i < len / 2; i++) swap_c(s.r1[i], s.r1[len - 1 - i]);
}
ld sum_d(ld a[], ld n) {
ld sum = 0;
for (int i = 0; i < n; i++) sum += a[i];
return sum;
}
lf sum_f(lf a[], ld n) {
lf sum = 0;
for (int i = 0; i < n; i++) sum += a[i];
return sum;
}
lf sqrt(lf x) {
if (x <= 0) return x < 0 ? -1.0 : 0.0;
lf r = x, dp = 1e-35;
while (r * r > x ? r * r - x > dp : x - r * r > dp) {
lf r_new = 0.5 * (r + x / r);
if (r_new == r) break;
r = r_new;
}
return r;
}
ld pow_d(ld a, ld b) {
if (b < 0) return 0;
if (b == 0) return 1;
ld c = 1;
while (b > 0) {
if (b & 1) c *= a;
a *= a;
b >>= 1;
}
return c;
}
lf pow_f(lf a, ld b) {
if (b == 0) return 1.0;
bool neg_exp = b < 0;
ld exp = neg_exp ? -b : b;
lf result = 1.0, base = a;
while (exp > 0) {
if (exp & 1) result *= base;
base *= base;
exp >>= 1;
}
return neg_exp ? 1.0 / result : result;
}
ld gcd(ld a, ld b) {
while (b) { ld r = a % b; a = b; b = r; }
return a;
}
ld lcm(ld a, ld b) {
return a / gcd(a, b) * b;
}
int main() {
}
全部评论 2
莫非真是高玩



1周前 来自 重庆
2回赞,谢谢
1周前 来自 浙江
1并非高玩,但是高玩
1周前 来自 浙江
1何意味
6天前 来自 重庆
1
快读性能不算好,用数字判断比用函数判断慢很多,而且运算改为位运算会快一些,此外 ch 可以定义为 int 类型减少一丁点开销
1周前 来自 广东
2
1周前 来自 重庆
0我希望你退役了不要天天来参与灌水
1周前 来自 广东
0其实你看看他的个签呢()
6天前 来自 浙江
0































有帮助,赞一个