超级福音!!!高进度模板
两大传送门
- 原址传送门(有详细讲解)
- 发现地址传送门
废话不多说,直接上代码!
| #include <bits/stdc++.h> |
| struct BigInt { |
| static const int maxlength = 1005; |
| int num[maxlength], len; |
| void clean() { |
| memset(num, 0, sizeof(num)); |
| len = 1; |
| } |
| BigInt() { |
| clean(); |
| } |
| void read() { |
| char str[maxlength]; |
| scanf("%s", str); |
| len = strlen(str); |
| for (int i = 1; i <= len; i++) { |
| num[i] = str[len - i] - '0'; |
| } |
| } |
| void write() { |
| for (int i = len; i; i--) { |
| printf("%d", num[i]); |
| } |
| putchar('\n'); |
| } |
| void itoBig(int x) { |
| clean(); |
| while (x != 0) { |
| num[len++] = x % 10; |
| x /= 10; |
| } |
| if (len != 1) { |
| len--; |
| } |
| } |
| bool operator <(const BigInt &cmp) const { |
| if (len != cmp.len) { |
| return len < cmp.len; |
| } |
| for (int i = len; i; i--) { |
| if (num[i] != cmp.num[i]) { |
| return num[i] < cmp.num[i]; |
| } |
| } |
| return 0; |
| } |
| bool operator >(const BigInt &cmp) const { |
| return cmp < *this; |
| } |
| bool operator <=(const BigInt &cmp) const { |
| return !(cmp < *this); |
| } |
| bool operator !=(const BigInt &cmp) const { |
| return cmp < *this || *this < cmp; |
| } |
| bool operator ==(const BigInt &cmp) const { |
| return !(cmp < *this || *this < cmp); |
| } |
| BigInt operator +(const BigInt &A) const { |
| BigInt S; |
| S.len = max(len, A.len); |
| for (int i = 1; i <= S.len; i++) { |
| S.num[i] += num[i] + A.num[i]; |
| if(S.num[i] >= 10) { |
| S.num[i] -= 10; |
| S.num[i + 1]++; |
| } |
| } |
| while(S.num[S.len + 1]) S.len++; |
| return S; |
| } |
| BigInt operator -(const BigInt &A) const { |
| BigInt S; |
| S.len = max(len, A.len); |
| for (int i = 1; i <= S.len; i++) { |
| S.num[i] += num[i] - A.num[i]; |
| if(S.num[i] < 0) { |
| S.num[i] += 10; |
| S.num[i + 1]--; |
| } |
| } |
| while (!S.num[S.len] && S.len > 1) { |
| S.len--; |
| } |
| return S; |
| } |
| BigInt operator *(const BigInt &A) const { |
| BigInt S; |
| if ((A.len == 1 && A.num[1] == 0) || (len == 1 && num[1] == 0)) { |
| return S; |
| } |
| S.len = A.len + len - 1; |
| for (int i = 1; i <= len; i++) { |
| for (int j = 1; j <= A.len; j++) { |
| S.num[i + j - 1] += num[i] * A.num[j]; |
| S.num[i + j] += S.num[i + j - 1] / 10; |
| S.num[i + j - 1] %= 10; |
| } |
| } |
| while (S.num[S.len + 1]) { |
| S.len++; |
| } |
| return S; |
| } |
| BigInt operator /(const BigInt &A) const { |
| BigInt S; |
| if ((A.len == 1 && A.num[1] == 0) || (len == 1 && num[1] == 0)) { |
| return S; |
| } |
| BigInt R, N; |
| S.len = 0; |
| for (int i = len; i; i--) { |
| N.itoBig(10); |
| R = R * N; |
| N.itoBig(num[i]); |
| R = R + N; |
| int flag = -1; |
| for (int j = 1; j <= 10; j++) { |
| N.itoBig(j); |
| if(N * A > R) { |
| flag = j - 1; |
| break; |
| } |
| } |
| S.num[++S.len] = flag; |
| N.itoBig(flag); |
| R = R - N * A; |
| } |
| for (int i = 1; i <= (S.len >> 1); i++) { |
| swap(S.num[i], S.num[len - i + 1]); |
| } |
| while (!S.num[S.len] && S.len > 1) { |
| S.len--; |
| } |
| return S; |
| } |
| BigInt operator %(const BigInt &A) const { |
| BigInt S; |
| BigInt P = *this / A; |
| S = *this - P * A; |
| return S; |
| } |
| }; |
| int main(){ |
| |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!