C++大数板子
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++大数板子的更多相关文章
- hdu 5666 Segment 俄罗斯乘法或者套大数板子
Segment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem ...
- POJ 1737 Connected Graph (大数+递推)
题目链接: http://poj.org/problem?id=1737 题意: 求 \(n\) 个点的无向简单(无重边无自环)连通图的个数.\((n<=50)\) 题解: 这题你甚至能OEIS ...
- 2019 ICPC 银川站
I. Base62(高精度进制转换) 比赛当时雷菊苣和队长俩人拿着大数板子摸了一百多行(然后在缺少大数板子的情况下雷菊苣一发过了orz) 今天补题随便摸了个高精度进制转换的板子交上去就过了还贼短,, ...
- 模拟赛小结:2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)
比赛链接:传送门 两个半小时的时候横扫了铜.银区的所有题,签到成功混进金区.奈何后面没能开出新的题. 最后一个小时的时候xk灵机一动想出了D题的做法,讨论了一波感觉可行,赶紧去敲.结束前2分钟终于过了 ...
- 树的计数 Prüfer编码与Cayley公式 学习笔记
最近学习了Prüfer编码与Cayley公式,这两个强力的工具一般用于解决树的计数问题.现在博主只能学到浅层的内容,只会用不会证明. 推荐博客:https://blog.csdn.net/moreja ...
- Pollard_rho定理 大数的因数个数 这个板子超级快
https://nanti.jisuanke.com/t/A1413 AC代码 #include <cstdio> #include <cstring> #include &l ...
- hdu_1042(模拟大数乘法)
计算n! #include<cstring> #include<cstdio> using namespace std; ]; int main() { int n; whil ...
- [HNOI 2016]大数
Description 题库链接 给你一个长度为 \(n\) ,可含前导零的大数,以及一个质数 \(p\) . \(m\) 次询问,每次询问你一个大数的子区间 \([l,r]\) ,求出子区间中有多少 ...
- ACM-ICPC 2018 焦作赛区网络预赛 J Participate in E-sports(大数开方)
https://nanti.jisuanke.com/t/31719 题意 让你分别判断n或(n-1)*n/2是否是完全平方数 分析 二分高精度开根裸题呀.经典题:bzoj1213 用java套个板子 ...
随机推荐
- 【Text Justification】cpp
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- WPF调用摄像头
添加程序集:WPFMediaKit.dll 更关键代码如下: 界面设计代码如下: <Window x:Class="摄像头调用.MainWindow" xmlns=" ...
- Python-map、filter、reduce方法
介绍 1.map()函数,会让列表中每一个元素都执行一某个函数(传递1个参数), 并且将执行函数返回的结果(无论是什么结果)放在结果列表中 2.filter()函数,会让列表中的每一个元素都执行一次某 ...
- 201621123033 《Java程序设计》第1周学习总结
1. 本周学习总结 · jdk.jre.jvm的含义及相关概念(具体见下文回答) · 会使用记事本及简单编辑器编写Java程序,理解javac和java命令的含义(具体见下文回答) · java与C语 ...
- Java9最受期待的5大新特性
虽然Java9要等到明年才正式发布,但是现在网上已经有了各种各样的有关Java9新特性的文章了,今天小编也将为大家分享除了通常猜测之外的一些很值得期待的5个新特性. 1.Java + REPL = j ...
- 提取URL字符串的搜索字符串中的参数
function urlArgs() { var args = []; var query = location.search.substring(1); var paris = query.spli ...
- P2625 豪华游轮 (背包$dp$,数学)
题目链接 Solution 贼有意思的一个题目. 可以发现阻止我们走的更远的就是那些需要反向走的路程. 然后发现当角度越接近 \(180^\circ\) ,对我们最终的答案则更优. 所以先是一个背包把 ...
- python登录qq
登录qq的用的是get方法, 首先抓login_sig(某个包中的cookie),接着验证码的包(包含对验证码的校验),,最后计算一个p的加密算法,接着再get请求一个链接 https://ssl.p ...
- AI人工客服开发 小程序智能客服 智能客服微信小程序 智能客服系统怎么做 如何设计智能客服系统
今天我们就来给大家分享下如何做 小程序的智能客服问答系统. 首先请确保你的小程序在线客服已经开通使用,并使用代码自己对接好了,将客户的提问自动做了拦截,拦截到了你自己开发的接口上. 做好了拦截以后,我 ...
- VMware HA 特性
关键特性1.自动检测服务器故障.VMware HA 自动监控物理服务器的可用性.VMware HA 可检测物理服务器故障,并且无需人工干预即可重新启动资源池中其他物理服务器上的新虚拟机.2.自动检测操 ...