高精度-HDU-1042-N!
来源:互联网 发布:php页面显示空白 编辑:程序博客网 时间:2024/04/30 07:54
N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 68606 Accepted Submission(s): 19629
Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
Input
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Input
1
2
3
Sample Output
1
2
6
题意:还需要解释么。。。1W以内的阶乘运算。
题解:一开始想着是不是打个表,后面MLE了几次,哭死,最后也懒得本地打表,索性把打表撤了一次一次算,耗时有点长,不过还是A了。
#include <iostream>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <algorithm>#include <math.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 argc, const char * argv[]){ a=BigInt(1); 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
- 高精度-HDU-1042-N!
- hdu 1042 N! 高精度运算
- Hdu 1042 N! (高精度数)
- HDU 1042 N!(高精度)
- hdu 1042 N! 高精度乘法
- HDU 1042 N! (模拟 高精度)
- hdu - 1042 - N!(高精度计算)
- hdu 1042 N!(高精度乘法 + 缩进)
- HDU 1042 N!(高精度计算阶乘)
- 【HDU 1042】N! —— 高精度整数
- HDU 1042 N! 阶乘 高精度(java)
- HDU 1042 N!(高精度计算阶乘)
- HDU 1042 大数据、高精度,求n的阶乘
- hdoj 1042 N!(高精度)
- HDU-1042 高精度乘法
- HDU-1042 高精度
- 高精度N!
- N!高精度
- JQuery中的事件委托机制:delegate和undelegate
- 分布式Java应用之网络通信(1)
- MySQL-5.6.26免安装版配置方法
- 腾讯美女设计师倾囊相授:抄现实(第一集)
- HIVE-DML
- 高精度-HDU-1042-N!
- Java基础笔记
- Github gui使用
- 手动异机还原数据库
- gpio配置mac地址
- Hibernate 总结2:配置方法
- LINUX驱动编程
- android中的http通信---(3)通过get、post方式传数据给服务器
- python绘图工具reportlab介绍