该模板不是本人的,但是该是加了个头文件哒。不然在某个oj上编译错误。

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
struct bign{
int d[maxn], len;
void clean() { while (len > && !d[len - ]) len--; }
bign() { memset(d, , sizeof(d)); len = ; }
bign(int num) { *this = num; }
bign(char* num) { *this = num; }
bign operator = (const char* num){
memset(d, , sizeof(d)); len = strlen(num);
for (int i = ; i < len; i++) d[i] = num[len - - i] - '';
clean();
return *this;
}
bign operator = (int num){
char s[]; sprintf(s, "%d", num);
*this = s;
return *this;
}
bign operator + (const bign& b){
bign c = *this; int i;
for (i = ; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > ) c.d[i] %= , c.d[i + ]++;
}
while (c.d[i] > ) c.d[i++] %= , c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i + ;
return c;
}
bign operator - (const bign& b){
bign c = *this; int i;
for (i = ; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < ) c.d[i] += , c.d[i + ]--;
}
while (c.d[i] < ) c.d[i++] += , c.d[i]--;
c.clean();
return c;
}
bign operator * (const bign& b)const{
int i, j; bign c; c.len = len + b.len;
for (j = ; j < b.len; j++) for (i = ; i < len; i++)
c.d[i + j] += d[i] * b.d[j];
for (i = ; i < c.len - ; i++)
c.d[i + ] += c.d[i] / , c.d[i] %= ;
c.clean();
return c;
}
bign operator / (const bign& b){
int i, j;
bign c = *this, a = ;
for (i = len - ; i >= ; i--)
{
a = a * + d[i];
for (j = ; j < ; j++) if (a < b*(j + )) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bign operator % (const bign& b){
int i, j;
bign a = ;
for (i = len - ; i >= ; i--)
{
a = a * + d[i];
for (j = ; j < ; j++) if (a < b*(j + )) break;
a = a - b*j;
}
return a;
}
bign operator += (const bign& b){
*this = *this + b;
return *this;
}
bool operator <(const bign& b) const{
if (len != b.len) return len < b.len;
for (int i = len - ; i >= ; i--)
if (d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bool operator >(const bign& b) const{ return b < *this; }
bool operator<=(const bign& b) const{ return !(b < *this); }
bool operator>=(const bign& b) const{ return !(*this < b); }
bool operator!=(const bign& b) const{ return b < *this || *this < b; }
bool operator==(const bign& b) const{ return !(b < *this) && !(b > *this); }
string str() const{
char s[maxn] = {};
for (int i = ; i < len; i++) s[len - - i] = d[i] + '';
return s;
}
}; istream& operator >> (istream& in, bign& x) {
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bign& x)
{
out << x.str();
return out;
}
int main()
{ }

高精度(x ,/, +, -, %)良心模板的更多相关文章

  1. C++高精度整数加减乘除模板

    其中高精度乘法通过了POJ2389,其他没有测过,不过应该是没有问题的. 其中高精度除法返回一对string,分别表示商和余数. 代码: #include <bits/stdc++.h> ...

  2. PAT A1024题解——高精度大数相加模板

    PAT:A1024 Palindromic Number A number that will be the same when it is written forwards or backwards ...

  3. hdu 1316 How many Fibs?(高精度斐波那契数)

    //  大数继续 Problem Description Recall the definition of the Fibonacci numbers:  f1 := 1  f2 := 2  fn : ...

  4. POJ 2325 Persistent Numbers#贪心+高精度除法

    (- ̄▽ ̄)-* 这道题涉及高精度除法,模板如下: ]; ];//存储进行高精度除法的数据 bool bignum_div(int x) { ,num=; ;s[i];i++) { num=num*+ ...

  5. JDOJ 1790: 高精度A-B

    JDOJ 1790: 高精度A-B JDOJ传送门 洛谷 P2142 高精度减法 洛谷传送门 题目描述 高精度减法 输入格式 两个整数a,b(第二个可能比第一个大) 输出格式 结果(是负数要输出负号) ...

  6. Noip2016

    <这篇是以前的,不开新的了,借版面来换了个标题> 高二了 开学一周,每天被文化课作业碾压... 但是仍然阻挡不了想刷题的心情... 对付noip2016的几块:(有点少,以后补) 高精度( ...

  7. bc#29 做题笔记

    昨天的bc被坑惨了= = 本来能涨rating的大好机会又浪费了...大号已弃号 A:第一反应是高精度,结果模板找不到了= =,然后现学现卖拍了个java的BigInteger+快速幂,调了好半天不说 ...

  8. 【BZOJ1500】【NOI2005】维修数列(Splay)

    [BZOJ1500][NOI2005]维修数列(Splay) 题面 不想再看见这种毒瘤题,自己去BZOJ看 题解 Splay良心模板题 真的很简单 我一言不发 #include<iostream ...

  9. 【SHOI2012】魔法树(树链剖分,线段树)

    [SHOI2012]魔法树 题面 BZOJ上找不到这道题目 只有洛谷上有.. 所以粘贴洛谷的题面 题解 树链剖分之后直接维护线段树就可以了 树链剖分良心模板题 #include<iostream ...

随机推荐

  1. 将应用代码由eclipse导入Android studio的方法NDK-Build和Cmake两种方法(以android_serialport_api为例)

    网上翻了几百篇博客,看了半天,要不就是写的乱七八糟看不懂,要不就是隐藏了一些细节,要不就是实现不了,最后还是在Android官网上看明白了,而且说得有条有理,以后遇到不懂的一定要先翻官网. 参考资料: ...

  2. Mini2440 通过 SPI 操作 OLED (裸板下使用 SPI 控制器)

    在裸板下使用 SPI 的话,有两种方法可选: 使用 IO 口模拟 SPI 进行操作 使用 SPI 控制器进行操作 这里我们选用控制器的方式,简单方便. 初始化 SPI static void SPIC ...

  3. 模拟HTTP协议接收请求并返回信息

    private string HttpPost(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)W ...

  4. 一个小时学会PHP

    一.PHP概要 PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于学习,使用广 ...

  5. APP接口做什么?

    提交数据:GET POST 有的接口是获取数据和提交数据相结合形式:如检测更新下载

  6. 洛谷P4563 [JXOI2018]守卫(dp)

    题意 题目链接 Sol 非常有意思的题目. 我们设\(f[l][r]\)表示区间\([l,r]\)的答案. 显然\(r\)位置一定有一个保镖 同时不难观察到一个性质:拿\([1, n]\)来说,设其观 ...

  7. 洛谷P4555 [国家集训队]最长双回文串(manacher 线段树)

    题意 题目链接 Sol 我的做法比较naive..首先manacher预处理出以每个位置为中心的回文串的长度.然后枚举一个中间位置,现在要考虑的就是能覆盖到i - 1的回文串中 中心最靠左的,和能覆盖 ...

  8. python 通过 pip 更新所有已安装的包

    较新的 pip 已经支持 list --outdated 了,所以记录一下新的方法: pip list --outdated --format=legacy |awk '{print $1}' |xa ...

  9. React 入门学习笔记整理(五)—— state

    1.state 1)组件本省也是有状态的,定义在组件内部的state中,state的状态只能由组件自身改变,任何其他组件都不能改变. 当需要改变state时,通过调用setState方法来改变,set ...

  10. CSS盒模型的介绍

    CSS盒模型的概念与分类      CSS盒模型就是一个盒子,封装周围的HTML元素,它包括内容content.边框border.内边距padding.外边距margin. CSS盒模型分为标准模型和 ...