压位高精模板(支持加法、高精乘低精、高精除低精)
2017-09-05 16:11
246 查看
这里的写法是针对低精乘数小于1e8的情况
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<set> #include<map> #include<vector> #include<cctype> #include<ctime> using namespace std; const int maxn=10005; const int maxl=205; const int carry=1e8; const int PP=8; struct bign{ int z[maxl],len; bign(){ memset(z,0,sizeof(z)); len=0; } bign(int x) { *this=x; } bign operator = (int x) { bign re; re.len=1; re.z[0]=x; *this=re; } bign operator = (char *c) { int len=strlen(c); char tmp[PP+5]; bign now; memset(tmp,0,sizeof(tmp)); while(len>=PP) { strncpy(tmp,c+len-PP,PP); now.z[now.len++]=atoi(tmp); len-=PP; } if(len) { memset(tmp,0,sizeof(tmp)); strncpy(tmp,c,len); now.z[now.len++]=atoi(tmp); } *this=now; } friend bign operator + (bign x,bign y) { bign re; re.len=max(x.len,y.len)+1; for(int i=0;i<re.len;i++) { re.z[i]+=x.z[i]+y.z[i]; if(re.z[i]>=carry) re.z[i]-=carry,re.z[i+1]++; } while(re.len>1 && re.z[re.len-1]==0) re.len--; return re; } friend bign operator += (bign &x,bign y) { x=x+y; return x; } friend bign operator * (bign x,int y) { LL tmp; bign re; re.len=x.len+1; for(int i=0;i<x.len;i++) { tmp=(LL)x.z[i]*y; if(tmp+re.z[i]>=carry) re.z[i+1]=(re.z[i]+tmp)/carry,re.z[i]=(re.z[i]+tmp)%carry; else re.z[i]+=tmp; } while(re.len>1 && re.z[re.len-1]==0) re.len--; return re; } friend bign operator / (bign x,int y) { LL tmp=0; bign re; int pos=x.len-1; while(pos>=0) { tmp*=carry; re.z[pos]+=(x.z[pos]+tmp)/y; tmp=(x.z[pos]+tmp)%y; pos--; } re.len=x.len; while(re.len>1 && re.z[re.len-1]==0) re.len--; return re; } friend bool operator < (bign x,bign y) { if(x.len!=y.len) return x.len<y.len; for(int i=x.len-1;i>=0;i--) if(x.z[i]!=y.z[i]) return x.z[i]<y.z[i]; return 0; } void out() { printf("%d",z[len-1]); for(int i=len-2;i>=0;i--) printf("%0*d",PP,z[i]); } }; int main() { return 0; }
相关文章推荐
- 用大数加法模板计算很大的斐波那契数列
- Django项目实践2 - Django模板(网页多语种支持/国际化)
- hdu_1041(Computer Transformation) 大数加法模板+找规律
- 线段树(加法和乘法的模板)
- jfinal-mailer一款支持线程池的异步邮件,可以使用freemarker作为模板的邮件插件
- 支持引用计数的智能指针类模板
- mojoportal学习之特色模块对Artisteer2.4生成的模板的支持
- 通过python群发邮件,支持模板
- [线段树](模板题)线段树1(或称线段树:加法)
- 为什么模板不支持分离编译?
- 为什么C++编译器不能支持对模板的分离式编译_刘未鹏
- 高精模板
- 为什么C++编译器不能支持对模板的分离式编译
- 为什么C++编译器不能支持对模板的分离式编译
- 让gcc支持成员函数模板的trick
- werkzeug实现简单Python web框架(2):添加jinjia2模板支持
- struts2 支持velocity的模板布局功能
- Angular4_下拉框多选(支持响应式表单验证和模板驱动表单验证)
- ASP.NET 移动控件设备模板支持
- flexBuilder3中生成的模板页不支持flash全屏的修改办法