模板——BigInteger
#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的更多相关文章
- POJ 1625 Censored!(AC自动机->指针版+DP+大数)题解
题目:给你n个字母,p个模式串,要你写一个长度为m的串,要求这个串不能包含模式串,问你这样的串最多能写几个 思路:dp+AC自动机应该能看出来,万万没想到这题还要加大数...orz 状态转移方程dp[ ...
- 模板-高精度BigInteger
#include <bits/stdc++.h> using namespace std; struct BigInteger { static const int BASE = 1000 ...
- C++ BigInteger 大整数类模板(转)
#include <deque> #include <vector> #include <iostream> #include <string> #in ...
- 【Java】-BigInteger大数类的使用【超强Java大数模板 总结】
Scanner cin = new Scanner(new BufferedInputStream(System.in)); 这样定义Scanner类的对象读入数据可能会快一些! 参考这个博客继续补充 ...
- C++ BigInteger模板
#include <cstdio> #include <cstring> #include <string> #include <iostream> # ...
- BigInteger
首先上模板(不断更新中...)(根据刘汝佳AOAPCII修改) #include <iostream> #include <sstream> #include <cstd ...
- 高精度模板 Luogu P1932 A+B & A-B & A*B & A/B Problem
P1932 A+B & A-B & A*B & A/B Problem 题目背景 这个题目很新颖吧!!! 题目描述 求A.B的和差积商余! 输入输出格式 输入格式: 两个数两行 ...
- Java 大数、高精度模板
介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...
- 大数模板 poj3982
1. 这个模板不是自己写的,转载的别人转载的,还没学完c++的我,想写也没有那能力. 这个模板我用在了POJ的一道题上,传送门--POJ3982 一般大数的题,都可用这个模板解决,仅仅须要改动主函数就 ...
随机推荐
- 使用Redis管道提升性能
首发于 樊浩柏科学院 Redis 的 管道 (pipelining)是用来打包多条无关命令批量执行,以减少多个命令分别执行带来的网络交互时间.在一些批量操作数据的场景,使用管道可以显著提升 Redis ...
- c中函数指针和回调函数
函数指针: 指向函数的指针.(定义的函数会分配一块内存,同变量一样存在首地址)示例如下: int Func(int x); /*声明一个函数*/ int (*p) (int x); /*定义一个函数指 ...
- 云服务器 ECS Linux Web 环境配置站点的方法
摘自:https://help.aliyun.com/knowledge_detail/41100.html ECS Linux 系统一键安装 Web 环境<专业版>下 Tomcat 添加 ...
- 【CF Manthan, Codefest 17 B】Marvolo Gaunt's Ring
[链接]h在这里写链接 [题意] 给你n个数字; 让你在其中找出三个数字i,j,k(i<=j<=k); 使得p*a[i]+q*a[j]+r*a[k]最大; [题解] /* 有一个要 ...
- 如何用Excel打开CSV文件
如何用Excel打开CSV文件? CSV文件一般是MS-SQL 导出查询数据的一种格式.格式结构是 用逗号分隔数据,如果直接用Excel打开那么数据不会自动分列.需要进行一定的设置.下面是设置过程. ...
- 构造器 构造方法 constructor
构造器的作用: 1.创建对象. 设计类时,若不显示的声明类的构造器的话,程序会默认提供一个空参的构造器. 一旦显示的定义了构造器,就不再默认提供. 声明类的构造器:权限修饰符 与类同名(形参){} 类 ...
- parkingLot
一个支付宝停车支付生活号前端页面 //index.html //自定义键盘 <!DOCTYPE html> <html> <head> <meta chars ...
- 散列表(Hash Table)
散列表(hash table): 也称为哈希表. 根据wikipedia的定义:是根据关键字(Key value)而直接访问在内存存储位置的数据结构.也就是说,它通过把键值通过一个函数的计算,映射到表 ...
- Flask第一篇
一. Python 现阶段三大主流Web框架 Django Tornado Flask 对比 1.Django 主要特点是大而全,集成了很多组件,例如: Models Admin Form 等等, 不 ...
- [React Native]高度自增长的TextInput组件
之前我们学习了从零学React Native之11 TextInput了解了TextInput相关的属性. 在开发中,我们有时候有这样的需求, 希望输入区域的高度随着输入内容的长度而增长, 如下: 这 ...