[带符号大整数模板]vector版
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring> using namespace std; typedef vector<int> vi;
typedef long long LL; const int maxI = 1e8;
const int Len = ; struct BigInt { vi num;
bool symbol; BigInt() { num.clear(); symbol = ; }
BigInt(int x) { symbol = ; if (x < ) { symbol = ; x = -x; } num.push_back(x % maxI); if (x >= maxI) num.push_back(x / maxI); }
BigInt(bool s, vi x) { symbol = s; num = x; }
BigInt(char s[]) {
int len = strlen(s), x = , sum = , p = s[] == '-';
symbol = p;
for (int i = len - ; i >= p; i--) {
sum += (s[i] - '') * x;
x *= ;
if (x == 1e8 || i == p) {
num.push_back(sum);
sum = ;
x = ;
}
}
while (num.back() == && num.size() > ) num.pop_back();
} void push(int x) { num.push_back(x); } BigInt abs() const { return BigInt(false, num); } bool smaller(const vi &a, const vi &b) const {
if (a.size() != b.size()) return a.size() < b.size();
for (int i = a.size() - ; i >= ; i--) {
if (a[i] != b[i]) return a[i] < b[i];
}
return ;
} bool operator < (const BigInt &p) const {
if (symbol && !p.symbol) return true;
if (!symbol && p.symbol) return false;
if (symbol && p.symbol) return smaller(p.num, num);
return smaller(num, p.num);
} bool operator > (const BigInt &p) const {
return p < *this;
} bool operator == (const BigInt &p) const {
return !(p < *this) && !(*this < p);
} bool operator >= (const BigInt &p) const {
return !(*this < p);
} bool operator <= (const BigInt &p) const {
return !(p < *this);
} vi add(const vi &a, const vi &b) const {
vi c;
c.clear();
int x = ;
for (int i = ; i < a.size(); i++) {
x += a[i];
if (i < b.size()) x += b[i];
c.push_back(x % maxI);
x /= maxI;
}
for (int i = a.size(); i < b.size(); i++) {
x += b[i];
c.push_back(x % maxI);
x /= maxI;
}
if (x) c.push_back(x);
while (c.back() == && c.size() > ) c.pop_back();
return c;
} vi sub(const vi &a, const vi &b) const {
vi c;
c.clear();
int x = ;
for (int i = ; i < b.size(); i++) {
x += maxI + a[i] - b[i] - ;
c.push_back(x % maxI);
x /= maxI;
}
for (int i = b.size(); i < a.size(); i++) {
x += maxI + a[i] - ;
c.push_back(x % maxI);
x /= maxI;
}
while (c.back() == && c.size() > ) c.pop_back();
return c;
} vi mul(const vi &a, const vi &b) const {
vi c;
c.resize(a.size() + b.size());
for (int i = ; i < a.size(); i++) {
for (int j = ; j < b.size(); j++) {
LL tmp = (LL)a[i] * b[j] + c[i + j];
c[i + j + ] += tmp / maxI;
c[i + j] = tmp % maxI;
}
}
while (c.back() == && c.size() > ) c.pop_back();
return c;
} vi div(const vi &a, const vi &b) const {
vi c(a.size()), x(, ), y(, ), z(, ), t(, );
y.push_back();
for (int i = a.size() - ; i >= ; i--) {
z[] = a[i];
x = add(mul(x, y), z);
if (smaller(x, b)) continue;
int l = , r = maxI - ;
while (l < r) {
int m = (l + r + ) >> ;
t[] = m;
if (smaller(x, mul(b, t))) r = m - ;
else l = m;
}
c[i] = l;
t[] = l;
x = sub(x, mul(b, t));
}
while (c.back() == && c.size() > ) c.pop_back();
return c;
} BigInt operator + (const BigInt &p) const{
if (!symbol && !p.symbol) return BigInt(false, add(num, p.num));
if (!symbol && p.symbol) return *this >= p.abs()? BigInt(false, sub(num, p.num)) : BigInt(true, sub(p.num, num));
if (symbol && !p.symbol) return (*this).abs() > p? BigInt(true, sub(num, p.num)) : BigInt(false, sub(p.num, num));
return BigInt(true, add(num, p.num));
} BigInt operator - (const BigInt &p) const {
return *this + BigInt(!p.symbol, p.num);
} BigInt operator * (const BigInt &p) const {
BigInt res(symbol ^ p.symbol, mul(num, p.num));
if (res.symbol && res.num.size() == && res.num[] == ) res.symbol = false;
return res;
} BigInt operator / (const BigInt &p) const {
if (p == BigInt()) return p;
BigInt res(symbol ^ p.symbol, div(num, p.num));
if (res.symbol && res.num.size() == && res.num[] == ) res.symbol = false;
return res;
} BigInt operator % (const BigInt &p) const {
return *this - *this / p * p;
} void show() const {
if (symbol) putchar('-');
printf("%d", num[num.size() - ]);
for (int i = num.size() - ; i >= ; i--) {
printf("%08d", num[i]);
}
putchar('\n');
} int TotalDigit() const {
int x = num[num.size() - ] / , t = ;
while (x) {
x /= ;
t++;
}
return t + (num.size() - ) * Len;
} };
[带符号大整数模板]vector版的更多相关文章
- [hdu5351]找规律,大整数模板
题意:f(1)="a",f(2)="b",f(i)=f(i-1)+f(i-2),"+"表示连接符.给定n,m,求f(n)的前m个字符的“ne ...
- OpenJudge 2737 大整数除法
链接地址:http://bailian.openjudge.cn/practice/2737/ 题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 求2个大的正整数相除的商 输入 第 ...
- Code Kata:大整数比较大小&大整数四则运算---加减法 javascript实现
大整数的四则运算已经是老生常谈的问题了.很多的库也已经包含了各种各样的解决方案. 作为练习,我们从最简单的加减法开始. 加减法的核心思路是用倒序数组来模拟一个大数,然后将两个大数的利用竖式进行运算. ...
- Ural 1158. Censored! 有限状态自动机+DP+大整数
Ural1158 看上去很困难的一道题. 原文地址 http://blog.csdn.net/prolightsfxjh/article/details/54729646 题意:给出n个不同的字符,用 ...
- 大整数四则运算(vector与数组两种版本实现)
每逢大整数四则运算,都会怯懦,虽是算法竞赛必会的东西,也零散的学过,简单的总结过,但不成体系的东西心里一直没底. 所以今天消耗了大量的卡路里,啃了几套模板之后终于总结成了一套自己的模板 再也不用担心大 ...
- Pollard-Rho大整数拆分模板
随机拆分,简直机智. 关于过程可以看http://wenku.baidu.com/link?url=JPlP8watmyGVDdjgiLpcytC0lazh4Leg3s53WIx1_Pp_Y6DJTC ...
- JAVA版拆分大整数为2幂的和算法
import java.util.ArrayList; import java.util.List; public class StrTest { public static void main(St ...
- 大整数类BIGN的设计与实现 C++高精度模板
首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...
- OpenJudge 2980 大整数乘法
链接地址:http://bailian.openjudge.cn/practice/2980/ 题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 求两个不超过200位的非负整数的积 ...
随机推荐
- srt字幕翻译
需要把字幕名改成i.txt 有有道和谷歌 代码: #Author:Chenglong Qian #Copyright :Chenglong Qian import json import reques ...
- SpringCloud(一)学习笔记之项目搭建
[springcloud项目名称不支持下划线] 一.创建父项目 File---new---project: 填写项目信息: 默认即可,点击finish创建完成: 由于父项目只用到pom文件 所以把sr ...
- Linux-LAMP虚拟主机配置
1.配置用户认证 <Directory /data/discuz/passwd> AllowOverride AuthConfig AuthName "自定义的" Au ...
- pytorch-API实现线性回归
示例: import torch import torch.nn as nn from torch import optim class MyModel(nn.Module): def __init_ ...
- 引导 ARM Linux
引导 ARM Linux 本文翻译自:https://www.kernel.org/doc/html/latest/arm/booting.html 引导 ARM Linux 需要一个引导加载程序,它 ...
- java list随机截取(洗牌)
public void solution(){ List<Integer> givenList = Arrays.asList(1, 2, 3,4,5,6); Collections.sh ...
- Inno Setup 大师 Tlama
https://stackoverflow.com/users/960757/tlama
- 【Linux常见命令】netstat命令
netstat - Print network connections, routing tables, interface statistics, masquerade connections, a ...
- 【Linux常见命令】tail命令
tail - output the last part of files tail 命令可用于查看文件的内容,有一个常用的参数 -f 常用于查阅正在改变的日志文件. tail -f filename ...
- 蘑菇街CEO陈琪上市致辞:科技是生产力 美丽也是生产力
雷帝网 乐天 12月7日报道 蘑菇街CEO陈琪今日在纽交所上市致辞时表示,蘑菇街的使命是让时尚触手可及,立志成为最领先的时尚目的地,并把"科技是生产力,美丽也是生产力"作为蘑菇街价 ...