C++大数板子

使用样例在主函数里看就好,必要的运算符都重载了。

#include <iostream>
using namespace std;
const int maxn=;/*精度位数,自行调整*/
//1.如果需要控制输出位数的话,在str()里面把len调成需要的位数
//2.很大的位数是会re的,所以如果是幂运算的话,如 计算x^p的位数n, n=p*log(10)x+1;(注意要加一)
//3.还可以加上qmul,取模的过程也就是str(),c_str()再搞一次
class bign{
friend istream& operator>>(istream&,bign&);
friend ostream& operator<<(ostream&,const bign&);
friend bign operator+(const bign&,const bign&);
friend bign operator+(const bign&,int&);
friend bign operator*(const bign&,const bign&);
friend bign operator*(const bign&,int&);
friend bign operator-(const bign&,const bign&);
friend bign operator-(const bign&,int&);
friend bign operator/(const bign&,const bign&);
friend bign operator/(const bign&,int&);
friend bign operator%(const bign&,const bign&);
friend bign operator%(const bign&,int&);
friend bool operator<(const bign&,const bign&);
friend bool operator>(const bign&,const bign&);
friend bool operator<=(const bign&,const bign&);
friend bool operator>=(const bign&,const bign&);
friend bool operator==(const bign&,const bign&);
friend bool operator!=(const bign&,const bign&); private://如果想访问len,改成public
int len,s[maxn];
public:
bign(){
memset(s,,sizeof(s));
len=;
}
bign operator=(const char* num){
int i=,ol;
ol=len=strlen(num);
while(num[i++]==''&&len>)
len--;
memset(s,,sizeof(s));
for(i=; i<len; i++)
s[i]=num[ol-i-]-'';
return *this;
}
bign operator=(int num){
char s[maxn];
sprintf(s,"%d",num);
*this=s;
return *this;
}
bign(int num){
*this=num;
}
bign(const char* num){
*this=num;
}
string str() const{
string res="";
for(int i=; i<len; i++)
res=char(s[i]+'')+res;
if(res=="")
res="";
return res;
}
};
bool operator<(const bign& a,const bign& b){
int i;
if(a.len!=b.len)
return a.len<b.len;
for(i=a.len-; i>=; i--)
if(a.s[i]!=b.s[i])
return a.s[i]<b.s[i];
return false;
}
bool operator>(const bign& a,const bign& b){
return b<a;
}
bool operator<=(const bign& a,const bign& b){
return !(a>b);
}
bool operator>=(const bign& a,const bign& b){
return !(a<b);
}
bool operator!=(const bign& a,const bign& b){
return a<b||a>b;
}
bool operator==(const bign& a,const bign& b){
return !(a<b||a>b);
}
bign operator+(const bign& a,const bign& b){
int up=max(a.len,b.len);
bign sum;
sum.len=;
for(int i=,t=;t||i<up; i++)
{
if(i<a.len)
t+=a.s[i];
if(i<b.len)
t+=b.s[i];
sum.s[sum.len++]=t%;
t/=;
}
return sum;
}
bign operator+(const bign& a,int& b){
bign c=b;
return a+c;
}
bign operator*(const bign& a,const bign& b){
bign res;
for(int i=; i<a.len; i++)
{
for(int j=; j<b.len; j++)
{
res.s[i+j]+=(a.s[i]*b.s[j]);
res.s[i+j+]+=res.s[i+j]/;
res.s[i+j]%=;
}
}
res.len=a.len+b.len;
while(res.s[res.len-]==&&res.len>)
res.len--;
if(res.s[res.len])
res.len++;
return res;
}
bign operator*(const bign& a,int& b){
bign c=b;
return a*c;
}
//只支持大数减小数
bign operator-(const bign& a,const bign& b){
bign res;
int len=a.len;
for(int i=; i<len; i++)
{
res.s[i]+=a.s[i]-b.s[i];
if(res.s[i]<)
{
res.s[i]+=;
res.s[i+]--;
}
}
while(res.s[len-]==&&len>)
len--;
res.len=len;
return res;
}
bign operator-(const bign& a,int& b){
bign c=b;
return a-c;
}
bign operator/(const bign& a,const bign& b){
int i,len=a.len;
bign res,f;
for(i=len-; i>=; i--)
{
f=f*;
f.s[]=a.s[i];
while(f>=b)
{
f=f-b;
res.s[i]++;
}
}
while(res.s[len-]==&&len>)
len--;
res.len=len;
return res;
}
bign operator/(const bign& a,int& b){
bign c=b;
return a/c;
}
bign operator%(const bign& a,const bign& b){
int len=a.len;
bign f;
for(int i=len-; i>=; i--)
{
f=f*;
f.s[]=a.s[i];
while(f>=b)
f=f-b;
}
return f;
}
bign operator%(const bign& a,int& b){
bign c=b;
return a%c;
}
bign& operator+=(bign& a,const bign& b){
a=a+b;
return a;
}
bign& operator-=(bign& a,const bign& b)
{
a=a-b;
return a;
}
bign& operator*=(bign& a,const bign& b){
a=a*b;
return a;
}
bign& operator/=(bign& a,const bign& b){
a=a/b;
return a;
}
bign& operator++(bign& a){
a=a+;
return a;
}
bign& operator++(bign& a,int){
bign t=a;
a=a+;
return t;
}
bign& operator--(bign& a){
a=a-;
return a;
}
bign& operator--(bign& a,int){
bign t=a;
a=a-;
return t;
}
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(){
bign a;
bign b;
cin>>a>>b;
cout<<a%b<<endl;
cout<<a-b<<endl;
return ;
}

C++大数板子的更多相关文章

  1. hdu 5666 Segment 俄罗斯乘法或者套大数板子

    Segment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem ...

  2. POJ 1737 Connected Graph (大数+递推)

    题目链接: http://poj.org/problem?id=1737 题意: 求 \(n\) 个点的无向简单(无重边无自环)连通图的个数.\((n<=50)\) 题解: 这题你甚至能OEIS ...

  3. 2019 ICPC 银川站

    I. Base62(高精度进制转换) 比赛当时雷菊苣和队长俩人拿着大数板子摸了一百多行(然后在缺少大数板子的情况下雷菊苣一发过了orz) 今天补题随便摸了个高精度进制转换的板子交上去就过了还贼短,, ...

  4. 模拟赛小结:2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)

    比赛链接:传送门 两个半小时的时候横扫了铜.银区的所有题,签到成功混进金区.奈何后面没能开出新的题. 最后一个小时的时候xk灵机一动想出了D题的做法,讨论了一波感觉可行,赶紧去敲.结束前2分钟终于过了 ...

  5. 树的计数 Prüfer编码与Cayley公式 学习笔记

    最近学习了Prüfer编码与Cayley公式,这两个强力的工具一般用于解决树的计数问题.现在博主只能学到浅层的内容,只会用不会证明. 推荐博客:https://blog.csdn.net/moreja ...

  6. Pollard_rho定理 大数的因数个数 这个板子超级快

    https://nanti.jisuanke.com/t/A1413 AC代码 #include <cstdio> #include <cstring> #include &l ...

  7. hdu_1042(模拟大数乘法)

    计算n! #include<cstring> #include<cstdio> using namespace std; ]; int main() { int n; whil ...

  8. [HNOI 2016]大数

    Description 题库链接 给你一个长度为 \(n\) ,可含前导零的大数,以及一个质数 \(p\) . \(m\) 次询问,每次询问你一个大数的子区间 \([l,r]\) ,求出子区间中有多少 ...

  9. ACM-ICPC 2018 焦作赛区网络预赛 J Participate in E-sports(大数开方)

    https://nanti.jisuanke.com/t/31719 题意 让你分别判断n或(n-1)*n/2是否是完全平方数 分析 二分高精度开根裸题呀.经典题:bzoj1213 用java套个板子 ...

随机推荐

  1. Spring+SpringMVC+MyBatis+Redis框架学习笔记

    在Java互联网中,以Spring+Spring MVC+MyBatis (SSM) 作为主流框架. SSM+Redis的结构图 在这种框架系统中: Spring IoC 承担了一个资源管理.整合.即 ...

  2. 使用selenium监听每一步操作

    1.创建类LogEventListener.java, 如下: package com.demo; import org.openqa.selenium.By; import org.openqa.s ...

  3. super和final关键字

    一.super关键字 super关键字的使用 JAVA类中使用super来引用父类的属性或者方法,用this来引用当前对象,主要用法: 1.子类的构造函数默认第一行会默认调用父类的无参数构造函数 2. ...

  4. Mac 电脑鼠标和触摸板滚动方向不一致的问题【已解决】

    当我们使用鼠标连接到 MacBook 时,会发现无论怎么设置,鼠标和触摸板的滚动方向都是相反的,导致不能同时使用鼠标和触摸板 解决方法: 我安装了下面的程序,它只允许您反转鼠标的滚动行为: Scrol ...

  5. 第六章 系统配置:DHCP和自动配置

    系统配置:DHCP和自动配置 写在开头:今天和导师见了个面,抛给我一堆材料以及论文,感觉自己学业更加繁重.有些知识现阶段我可能没办法掌握,但是至少在我需要进一步理解它的时候,要知道在哪个地方能够找到. ...

  6. Qt-Creator 加入qwt库

    qwt是基于Qt的开源图表库 从官网下载qwt的源码 http://sourceforge.jp/projects/sfnet_qwt/downloads/qwt/6.1.0/qwt-6.1.0.ta ...

  7. B-Tree索引和Hash索引的区别

    Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,所以 Hash 索引的查询效率要远高于 B-T ...

  8. GCD 开发详情

    目录 一.简介 二.dispatch Queue - 队列 三.dispatch Groups - 组 四.dispatch Semaphores - 信号量 五.dispatch Barriers ...

  9. Nginx出现500错误解决办法

    查看错误日志D:\nginx\logs\error.log 得知:Nginx配置文件中会将路径中的 \t 默认转义成 空格 改为双斜杠就可以了

  10. [洛谷P2801]教主的魔法

    题目大意:有$n$个数,$q$个操作.两种操作: $M\;l\;r\;w:$把$[l,r]$所有数加上$w$ $A\;l\;r\;c:$查询$[l,r]$内大于等于$c$的元素的个数. 题解:分块,对 ...