Vision_MATH_高精度
来源:互联网 发布:双拼net域名行情 编辑:程序博客网 时间:2024/05/01 04:49
/***name:高精度**function:大数运算**题意:求n的阶乘*/#include<bits/stdc++.h>using namespace std;struct BigInt{ const static int mod = 10000; const static int DLEN = 4; int a[10000],len; BigInt() { memset(a,0,sizeof(a)); len = 1; } BigInt(int v) { memset(a,0,sizeof(a)); len = 0; do { a[len++] = v%mod; v /= mod; } while(v); } BigInt(const char s[]) { memset(a,0,sizeof(a)); int L = strlen(s); len = L/DLEN; if(L%DLEN)len++; int index = 0; for(int i = L-1; i >= 0; i -= DLEN) { int t = 0; int k = i - DLEN + 1; if(k < 0)k = 0; for(int j = k; j <= i; j++) t = t*10 + s[j] - '0'; a[index++] = t; } } BigInt operator +(const BigInt &b)const { BigInt res; res.len = max(len,b.len); for(int i = 0; i <= res.len; i++) res.a[i] = 0; for(int i = 0; i < res.len; i++) { res.a[i] += ((i < len)?a[i]:0)+((i < b.len)?b.a[i]:0); res.a[i+1] += res.a[i]/mod; res.a[i] %= mod; } if(res.a[res.len] > 0)res.len++; return res; } BigInt operator *(const BigInt &b)const { BigInt res; for(int i = 0; i < len; i++) { int up = 0; for(int j = 0; j < b.len; j++) { int temp = a[i]*b.a[j] + res.a[i+j] + up; res.a[i+j] = temp%mod; up = temp/mod; } if(up != 0) res.a[i + b.len] = up; } res.len = len + b.len; while(res.a[res.len - 1] == 0 &&res.len > 1)res.len--; return res; } void output() { printf("%d",a[len-1]); for(int i = len-2; i >=0 ; i--) printf("%04d",a[i]); printf("\n"); }};BigInt a,temp;int main(){ int n; while(cin >> n){ a=BigInt(1); for(int i=2;i<=n;i++){ temp=BigInt(i); a=a*temp; } a.output(); } return 0;}
阅读全文
0 0
- Vision_MATH_高精度
- Vision_MATH_(扩展)欧几里得
- Vision_MATH_中国剩余定理
- Vision_MATH_逆元
- Vision_MATH_三分查找
- Vision_MATH_快速幂||矩阵快速幂
- 高精度
- 高精度
- 高精度
- 高精度。。
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- 高精度
- linux中进程的管理
- CentOS 安装VNCserver 并开启远程桌面
- 【Leetcode-Easy-20】 Valid Parentheses
- 输入一个字符串,统计其出现频率最高的字母以及对应的次数
- 值得收藏的知识
- Vision_MATH_高精度
- 前端之js插件-fullPage
- (转)做个男人,做个成熟的男人,做个有城府的男人
- 对新概念应当”不求甚解“
- Ionic CLI使用教程指南
- Date与String相互转化 (含倒推时间段)
- Swagger UI 中文乱码解决
- dos命令行,常见命令
- 想成为UXD设计师?先学会这4件事