[板子]高精度

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char ch[500000];
 4 struct node{
 5     int s[1000000],len;
 6     void init(){
 7         scanf("%s",ch+1);
 8         len=strlen(ch+1);
 9         for(int i=1;i<=len;++i) s[len-i+1]=ch[i]-48;
10     }
11     void multi(){
12         ++len;
13         for(int i=len;i;--i) s[i]=s[i-1];
14     }
15     void divid(){
16         for(int i=1;i<=len;++i) s[i]=s[i+1];
17         --len;
18     }
19     friend bool operator <= (const node &a,const node &b){
20         if(a.len^b.len) return a.len<b.len;
21         for(int i=a.len;i;--i) if(a.s[i]^b.s[i]) return a.s[i]<b.s[i];
22         return 1;
23     }
24     friend void operator -= (node &a,const node &b){
25         for(int i=1;i<=b.len;++i) a.s[i]-=b.s[i];
26         for(int i=1;i<=a.len;++i) if(a.s[i]<0) a.s[i]+=10,a.s[i+1]--;
27         while(a.len!=1&&a.s[a.len]==0) a.len--;
28     }
29     friend void operator += (node &a,const node &b){
30         a.len=max(a.len,b.len)+1;
31         for(int i=1;i<=a.len;++i) a.s[i]+=b.s[i];
32         for(int i=1;i<=a.len;++i) if(a.s[i]>9) a.s[i]-=10,a.s[i+1]++;
33         while(a.len!=1&&a.s[a.len]==0) a.len--;
34     }
35     friend void operator *= (node &a,const node &b){
36         node c;c.len=a.len*b.len+10;
37         for(int i=1;i<=a.len;++i)
38             for(int j=1;j<=b.len;++j)
39                 c.s[i+j-1]+=a.s[i]*b.s[j];
40         for(int i=1;i<=c.len;++i) if(c.s[i]>9) c.s[i+1]+=c.s[i]/10,c.s[i]%=10;
41         while(c.len!=1&&c.s[c.len]==0) c.len--;
42         a=c;
43     }
44     friend void operator %= (node &a,const node &d){
45         node b=d;int x=b.len;
46         while(b<=a) b.multi();b.divid();
47         node c;c.s[c.len=0]=0;
48         while(b.len>=x){
49             ++c.len;
50             while(b<=a) a-=b,++c.s[c.len];
51             b.divid();
52         }
53         c.len=max(c.len,1);//防止a<d的情况出不来数
54     }
55     friend void operator /= (node &a,const node &d){
56         node b=d;int x=b.len;
57         while(b<=a) b.multi();b.divid();
58         node c;c.s[c.len=0]=0;
59         while(b.len>=x){
60             ++c.len;
61             while(b<=a) a-=b,++c.s[c.len];
62             b.divid();
63         }
64         c.len=max(c.len,1);//防止a<d的情况出不来数
65         a=c;
66     }
67 }n,m;
68 int main(){
69     n.init(),m.init();
70     n/=m;
71     for(int i=1;i<=n.len;++i)printf("%d",n.s[i]);
72 
73 }

 

posted @ 2019-09-07 09:38  _xuefeng  阅读(20)  评论(0编辑  收藏  举报