基于大整形的运算收录

news2024/10/1 23:34:46

目录

目录

目录

前言

为什么要大整数

大整形的加/减法

大整形的乘法

大整形除法

大整形开方

代码实现


前言

好久没有更新博客了,hhh。时隔三个月,我又回来了。先来点简单的大整形,虽说简单,但是在编写的时候还是debug了好久。

申明:本文代码为博主自行编写,尚有不足还望海涵,也希望大佬可以指点一二。

为什么要大整数

这是一个令人尴尬的问题。个人看法是除了竞赛大概率碰不上大整形,或者使用c/c++处理大整形的几率很小。我想有的小伙伴要问了,为什么说c/c++处理大整形的几率很小。请看c/c++的语言标准。时至今日,c/c++语言标准都没有明确规定相关生产商必须提供大整形。而在实际的生产中,只有GCC一方提供了“大整形” -- 128位整形。事实上,128位整型并不是内嵌的、官方认定的类型,换句话说只要128位系统没出现,128位类型就不能内嵌,一定是一个认为实现的标准库。在现实生活中,64位整数已经能够处理我们日常生活中的事情。正如有人调侃,微软当初使用64位整型是因为需要64位来存放比尔盖茨的财产,但是32位对于我们普通人来说足够了。由此可见,实际生活没有那么多的大整形。凡事皆有例外,为金融、航天等机密仪器所设计的程序可能会面临大整形、高精度的需求。因此,你发现python在金融称王称霸是合理的。python处理大整形、高精度有着天然优势。但是在精密仪器上应该还是c/cpp开发的程序较多。

总而言之,就是当64位整数不能够满足需求时,就需要按需设计一个存储结构。这个存储结构就是大整形。顺便一提这里不建议使用128位整数,因为可移植性比较差。

大整形的加/减法

大整形的加减法是最为简单的,简单来说就是按位相加减,事后借进位

大整形的乘法

乘法的实现也比较低,相较于加减法难一丢丢。如果我们有如下的式子:

number=a_n*base^{n}+a_{n-1}*base^{n-1}+\dots+a_{0}*base^{0}

那么,现在有number_{1},number_{2}可以像如上式子表达。为了方便表示结果存储在 result ,并且number[i]=a_{i}*base^{i}

于是我们知道number_{1}*number_{2}=\sum_{i=0}^{n_{1}}(number_{1}[i])*\sum_{i=0}^{n_{2}}(number_{2}[i]),进一步推导得到result[k]=\sum_{i}^{k}(number_{1}[i]*number_{2}[k-i])

大整形除法

除法最简单的直观的方式就是位对齐,减试商。这里还可以来一个小优化,就是试商的时候可以不用循环尝试,使用二分搜索尝试,这样会快一点。因为我们的基底不一定是10。如果是大基底循环试商就太慢了,而选用小基底空间上又太浪费。

当然还有数学方法,如果基于数学算法,大整形的运算速度都可以提升到O(nlog^{n})。但是,我太菜了,不能理解。所以就不出来瞎掰扯了,误人子弟了。

大整形开方

这里也是使用了试根法。也有数学方法来着,不过是基于除法实现。so,除法不懂,这里就更不懂了(苦笑ing...)。

代码实现

1000位(十进制)以内目前没发现bug。仍有待测试和完善,乘法计算速度尚可。里面的减法只实现了移位减法供除法使用。一般的减法设计可参看加法 + 移位减法中的检测机制。

里面虽然是无符号类型,但是无符号和有符号的计算效果是一样的。也就是说,如果你需要设计有符号的大整形,可以标注最高位的无符号第1个二进制位就是符号位。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>

class BigInt : public std::vector<unsigned long long> {
public:
  BigInt(unsigned long long n = 0) {
	int i = 0;
	do {
	  push_back(0);
	  at(i++) = n % base;
	  n /= base;
	} while (n);
  };
  BigInt(char* s) {
	int len = strlen(s);
	for (int i = len - 1; i >= 0; i -= log_10_base) {
	  unsigned long long num = 0;
	  for (int j = i - log_10_base + 1 < 0 ? 0 : i - log_10_base + 1; j <= i; ++j)
		num = num * 10 + (s[j] - '0');
	  push_back(num);
	}
  }

  const int theBase() const { return base; };

  // 赋值号
  BigInt& operator = (const BigInt& other) {
	for (int i = 0, j = 0; i < other.size(); ++i, ++j) {
	  if (i >= size()) push_back(0);
	  at(j) = other[i];
	}
	for (int i = other.size(); i < size(); ++i) pop_back();
	return *this;
  };
  // 加法类
  BigInt operator+ (const BigInt& other) {
	int min_digital = std::min(size(), other.size());
	int max_digital = std::max(size(), other.size());
	BigInt ret;
	for (int i = max_digital - 1; i > 0; --i) ret.push_back(0);
	for (int i = 0; i < min_digital; ++i) ret[i] = at(i) + other[i];
	for (int i = min_digital; i < size(); ++i) ret[i] = at(i);
	for (int i = min_digital; i < other.size(); ++i) ret[i] = other[i];
	ret.process();
	return ret;
  };
  BigInt& operator+= (const BigInt& other) {
	int min_digital = std::min(size(), other.size());
	int max_digital = std::max(size(), other.size());
	for (int i = 0; i < min_digital; ++i) at(i) += other[i];
	for (int i = min_digital; i < other.size(); ++i) {
	  push_back(0);
	  at(i) = other[i];
	}
	process();
	return *this;
  };
  // 乘法类
  BigInt operator* (const BigInt& other) {
	BigInt ret;
	for (int i = size() + other.size() - 1; i > 0; --i) ret.push_back(0);
	for (int i = 0; i < size(); ++i)
	  for (int j = 0; j < other.size(); ++j)
		ret[i + j] += at(i) * other[j];

	ret.process();
	return ret;
  };
  BigInt operator* (const long long num) {
	BigInt ret;
	for (int i = size() - 1; i > 0; --i) ret.push_back(0);
	for (int i = 0; i < size(); ++i)
	  ret[i] = at(i) * num;

	ret.process();
	return ret;
  };
  BigInt& operator*= (const int num) {
	for (int i = 0; i < size(); ++i)
	  at(i) *= num;

	process();
	return *this;
  };
  BigInt operator*= (const BigInt& other) {
	BigInt ret;
	for (int i = size() + other.size() - 1; i > 0; --i) ret.push_back(0);
	for (int i = 0; i < size(); ++i)
	  for (int j = 0; j < other.size(); ++j)
		ret[i + j] += at(i) * other[j];

	ret.process();
	for (int i = 0; i < ret.size(); ++i) {
	  if (i >= size()) push_back(0);
	  at(i) = ret[i];
	}
	return *this;
  };
  // 减法类
  // *this - (num << shl) the base is class_base 
  void sub_with_shl(const BigInt& num, int shl) {
	for (int i = 0; i < num.size(); ++i) {
	  unsigned long long check = at(i + shl);
	  at(i + shl) -= num[i];
	  if (check - num[i] > check) { // 如果发生结尾
		at(i + shl) += base;
		at(i + shl + 1) -= 1;
		int higher = i + shl + 1;
		while (at(higher) >= base) { // 是否会产生连续借位
		  at(higher) += base;
		  higher += 1;
		  at(higher) -= 1;
		} // 
	  } // 
	}
	process();
  };
  // 除法类
  // 最简单的方法就是一直循环减
  // 实际上还有数学方法,本人能力有限无法实现,涉及到多项式环快速逆,Crypto的知识。
  BigInt operator/ (BigInt& divisor) {
	if (*this < divisor) return BigInt((unsigned long long) 0);
	BigInt quotiend;
	BigInt remainder = *this;
	int shl = size() - divisor.size(); // 移位
	if (!divisor.less_equal_with_shl(*this, shl)) shl -= 1;
	while (divisor <= remainder) {
	  unsigned long long q = remainder.search_quotient(divisor, shl);
	  remainder.sub_with_shl(divisor * (q - 1), shl);
	  quotiend[quotiend.size() - 1] = q - 1;
	  quotiend.push_back(0);
	  if (shl) shl -= 1;
	}
	quotiend.pop_back();
	quotiend.reverse();
	quotiend.process();
	return quotiend;
  }
  BigInt operator% (BigInt& divisor) {
	if (*this < divisor) return *this;
	BigInt quotiend;
	BigInt remainder = *this;
	int shl = size() - divisor.size(); // 移位
	if (!divisor.less_equal_with_shl(*this, shl)) shl -= 1;
	while (divisor <= remainder) {
	  unsigned long long q = remainder.search_quotient(divisor, shl);
	  remainder.sub_with_shl(divisor * (q - 1), shl);
	  quotiend[quotiend.size() - 1] = q - 1;
	  quotiend.push_back(0);
	  if (shl) shl -= 1;
	}
	return remainder;
  }
  int operator% (int divisor) {
	int r = 0;
	for (int i = size() - 1; i >= 0; --i) {
	  // r = r * base + at(i);
	  r = (r * (base % divisor) + at(i) % divisor) % divisor;
	}
	return r;
  }

  // 开方运算 -- 1000位精确
  BigInt sqrt() {
	BigInt ret;
	int sz = 0;
	if (size() % 2 == 0) ret.resize(sz = size() >> 1);
	else ret.resize(sz = (size() >> 1) + 1);
	for (int i = sz - 1; i >= 0; --i) {
	  search_root_for_ith_digital(ret, i);
	}
	ret.process();
	return ret;
  }

  // 比较类
  bool operator<(const BigInt& other) const {
	if (size() != other.size()) return size() < other.size();
	for (int i = size() - 1; i >= 0; --i)
	  if (at(i) != other[i]) return at(i) < other[i];
	return false;
  }
  bool operator<=(const BigInt& other) const {
	if (size() != other.size()) return size() < other.size();
	for (int i = size() - 1; i >= 0; --i)
	  if (at(i) != other[i]) return at(i) < other[i];
	return true;
  }
  // *this << shl <= other
  bool less_equal_with_shl(const BigInt& other, int shl) const {
	if (size() + shl != other.size()) return size() + shl < other.size();
	for (int i = size() - 1; i >= 0; --i)
	  if (at(i) != other[i + shl]) return at(i) < other[i + shl];
	return true;
  }


  // 输出
  void output() {
	printf("%Id", at(size() - 1));
	for (int i = size() - 2; i >= 0; --i) {
	  for (int j = base / 10; j > 0; j /= 10)
		printf("%Id", at(i) % (j * 10) / j);
	}
	puts("");
  }
private:
  void process() {
	for (int i = 0; i < size(); ++i) {
	  if (at(i) < base) continue;
	  if (i + 1 == size()) push_back(0);
	  at(i + 1) += at(i) / base;
	  at(i) %= base;
	}
	for (int i = size() - 1; at(i) == 0 && i > 0; --i) pop_back();
  }
  // 为除法设计 -- 小优化
  long long search_quotient(BigInt& divisor, int shl) {
	long long l = 1, r = base;
	while (l < r) {
	  long long mid = l + (r - l >> 1);
	  BigInt tmp = divisor * mid;
	  if (tmp.less_equal_with_shl(*this, shl)) l = mid + 1;
	  else r = mid;
	}
	return l;
  }
  BigInt& reverse() {
	for (int i = 0, j = size() - 1; i < j; ++i, --j) {
	  unsigned long long tmp = at(i);
	  at(i) = at(j);
	  at(j) = tmp;
	}
	return *this;
  }
  void search_root_for_ith_digital(BigInt& ret, int i) {
	long long l = 1, r = base;
	while (l < r) {
	  ret[i] = l + (r - l >> 1);
	  BigInt tmp = ret * ret;
	  if (tmp.less_equal_with_shl(*this, 0)) l = ret[i] + 1;
	  else r = ret[i];
	}
	ret[i] = l;
	unsigned long long check = ret[i];
	ret[i] -= 1;
	if (ret[i] > check) {
	  int higner = i + 1;
	  ret[i] += base;
	  ret[higner] -= 1;
	  while (ret[higner] > base) {
		ret[higner] += base;
		higner += 1;
		ret[higner] -= 1;
	  }
	}
	return;
  }
private:
  const static int base = 1000000000;
  const static int log_10_base = 9;
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1402467.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

redis优化系列(六)

本期分享redis内存过期策略&#xff1a;过期key的处理 Redis之所以性能强&#xff0c;最主要的原因就是基于内存存储。然而单节点的Redis其内存大小不宜过大&#xff0c;会影响持久化或主从同步性能。 可以通过修改配置文件来设置Redis的最大内存&#xff1a; maxmemory 1gb …

远程登录Linux服务器:命令+工具版

通常在工作过程中&#xff0c;公司中使用的真实服务器或者是云服务器&#xff0c;都不允许除运维人员之外的员工直接接触&#xff0c;因此就需要通过远程登录的方式来操作。 所以&#xff0c;远程登录工具就是必不可缺的&#xff0c;目前&#xff0c;比较主流的有 Xshell,SSHS…

京东云开发者DDD妙文欣赏(2)报菜名和化繁为简的创新

DDD领域驱动设计批评文集 做强化自测题获得“软件方法建模师”称号 《软件方法》各章合集 京东云开发者原文链接&#xff1a;DDD落地实践-架构师眼中的餐厅>>&#xff0c;以下简称《餐厅》。 我截图时&#xff0c;阅读量有6044&#xff0c;在同类文章中已经算是热文了…

C++ | 头文件

头文件&#xff08;.h)&#xff1a; 写类的声明&#xff08;包括类里面的成员和方法的声明&#xff09;、函数原型、#define常数等&#xff0c;但一般来说不写出具体的实现。 注&#xff1a; 1&#xff09;头文件中只能写声明&#xff0c;不能写定义&#xff1b;2&#xff09…

【论文解读】PV-RCNN: Point-Voxel Feature Set Abstraction for 3D Object Detection

PV-RCNN 摘要引言方法3D Voxel CNN for Efficient Feature Encoding and Proposal GenerationVoxel-to-keypoint Scene Encoding via Voxel Set AbstractionKeypoint-to-grid RoI Feature Abstraction for Proposal Refinement 实验结论 摘要 我们提出了一种新的高性能3D对象检…

Kotlin协程的JVM实现源码分析(下)

协程 根据 是否保存切换 调用栈 &#xff0c;分为&#xff1a; 有栈协程&#xff08;stackful coroutine&#xff09;无栈协程&#xff08;stackless coroutine&#xff09; 在代码上的区别是&#xff1a;是否可在普通函数里调用&#xff0c;并暂停其执行。 Kotlin协程&…

Apifox适用于API测试、管理的工具

一、产品介绍 Apifox是一款强大的API管理工具&#xff0c;它可以帮助开发人员和团队高效地设计、开发、测试、部署和管理API。Apifox提供了丰富的功能&#xff0c;如API文档生成、版本控制、团队协作、性能监控等&#xff0c;让API开发和管理变得更加简单和高效。 二、应用场…

2023 年值得一读的技术文章 | NebulaGraph 技术社区

在之前的产品篇&#xff0c;我们了解到了 NebulaGraph 内核及周边工具在 2023 年经历了什么样的变化。伴随着这些特性的变更和上线&#xff0c;在【文章】博客分类中&#xff0c;一篇篇的博文记录下了这些功能背后的设计思考和研发实践。当中&#xff0c;既有对内存管理 Memory…

【LeetCode】每日一题 2024_1_21 分割数组的最大值(二分)

文章目录 LeetCode&#xff1f;启动&#xff01;&#xff01;&#xff01;题目&#xff1a;分割数组的最大值题目描述代码与解题思路 LeetCode&#xff1f;启动&#xff01;&#xff01;&#xff01; 今天是 hard&#xff0c;难受&#xff0c;还好有题解大哥的清晰讲解 题目&a…

十一、K8S-ingress

目录 一、什么是Ingress 1、为什么要用ingress&#xff1a; 2、ingress概念&#xff1a; 1、pod漂移问题 ​编辑 2、端口管理的问题&#xff1a; 3、域名分配及动态更新问题 3、Ingress-nginx 工作原理 4、ingress-controller工作原理 5、ingress部署原理 1、Deploy…

【UEFI基础】EDK网络框架(TCP4)

TCP4 TCP4协议说明 相比UDP4&#xff0c;TCP4是一种面向连接的通信协议&#xff0c;因此有更好的可靠性。 TCP4的首部格式如下&#xff1a; 各个参数说明如下&#xff1a; 字段长度&#xff08;bit&#xff09;含义Source Port16源端口&#xff0c;标识哪个应用程序发送。D…

爬虫案例—爬取ChinaUnix.net论坛板块标题

爬虫案例—爬取ChinaUnix.net论坛板块标题 ChinaUnix.net论坛网址&#xff1a;http://bbs.chinaunix.net 目标&#xff1a;抓取各个板块的标题和内容的标题 网站截图&#xff1a; 利用requests和xpath实现目标。源码如下&#xff1a; import requests from lxml import etr…

Vue——计算属性

文章目录 计算属性computed 计算属性 vs methods 方法计算属性完整写法 综合案例&#xff1a;成绩案例 计算属性 概念&#xff1a;基于现有的数据&#xff0c;计算出来的新属性。依赖的数据变化&#xff0c;自动重新计算 语法: ①声明computed配置项中&#xff0c;一个计算属性…

vue3-模版引用ref

1. 介绍 概念&#xff1a;通过 ref标识 获取真实的 dom对象或者组件实例对象 2. 基本使用 实现步骤&#xff1a; 调用ref函数生成一个ref对象 通过ref标识绑定ref对象到标签 代码如下&#xff1a; 父组件&#xff1a; <script setup> import { onMounted, ref } …

必看——SSL安全证书

SSL&#xff08;Secure Socket Layer&#xff09;安全证书是一种用于确保在网络上数据传输过程中的安全性和加密性的数字证书。SSL证书通过对数据进行加密&#xff0c;确保敏感信息在用户和服务器之间的传输过程中不被窃取或篡改。下面是获取和配置SSL安全证书的基本步骤&#…

【大数据】YARN常用命令及Rest API

YARN 1.YARN常用命令 1.1 作业 命令说明yarn application -list列出所有的applicationyarn application -list -appStates [ALL、NEW、NEW_SAVING、SUBMITTED、ACCEPTED、RUNNING、FINISHED、FAILED、KILLED]根据application状态过滤yarn application -kill [applicationId]…

【GitHub项目推荐--不错的 C 开源项目】【转载】

大学时接触的第一门语言就是 C语言&#xff0c;虽然距 C语言创立已过了40多年&#xff0c;但其经典性和可移植性任然是当今众多高级语言中不可忽视的&#xff0c;想要学好其他的高级语言&#xff0c;最好是先从掌握 C语言入手。 今天老逛盘点 GitHub 上不错的 C语言 开源项目&…

commit 历史版本记录修正

commit 历史版本记录修正 当 Bug 发生的时候&#xff0c;我们会需要去追踪特定 bug 的历史记录&#xff0c;以查出该 bug 真正发生的原因&#xff0c;这个时候就是版本控制带来最大价值的时候。 因此&#xff0c;要怎样维持一个好的版本记录是非常重要的&#xff0c;下面是一…

第91讲:MySQL主从复制集群主库与从库状态信息的含义

文章目录 1.主从复制集群正常状态信息2.从库状态信息中重要参数的含义 1.主从复制集群正常状态信息 通过以下命令查看主库的状态信息。 mysql> show processlist;在主库中查询当前数据库中的进程&#xff0c;看到Master has sent all binlog to slave; waiting for more u…

通俗易懂理解小波池化/WaveCNet

重要说明&#xff1a;本文从网上资料整理而来&#xff0c;仅记录博主学习相关知识点的过程&#xff0c;侵删。 一、参考资料 github代码&#xff1a;WaveCNet 通俗易懂理解小波变换(Wavelet Transform) 二、相关介绍 关于小波变换的详细介绍&#xff0c;请参考另一篇博客&…