模板——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 一般大数的题,都可用这个模板解决,仅仅须要改动主函数就 ...
随机推荐
- day38 16-Spring的Bean的装配:注解的方式
Struts 2和hibernate也使用注解,但是使用注解在以后的开发中应用不多.但是可以说在整合的时候如何进行注解开发.在Spring中,注解必须会玩. package cn.itcast.spr ...
- liferay 7用OSGi的方式修改默认权限
事先声明,支持这一功能的新版本还没有发布,2017年5月份才支持的 1.以前在62的时候是通过修改ext项目进行修改 2.现在在70可以通过Module Fragment项目进行修改 相关文件:现在出 ...
- PHP核心编程--文件上传(包含多文件上传)
一.单文件上传 图片上传界面: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- 泛型List 扩展 比较类
List<string> outputList = resultList.Distinct(new Compare<string>((x, y) => (null != ...
- 2018-9-2-WPF-开发自动开机启动程序
title author date CreateTime categories WPF 开发自动开机启动程序 lindexi 2018-09-02 15:10:52 +0800 2018-9-2 14 ...
- 【记录Bug】 This is probably not a problem with npm. There is likely additional logging output above.
一个eslint的错误 我的报错如下 $ npm install > node-sass@4.11.0 install C:\Users\Administrator\Desktop\forGit ...
- php 明天,下个星期,时间函数
例子:<?php $nextWeek = time() + (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs echo 'Now: ...
- Python发送邮件1(带附件的)
普通的发邮件(不使用类)
- C++复制初始化的限制
相比于直接初始化,复制初始化有更加严格的限制. 1:在复制初始化时,不能使用声明为explicit的构造函数进行的隐式转换.而直接初始化则是允许的: struct Exp { explicit Exp ...
- LeetCode110 Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...