【高精度-加减乘除-模板】

·座位旁边一个同学,哼着歌,说着:“要用高精度啊,好麻烦,不想写啊啊啊,不想写啊,不想写啊……”他的这首歌持续了大约6min,然后他的DEV里一个漂亮的高精度运算代码已经写好了。

·看来大米饼也不能甘于落后啊,模板来啦:

 1 #include<vector>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #define ll long long
 5 #define go(i,a,b) for(int i=a;i<=b;i++)
 6 #define ro(i,a,b) for(int i=a;i>=b;i--)
 7 using namespace std;const int N=5000003;
 8 char A[N],B[N];
 9 struct Rabbit
10 {
11     static const ll gap=10;
12     static const ll len=1;
13     vector<int>s;Rabbit(ll num=0){*this=num;}
14     int ID(char c){return c-'0';}
15     Rabbit operator=(char *T)
16     {
17         s.clear();int n=strlen(T),_=0,f=1;
18         ro(i,n-1,0){_=_+ID(T[i])*f,f*=10,
19         i==0||(n-1-i)%len==len-1?s.push_back(_),_=0,f=1:1;}
20         return *this;
21     }
22     Rabbit operator=(ll T)
23     {
24         s.clear();do{s.push_back(T%gap),T/=gap;}while(T);return *this;
25     }
26     Rabbit operator+(Rabbit b)
27     {
28         Rabbit c;c.s.clear();
29         int i=0,_=0,n=s.size(),m=b.s.size(),w;
30         while(_||i<n||i<m)w=_,i<n?w+=s[i]:1,i<m?w+=b.s[i]:1,
31         c.s.push_back(w%gap),_/=gap,i++;return c;
32     }
33     Rabbit operator-(Rabbit b)
34     {  
35         Rabbit c;c.s.clear();
36         int i=0,g=0,n=s.size(),m=b.s.size(),w;
37         while(i<n)w=s[i]-g,i<m?w-=b.s[i]:1,  
38         w<0?g=1,w+=gap:g=0,c.s.push_back(w),i++; 
39         i=c.s.size()-1;while(c.s[i]==0&&i)c.s.pop_back(),i--;
40         return c; 
41     }
42     Rabbit operator*(Rabbit b)
43     {
44         Rabbit c;int n=s.size(),m=b.s.size();c.s.resize(m+n,0);
45         go(i,0,n-1)go(j,0,m-1)c.s[i+j]+=s[i]*b.s[j];
46         go(i,0,n+m-1)c.s[i+1]+=c.s[i]/gap,c.s[i]%=gap;
47         int i=n+m-1;while(c.s[i]==0&&i)c.s.pop_back(),i--;
48         return c;
49     }
50     Rabbit operator/(Rabbit b)
51     {
52         Rabbit c,d=0;int n=s.size();c.s.resize(n,0);
53         ro(i,n-1,0){d=d*gap;d.s[0]=s[i];
54         while(d>=b)d-=b,c.s[i]++;}
55         int i=n-1;while(c.s[i]==0&&i)c.s.pop_back(),i--;
56         return c;
57     }
58     Rabbit operator%(Rabbit b){Rabbit w=*this/b;w=*this-w*b;return w;}
59     bool operator<(const Rabbit& b)const
60     {
61         int n=s.size(),m=b.s.size();
62         if(m>n)return 1;if(m<n)return 0;ro(i,n-1,0){
63         if(s[i]<b.s[i])return 1;
64         if(s[i]>b.s[i])return 0;}return 0;
65     }
66     bool operator> (Rabbit b)const{return b<*this;}
67     bool operator>=(Rabbit b)const{return !(*this<b);}
68     bool operator<=(Rabbit b)const{return !(b<*this);}
69     bool operator==(Rabbit b)const{return !(*this<b)&&!(b<*this);}
70     bool operator!=(Rabbit b)const{return (*this<b)||(b<*this);}
71     
72     Rabbit operator+=(Rabbit b){*this=*this+b;return *this;}
73     Rabbit operator-=(Rabbit b){*this=*this-b;return *this;}
74     Rabbit operator*=(Rabbit b){*this=*this*b;return *this;}
75     Rabbit operator/=(Rabbit b){*this=*this/b;return *this;}
76     Rabbit operator%=(Rabbit b){*this=*this%b;return *this;}
77 
78     void Print(){int n=s.size();ro(i,n-1,0)printf("%d",s[i]);puts("");}
79 };
80 int main()
81 {
82     
83     scanf("%s%s",A,B);
84     Rabbit a,b;a=A;b=B;
85     (a/b).Print();//b.Print();
86     return 0;
87 }//Paul_Guderian

条,长长的街道,留下我多少青春和梦想,
它会把我,带向,何方。——————汪峰《街道》

原文地址:https://www.cnblogs.com/Paul-Guderian/p/7308518.html