模板——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 一般大数的题,都可用这个模板解决,仅仅须要改动主函数就 ...
随机推荐
- JQuery--关系选择器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- kubernetes1.5新特性跟踪(续)
Kubernetes发布历史回顾 Kubernetes 1.0 - 2015年7月发布 Kubernetes 1.1 - 2015年11月发布 Kubernetes 1.2 - 2016年3月发布 K ...
- 几种常见的flex布局
1,水平等距排列.俩端对齐.垂直方向居顶对齐 html: <div class="container flex"> <div class="div1&q ...
- 【时光回溯】【JZOJ3566】【GDKOI2014】阶乘
题目描述 输入 第一行有一个正整数T,表示测试数据的组数. 接下来的T行,每行输入两个十进制整数n和base. 输出 对于每组数据,输出一个十进制整数,表示在base进制下,n!结尾的零的个数. 样例 ...
- 支付宝sdk iOS 集成
1添加支付宝源文件和库文件AlipayOrder.h AlipayOrder.m AlipayResult.h AlipayResult.m AlixLibService.h ...
- 【JZOJ4878】【NOIP2016提高A组集训第10场11.8】时空传送
题目描述 经过旷久的战争,ZMiG和707逐渐培养出了深厚的感♂情.他们逃到了另一块大陆上,决定远离世间的纷争,幸福地生活在一起.钟情707的neither_nor决心要把他们拆散,他要动用手中最大杀 ...
- 2017校赛 问题 D: 我知道了,你知道了吗?【递归】
题目描述 Alice和Bob走在去学校的路上,听到两个路人的对话: 路人甲:我知道了, 你知道了吗? 路人乙:我知道你知道了,你知道了吗? 路人甲:我知道你知道我知道了,你知道了吗? 路人乙:我知道你 ...
- nodeJs学习-08 cookie、session
http-无状态的:两次访问之间,无区别,cookie可解决 cookie:在浏览器保存一些数据,每次请求都会带过来: 弊端:可以查看修改,并不安全.大小有限(4K) 读取--cookie-parse ...
- iOS图片折叠效果:Layer的contentsRect属性和渐变层
http://www.cocoachina.com/ios/20150722/12622.html 作者:@吖了个峥 授权本站转载. 前言 此次文章,讲述的是Layer的一个属性contentsRec ...
- [Linux]jenkins的安装 标签: linux服务器 2016-08-21 20:47 1060人阅读 评论(23)
现阶段遇到一个问题,因为某台服务器需要腾出来,所以需要将这台服务器上jenkins的主节点重新安装到另外一台服务器上.,下面就介绍一下centos7上面jenkins的安装. 一,步骤 注意:新安装的 ...