#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL; struct Bign
{
static const int BASE=;
static const int WIDTH=;
vector<int>s; Bign(LL n=){*this=n;}
Bign(const string& str){*this=str;}
Bign operator =(LL n)
{
s.clear();
do
{
s.push_back(n%BASE);
n/=BASE;
}while(n>);
return *this;
}
Bign operator =(const string& str)
{
s.clear();
int x,len=(str.length()-)/WIDTH+;
for(int i=;i<len;i++)
{
int end=str.length()-i*WIDTH;
int start=max(,end-WIDTH);
sscanf(str.substr(start,end-start).c_str(),"%d",&x);
s.push_back(x);
}
return *this;
}
Bign operator +(const Bign& b)const
{
int len1=s.size(),len2=b.s.size();
int len=max(len1,len2);
int ans=;
Bign c;c.s.clear();
for(int i=;i<len||ans!=;i++)
{
if(i<len1) ans+=s[i];
if(i<len2) ans+=b.s[i];
c.s.push_back(ans%BASE);
ans/=BASE;
}
return c;
}
Bign operator -(const Bign& b)const
{ int len1=s.size(),len2=b.s.size();
Bign c;c.s.clear();
int ans=,t=;
for(int i=;i<len1;i++)
{
if(i<len2) ans=s[i]-b.s[i]+t;
else ans=s[i]+t;
if(ans<)
{
ans+=BASE;t=-;
}
else t=;
if(ans>)c.s.push_back(ans);
}
return c;
}
Bign operator *(const Bign& b)const
{
Bign c;
int len1=s.size(),len2=b.s.size();
for(int i=;i<len2;i++)
{
for(int j=;j<len1;j++)
{
Bign ans=(LL)b.s[i]*s[j];
for(int k=;k<i+j;k++)
ans.s.insert(ans.s.begin(),);
c=c+ans;
}
}
return c;
} bool operator <(const Bign& b)const
{
if(s.size()!=b.s.size()) return s.size()<b.s.size();
for(int i=s.size()-;i>=;i--)
if(s[i]!=b.s[i]) return s[i]<b.s[i];
return false;
}
bool operator ==(const Bign& b)const
{
if(s.size()!=b.s.size()) return false;
for(int i=s.size()-;i>=;i--)
if(s[i]!=b.s[i]) return false;
return true;
}
};
ostream& operator <<(ostream& out,const Bign& a)
{
out<<a.s.back();
char buf[];
int len=a.s.size();
for(int i=len-;i>=;i--)
{
sprintf(buf,"%08d",a.s[i]);
out<<buf;
}
return out;
}
LL BtoL(const Bign& a)
{
LL c;
char buf[];
string ss="";
int len=a.s.size();
sprintf(buf,"%d",a.s.back());ss+=(string)buf;
for(int i=len-;i>=;i--)
{
sprintf(buf,"%08d",a.s[i]);
ss+=(string)buf;
}
sscanf(ss.c_str(),"%lld",&c);
return c;
}
istream& operator >>(istream& in,Bign& a)
{
string s;
in>>s;
a=s;
return in;
}

模板——BigInteger的更多相关文章

  1. POJ 1625 Censored!(AC自动机->指针版+DP+大数)题解

    题目:给你n个字母,p个模式串,要你写一个长度为m的串,要求这个串不能包含模式串,问你这样的串最多能写几个 思路:dp+AC自动机应该能看出来,万万没想到这题还要加大数...orz 状态转移方程dp[ ...

  2. 模板-高精度BigInteger

    #include <bits/stdc++.h> using namespace std; struct BigInteger { static const int BASE = 1000 ...

  3. C++ BigInteger 大整数类模板(转)

    #include <deque> #include <vector> #include <iostream> #include <string> #in ...

  4. 【Java】-BigInteger大数类的使用【超强Java大数模板 总结】

    Scanner cin = new Scanner(new BufferedInputStream(System.in)); 这样定义Scanner类的对象读入数据可能会快一些! 参考这个博客继续补充 ...

  5. C++ BigInteger模板

    #include <cstdio> #include <cstring> #include <string> #include <iostream> # ...

  6. BigInteger

    首先上模板(不断更新中...)(根据刘汝佳AOAPCII修改) #include <iostream> #include <sstream> #include <cstd ...

  7. 高精度模板 Luogu P1932 A+B & A-B & A*B & A/B Problem

    P1932 A+B & A-B & A*B & A/B Problem 题目背景 这个题目很新颖吧!!! 题目描述 求A.B的和差积商余! 输入输出格式 输入格式: 两个数两行 ...

  8. Java 大数、高精度模板

    介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...

  9. 大数模板 poj3982

    1. 这个模板不是自己写的,转载的别人转载的,还没学完c++的我,想写也没有那能力. 这个模板我用在了POJ的一道题上,传送门--POJ3982 一般大数的题,都可用这个模板解决,仅仅须要改动主函数就 ...

随机推荐

  1. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

  2. 【水滴石穿】react-native忽略黄色提醒

    方法一 import { YellowBox } from 'react-native'; YellowBox.ignoreWarnings(['Remote debugger']); // 忽略黄色 ...

  3. Linux之脚本安装包

    1.脚本安装包 脚本安装包不是独立的软软件包类型,常见的安装时源码包 是人为把安装过程写成了自动安装的脚本,只要执行脚本,定义简单的参数,就可以完成安装 类似于windows下的软件安装 一般是硬件驱 ...

  4. golang数据类型三

  5. java读取项目路径下的中文文件乱码问题

    出现乱码错误: 处理方案: 对文件路径中存在中文的,都要进行URLDecoder.decode(path,"UTF-8")编码转换 wordContent = URLEncoder ...

  6. Directx教程(24) 简单的光照模型(3)

    原文:Directx教程(24) 简单的光照模型(3)      在工程myTutorialD3D11_17中,我们重新定义我们的cube顶点法向,每个三角形面的顶点法向都是和这个三角形的面法向是一致 ...

  7. es6中的(=>)箭头函数

    x => x * x 上面的箭头函数相当于: function (x) { return x * x; } 箭头函数相当于匿名函数,并且简化了函数定义. 箭头函数有两种格式,一种像上面的,只包含 ...

  8. No PostCSS Config found解决办法

    npm install报错 Module build failed: Error: No PostCSS Config found 解决办法是同级package.json文件新建postcss.con ...

  9. 1.27eia原油

  10. BKDRHash算法的初步了解

    字符串hash最高效的算法,  搜了一下,  原理是: 字符串的字符集只有128个字符,所以把一个字符串当成128或更高进制的数字来看,当然是唯一的 这里unsigned不需要考虑溢出的问题,  不过 ...