【模板】高精度。。。。。
真是越学越回去了,颓颓颓~
看到了zky大佬的高精模板 简直tql
兴冲冲打抄了一发
如下:
1 struct bignum{ 2 int num[100]; 3 bignum(){num[0]=1;} 4 void set(long long x){ 5 memset(num,0,sizeof(num)); 6 while (x) num[++num[0]] =x%10,x/=10; 7 if(!num[0]) num[0]=1; 8 } 9 void show(){ 10 for(int i=num[0];i>=1;--i) 11 putchar(num[i]+'0'); 12 } 13 bignum operator + (bignum b) const { 14 bignum c; 15 c.set(0); 16 c.num[0]=max(num[0]+b.num[0])+1; 17 for(int i=1;i<=num[i];i++){ 18 c.num[i]+=num[i]+b.num[i]; 19 c.num[i+1]+=c.num[i]/10; 20 c.num[i]%=10; 21 } 22 while (c.num[0]>1 && !c.num[c.num[0]]) 23 c.num[0]--; 24 return c; 25 } 26 bignum operator * (bignum b) const { 27 bignum c; 28 c.set(0); 29 c.num[0]=num[0]+b.num[0]+1; 30 for(int i=1;i<=num[i];i++) 31 for(int j=1;j<=b.num[0];j++){ 32 c.num[i-j+1]+=num[i]*b.num[j]; 33 c.num[i-j]+=c.num[i-j+1]/10; 34 c.num[i-j+1]%=10; 35 } 36 while (c.num[0]>1 && !c.num[c.num[0]]) 37 c.num[0]--; 38 return c; 39 } 40 bool operator < (bignum b) const { 41 if(num[0]<b.num[0]) return true; 42 if(num[0]>b.num[0]) return false; 43 for(int i=num[0];i>=1;i--){ 44 if(num[i]<b.num[i]) return true; 45 if(num[i]>b.num[i]) return false; 46 } 47 return false; 48 } 49 };
附% and -
1 struct bignum{ 2 int len, s[MAXN]; 3 bignum() { 4 len = 0; 5 memset(s, 0, sizeof(s)); 6 } 7 }; 8 char str[10000 + 5]; 9 bignum operator - (bignum a, const bignum& b){ 10 for(int i = a.len; i >= 1; i--){ 11 a.s[i] -= b.s[i]; 12 if(a.s[i] < 0) { 13 a.s[i+1]--, a.s[i] += base; 14 } 15 } 16 while(!a.s[a.len] && a.len > 0) { 17 a.len--; 18 } 19 return a; 20 } 21 bool judge(bignum a, bignum b) { 22 if(a.len != b.len) return a.len > b.len; 23 for(int i = a.len; i >= 1; i--){ 24 if(a.s[i] != b.s[i]) return a.s[i] > b.s[i]; 25 } 26 return true; 27 } 28 bignum operator % (bignum a, bignum b){ 29 while(judge(a, b)) { 30 a = a - b; 31 } 32 return a; 33 }
“胡须如草芥疯长,悲欢不过梦一场”