高精度
来源:互联网 发布:mac安装win10单系统 编辑:程序博客网 时间:2024/04/29 16:33
const int maxLen = 1005;struct BigInt{//not support negative long long num[maxLen]; int digit = 0; BigInt() = default; BigInt(char a[]){ int la = strlen(a); int i = la-8, j = 0; for(; i >= 0; i -= 8) sscanf(a+i,"%8d",&num[j++]); int t; num[j] = 0; if(i+8 > 0){ for(int k = 0; i+8 > 0; ++k){ sscanf(a+k,"%1d",&t); num[j] = num[j]*10 + t; --i; } ++j; } num[j] = -1; digit = j; } BigInt(int a){ int j = 0; num[j++] = a%100000000; if(a >= 100000000) num[j++] = a/100000000; num[j] = -1; digit = j; } BigInt(long long a){ int j = 0; num[j++] = a%100000000; a /= 100000000; if(a > 0){ num[j++] = a%100000000; a /= 100000000; if(a > 0){ num[j++] = a%100000000; } } num[j] = -1; digit = j; } int bigCmp(const BigInt rhs){ if(digit > rhs.digit) return 1; else if(digit < rhs.digit) return -1; else{ for(int i = digit-1; i >= 0; --i){ if(num[i] > rhs.num[i]) return 1; else if(num [i] < rhs.num[i]) return -1; } return 0; } } BigInt operator + (const BigInt rhs){ BigInt ans; int idx = 0, carry = 0; while(num[idx]!=-1 && rhs.num[idx]!=-1){ ans.num[idx] = num[idx]+rhs.num[idx]+carry; carry = ans.num[idx] / 100000000; ans.num[idx++] %= 100000000; } if(num[idx] != -1){ while(num[idx] != -1){ ans.num[idx] = num[idx] + carry; carry = ans.num[idx] / 100000000; ans.num[idx++] %= 100000000; } } else{ while(rhs.num[idx] != -1){ ans.num[idx] = rhs.num[idx] + carry; carry = ans.num[idx] / 100000000; ans.num[idx++] %= 100000000; } } if(carry > 0) ans.num[idx++] = carry; ans.num[idx] = -1; ans.digit = idx; return ans; } BigInt operator * (const int rhs){ BigInt ans; int carry = 0,idx = 0; if(rhs != 0){ for(idx = 0; num[idx] != -1; ++idx){ ans.num[idx] = num[idx]*rhs + carry; carry = ans.num[idx] / 100000000; ans.num[idx] %= 100000000; } if(carry > 0) ans.num[idx++] = carry; } else{ ans.num[idx++] = 0; } ans.num[idx] = -1; ans.digit = idx; return ans; } BigInt operator - (const BigInt rhs){//l >= r BigInt ans; int idx = 0, carry = 0; while(rhs.num[idx] != -1){ ans.num[idx] = num[idx] - rhs.num[idx] - carry; carry = 0; if(ans.num[idx] < 0){ ans.num[idx] += 100000000; carry = 1; } ++idx; } while(num[idx] != -1){ ans.num[idx] = num[idx] - carry; carry = 0; if(ans.num[idx] < 0){ ans.num[idx] += 100000000; carry = 1; } ++idx; } ans.num[idx] = -1; ans.digit = idx; return ans; } void frac(int n){ num[0] = 1; num[1] = -1; digit = 1; int carry, idx; for(int i = 2; i <= n; ++i){ carry = 0; for(idx = 0; num[idx] != -1; ++idx){ num[idx] = num[idx]*i + carry; carry = num[idx] / 100000000; num[idx] %= 100000000; } if(carry > 0) num[idx++] = carry; num[idx] = -1; digit = idx; } } long long operator % (const int rhs){ int idx = digit-1; long long r = num[idx--]%rhs; while(idx >= 0){ r = (r*100000000LL+num[idx])%rhs; --idx; } return r; } BigInt operator / (const int rhs){ BigInt ans; int i = digit-1, r = 0; while(i >= 0){ ans.num[i] = (num[i]+r*100000000LL)/(long long)rhs; r = (num[i]+r*100000000LL)%(long long)rhs; --i; } ans.digit = digit; while(ans.digit > 1 && ans.num[ans.digit-1] == 0) ans.digit--; ans.num[ans.digit] = -1; return ans; } void Print(){ if(digit > 0){ int i = digit-1; printf("%lld",num[i]); while(i--) printf("%08lld",num[i]); printf("\n"); } }};
0 0
- 高精度
- 高精度
- 高精度
- 高精度。。
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- Android中的消息机制
- 【VMware】[vm tools]安装VMtools时出现的错误,无法与主机共享文件
- php openssl加密解密方法
- NOIP 提高组【JZOJ 4783】Osu
- VS2015使用小技巧 将选中的代码全都注释掉的快捷键
- 高精度
- Codeforcces 617C Watering Flowers【贪心+暴力枚举】
- JAVA进阶2.2——用try...catch处理异常
- 进程状态
- Linux kali真正简单快捷方便安装VMware tools的方法 附上kali2.0iso 文件下载地址
- 做题の经验与错误
- Spring Boot 部署与服务配置
- office2010全家桶破解版
- mvc 中BeginForm常用重载版本