模板 高精度运算
Posted guapisolo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模板 高精度运算相关的知识,希望对你有一定的参考价值。
联赛前终于把高精度的坑填上了
读入写错了导致第一交爆5了..
贴上丑陋的压4位代码
1 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define N 26000 6 #define maxn 10000 7 #define inf 66666666 8 #define idx(x) (x-‘0‘) 9 using namespace std; 10 11 int n,cte,num; 12 struct bignum{ 13 int a[N],neg,len,res; 14 friend bignum operator - (bignum &a,bignum &b); 15 friend bignum operator + (bignum &a,bignum &b); 16 friend int operator != (const bignum &a,const bignum &b) 17 { 18 if(a.len!=b.len) return 1; 19 for(int i=1;i<=min(a.len,b.len);i++) 20 if(a.a[i]!=b.a[i]) return 1; 21 return 0; 22 } 23 friend int operator <= (const bignum &a,const bignum &b) 24 { 25 if(a.len>b.len) return 0; 26 if(b.len>a.len) return 1; 27 for(int i=a.len;i>=1;i--) 28 if(a.a[i]>b.a[i]) return 0; 29 else if(a.a[i]<b.a[i]) return 1; 30 return 1; 31 } 32 friend bignum operator + (bignum &a,bignum &b) 33 { 34 if(a.neg&&!b.neg){ 35 a.neg^=1; 36 return b-a; 37 }else if(!a.neg&&b.neg){ 38 b.neg^=1; 39 return a-b; 40 } 41 bignum ret; 42 memset(&ret,0,sizeof(ret)); 43 if(a.neg&&b.neg) ret.neg^=1; 44 ret.len=max(a.len,b.len); 45 for(int i=1;i<=ret.len;i++) 46 { 47 ret.a[i]+=a.a[i]+b.a[i]; 48 if(ret.a[i]>=maxn) 49 ret.a[i]-=maxn,ret.a[i+1]++; 50 } 51 if(ret.a[ret.len+1]!=0) ret.len++; 52 while(!ret.a[ret.len]) 53 ret.len--; 54 return ret; 55 } 56 friend bignum operator - (bignum &a,bignum &b) 57 { 58 if(a.neg&&!b.neg){ 59 b.neg^=1; 60 return a+b; 61 }else if(!a.neg&&b.neg){ 62 b.neg^=1; 63 return a+b; 64 } 65 bignum ret; 66 memset(&ret,0,sizeof(ret)); 67 if(b<=a) ret.neg=0; 68 else swap(a,b),ret.neg^=1; 69 if(a.neg&&b.neg) 70 swap(a,b),ret.neg^=1; 71 ret.len=a.len; 72 for(int i=1;i<=a.len;i++) 73 { 74 ret.a[i]+=a.a[i]-b.a[i]; 75 if(ret.a[i]<0) 76 ret.a[i+1]--,ret.a[i]+=maxn; 77 } 78 while(!ret.a[ret.len]) 79 ret.len--; 80 return ret; 81 } 82 friend bignum operator * (bignum &a,bignum &b) 83 { 84 bignum ret; 85 memset(&ret,0,sizeof(ret)); 86 if(a.neg) ret.neg^=1; 87 if(b.neg) ret.neg^=1; 88 for(int i=1;i<=a.len;i++) 89 for(int j=1;j<=b.len;j++){ 90 ret.a[i+j-1]+=a.a[i]*b.a[j]; 91 if(ret.a[i+j-1]>=maxn){ 92 ret.a[i+j]+=ret.a[i+j-1]/maxn; 93 ret.a[i+j-1]%=maxn;} 94 } 95 ret.len=a.len+b.len; 96 while(!ret.a[ret.len]) 97 ret.len--; 98 return ret; 99 } 100 friend bignum operator / (bignum &a,int b) 101 { 102 bignum ret; 103 memset(&ret,0,sizeof(ret)); 104 //memset(&ret,&x,sizeof(x)); 105 int sum=0; 106 for(int i=a.len;i>=1;i--){ 107 sum=sum*maxn+a.a[i]; 108 ret.a[i]=sum/b; 109 sum%=b; 110 }ret.len=a.len,ret.res=sum; 111 while(!ret.a[ret.len]&&ret.len>1) 112 ret.len--; 113 return ret; 114 } 115 }; 116 char str[N*4]; 117 bignum read() 118 { 119 scanf("%s",str); 120 bignum ret; 121 int mi=0; 122 memset(&ret,0,sizeof(ret)); 123 if(str[0]==‘-‘) ret.neg=1,mi++; 124 int len=strlen(str); 125 ret.len=1; 126 int cnt=1,pw=1; 127 for(int i=len-1;i>=mi;i--) 128 { 129 if(cnt==5) 130 cnt=1,pw=1,ret.len++; 131 ret.a[ret.len]+=idx(str[i])*pw; 132 cnt++,pw*=10; 133 } 134 return ret; 135 } 136 137 int main() 138 { 139 scanf("%d",&n); 140 bignum ret,a,b; 141 int res,val; 142 if(n==1){ 143 a=read(),b=read(); 144 ret=a+b; 145 }else if(n==2){ 146 a=read(),b=read(); 147 ret=a-b; 148 }else if(n==3){ 149 a=read(),b=read(); 150 ret=a*b; 151 }else{ 152 a=read(); 153 scanf("%d",&val); 154 ret=a/val; 155 } 156 if(ret.neg) 157 printf("-"); 158 printf("%d",ret.a[ret.len]); 159 for(int i=ret.len-1;i>=1;i--) 160 printf("%04d",ret.a[i]); 161 puts(""); 162 if(n==4) printf("%d ",ret.res); 163 return 0; 164 }
以上是关于模板 高精度运算的主要内容,如果未能解决你的问题,请参考以下文章