超 短 高精度 bign 模板

终于会了,233

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 using namespace std;
  5 struct bign
  6 {
  7     int len;
  8     int num[1501];
  9     bool flag;
 10     bign(){len=1;flag=0;memset(num,0,sizeof num);}
 11     bign(int x)
 12     {
 13         if(!x)return;
 14         len=0;
 15         while(x)
 16         {
 17             num[++len]=x%10;x/=10;
 18         }
 19     }
 20 };
 21 bool operator < (bign a,bign b)
 22 {
 23     if(a.len<b.len)return 1;
 24     if(a.len>b.len)return 0;
 25     for(int i=a.len;i>=1;i--)
 26         if(a.num[i]>b.num[i])return 0;
 27     return 1;
 28 }
 29 bool operator == (bign a,bign b)
 30 {
 31     if(a.len!=b.len)return 0;
 32     for(int i=1;i<=a.len;i++)
 33         if(a.num[i]!=b.num[i])return 0;
 34     return 1;
 35 }
 36 bign operator +(bign &A,bign &B)
 37 {
 38     bign ret;
 39     int i=1,x=0;
 40     while(i<=A.len || i<=B.len)
 41     {
 42         ret.num[i]=A.num[i]+B.num[i]+x;
 43         x=ret.num[i]/10;
 44         ret.num[i]%=10;
 45         i++;
 46     }
 47     ret.num[i]=x;
 48     ret.len=i;
 49     if(!ret.num[ret.len])ret.len--;
 50     return ret;
 51 }
 52 bign operator * (bign a,bign b)
 53 {
 54     bign ans;
 55     int len=a.len+b.len;
 56     for(int i=1;i<=a.len;i++)
 57     {
 58         int x=0;
 59         for(int j=1;j<=b.len;j++)
 60         {
 61             ans.num[i+j-1]+=(a.num[i]*b.num[j]+x);
 62             x=ans.num[i+j-1]/10;
 63             ans.num[i+j-1]%=10;
 64         }
 65         ans.num[i+b.len]+=x;
 66     }
 67     while(!ans.num[len] && len>1)len--;
 68     ans.len=len;
 69     return ans;
 70 }
 71 bign operator - (bign a,bign b)
 72 {
 73     bign ans;
 74     if(a==b)return ans;
 75     if(b<a)
 76     {
 77         for(int i=1;i<=a.len;i++)
 78         {
 79             if(a.num[i]<0)
 80                 a.num[i]+=10,a.num[i+1]--;
 81             ans.num[i]=a.num[i]-b.num[i];
 82             if(ans.num[i]<0)
 83             {
 84                 ans.num[i]+=10;
 85                 a.num[i+1]--;//向a的高位借位
 86             }
 87         }
 88     }
 89     else
 90     {
 91         ans.flag=1;// this number(I mean the ans) is smaller than zero
 92         for(int i=1;i<=b.len;i++)
 93         {
 94             if(b.num[i]<0)
 95             {
 96                 b.num[i]+=10;b.num[i+1]--;
 97             }
 98             ans.num[i]=b.num[i]-a.num[i];
 99             if(ans.num[i]<0)
100             {
101                 ans.num[i]+=10;
102                 b.num[i+1]--;
103             }
104         }
105     }
106     int len=max(a.len,b.len);
107     while(ans.num[len]<=0 && len>1)len--;
108     ans.len=len;
109     return ans;
110 }
111 bign get()
112 {
113     bign ans;
114     string s;
115     cin>>s;
116     int l=s.length();
117     ans.len=l;
118     for(int i=0;i<ans.len;i++)ans.num[ans.len-i]=s[i]-48;
119     return ans;
120 }
121 void out(bign x)
122 {
123     if(x.flag)cout<<"-";
124     for(int i=x.len;i>=1;i--)
125     {
126         cout<<x.num[i];
127     }
128 }
129 int main()
130 {
131     return 0;
132 }

时间: 2024-07-28 14:42:35

超 短 高精度 bign 模板的相关文章

hdu1134 Game of Connections 高精度 四则运算 模板

即卡特兰数. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <stack> #include <cmath> #include <queue> #include <set> #include <map> #define FOR

【转】ACM高精度加减乘除模板

#include <iostream> #include <string> using namespace std; inline int compare(string str1, string str2) { if(str1.size() > str2.size()) //长度长的整数大于长度小的整数 return 1; else if(str1.size() < str2.size()) return -1; else return str1.compare(str

高精度加法模板

只想说这个模板好啊 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; char s[205][200]; void add(char a[],char b[],char back[]) { int i,j,k,up,x,y,z,l; char *c; if (strlen(a)>strlen(b)) l=s

【BZOJ2179】FFT快速傅立叶 高精度乘模板题

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44015679"); } 题解: 其实没什么题解,只是贴个模板+理解注释 代码: #include <cmath> #include <cstdio> #include <cstring> #inc

Hdu 4762 网络赛 高精度大数模板+概率

注意题目中的这句话he put the strawberries on the cake randomly one by one,第一次选择草莓其实有N个可能,以某一个草莓为开头,然后顺序的随机摆放,所以最后的概率为n/m^(n-1),最后通过大数模板搞定该题的化简. C++代码 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<iomanip> 5 #include

[C++]高精度 bign (重载运算符版本)

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define maxn 2000 #define base 10000 struct Bign { int c[maxn],len,sign; //初始化 Bign(){memset(c,0,sizeof(c)),len = 1,sign = 0;} //高位清

HDU1402 FFT高精度乘法模板题

#include<bits/stdc++.h> using namespace std; //HDU 1402 求高精度乘法 const double PI = acos(-1.0); //复数结构体 struct Complex { double x,y;//实部和虚部x+yi Complex(double _x = 0.0,double _y = 0.0) { x = _x; y = _y; } Complex operator -(const Complex &b)const {

高精度加减乘除模板

高精度板子. 我用的是重载运算符. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 5 struct mega 6 { 7 int num[200005]; 8 int len,val; 9 bool anti; 10 void reset() 11 { 12 len=val=0; 13 anti=false; 14 memset(num,0,sizeof(num)); 15 } 16 fri

C++高精度加减乘除模板

其中高精度乘法通过了POJ2389,其他没有测过,不过应该是没有问题的. 其中高精度除法返回一对string,分别表示商和余数. 代码: #include <bits/stdc++.h> using namespace std; const int maxn = 100010; int a[maxn], b[maxn], res[maxn]; string add(string s1, string s2) { // under condition: s1,s2>=0 // 初始化部分