该模板不是本人的,但是该是加了个头文件哒。不然在某个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. 2017 ACM/ICPC Asia Regional Qingdao Online解题报告(部分)

    HDU 6206 Apple 题意: 给出四个点的坐标(每个点的坐标值小于等于1,000,000,000,000),问最后一个点是否在前三个点组成的三角形的外接圆内,是输出Accept,否输出Reje ...

  2. .15-浅析webpack源码之WebpackOptionsApply模块-plugin事件流总览

    总体过了一下后面的流程,发现Compiler模块确实不适合单独讲解,这里继续讲解后面的代码: compiler.options = new WebpackOptionsApply().process( ...

  3. What are the differences between struct and class in C++?

    Question: This question was already asked in the context of C#/.Net. Now I'd like to learn the diffe ...

  4. 使用git push命令如何忽略不想提交的文件夹或者文件

    如下场景是在window下的操作. 在使用node的时候有个node_modules文件夹很大,一般情况下不想提交,忽略的办法如: 方法一(来自评论区):直接在仓库根目录:执行命令echo 'node ...

  5. Linux常用基本命令:三剑客命令之-awk输入输出分隔符

    输入分隔符,英文原文为field separator,此处简称为FS,默认是空白字符(即空格),awk默认以空白字符为分隔符对每一行进行分割. 输出分割符,英文原文为output field sepa ...

  6. 初学HTML-8

    video标签:播放视频 格式一:<video src=""> </video> video标签的属性: src:用于告诉video标签需要播放的视频地址. ...

  7. 小结:ES7——async和await初识

    一.async async其实是ES7才有有的关键字,async的意思是异步,顾名思义是有关异步的操作 async用于声明一个函数是异步的. 通常情况下async.await都是跟随promise一起 ...

  8. php获取数据库中数据

    <?php header("Content-type:text/html;charset=utf-8");//字符编码设置 $servername = "local ...

  9. Ubuntu切换root身份,命令行以中文显示

    很多VPS商给的默认用户名并不是root,用以下命令处理即可: 1.修改root密码 sudo passwd root 输入密码,回车,再确认一次即可 2.更改密码后切换root身份 su root ...

  10. python 定时修改数据库

    当需要定时修改数据库时,一般我们都选择起一个定时进程去改库.如果将这种定时任务写入业务中,写成一个接口呢,定时进程显得有些不太合适?如果需要定时修改100次数据库,常规做法会启动100个进程,虽然这种 ...