这道题一开始就采用将一万个解的表打好的话,虽然时间效率比较高,但是内存占用太大,就MLE

这里写好大数后,每次输入一个n,然后再老老实实一个个求阶层就好

java代码:

 /**
* @(#)Main.java
*
*
* @author
* @version 1.00 2014/12/21
*/
import java.util.*;
import java.math.*; public class Main {
//public static BigInteger a [] = new BigInteger[10001];
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int n;
while(input.hasNext()){
n = input.nextInt();
BigInteger a [] = new BigInteger[2];
a[0] = new BigInteger("1");
for(int i = 1; i<=n ; i++){
String s = Integer.toString(i);
a[i&1] = new BigInteger(s);
// a[i].valueOf(i);
a[i&1] = a[i&1].multiply(a[1-(i&1)]);
} System.out.println(a[n&1]);
}
} }

c++代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ; typedef long long LL ; #define rep( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define For( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define rev( i , a , b ) for ( int i = a ; i >= b ; -- i ) const int M = ; struct BigInt {
int digit[] ;
int length ; BigInt () {
length = ;
memset ( digit , , sizeof digit ) ;
} BigInt ( LL number ) {
length = ;
memset ( digit , , sizeof digit ) ;
while ( number ) {
digit[length ++] = number % M ;
number /= M ;
}
} int operator [] ( const int index ) const {
return digit[index] ;
} int& operator [] ( const int index ) {
return digit[index] ;
} BigInt fix () {
while ( length && digit[length - ] == ) -- length ;
return *this ;
} BigInt operator + ( const BigInt& a ) const {
BigInt c ;
c.length = max ( length , a.length ) + ;
int add = ;
rep ( i , , c.length ) {
add += a[i] + digit[i] ;
c[i] = add % M ;
add /= M ;
}
return c.fix () ;
} BigInt operator - ( const BigInt& a ) const {
BigInt c ;
c.length = max ( a.length , length ) ;
int del = ;
rep ( i , , c.length ) {
del += digit[i] - a[i] ;
c[i] = del ;
del = ;
if ( c[i] < ) {
int tmp = ( c[i] - ) / M + ;
c[i] += tmp * M ;
del -= tmp ;
}
}
return c.fix () ;
} BigInt operator * ( const BigInt& a ) const {
BigInt c ;
c.length = a.length + length ;
rep ( i , , length ) {
int mul = ;
For ( j , , a.length ) {
mul += digit[i] * a[j] + c[i + j] ;
c[i + j] = mul % M ;
mul /= M ;
}
}
return c.fix () ;
} void show () {
printf ( "%d" , digit[length - ] ) ;
rev ( i , length - , ) printf ( "%04d" , digit[i] ) ;
printf ( "\n" ) ;
} } ; int main ()
{
int n;
while (~scanf("%d" , &n)) {
BigInt big[];
big[] = BigInt();
for(int i = ; i<=n ; i++)
big[i&] = big[-(i&)] * BigInt(i);
big[n&].show();
}
return ;
}

HDU 1042 大数计算的更多相关文章

  1. HDU 1042 大数阶乘

    B - 2 Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  2. Java & C++ 大数计算

    Java--大数计算,妈妈再也不用担心我的学习了 . BigInteger 英文API: http://docs.oracle.com/javase/8/docs/api/ 中文API: http:/ ...

  3. HDU 1042 N! 參考代码

    HDU 1042 N! 题意:给定整数N(0 ≤ N ≤ 10000), 求 N! (题目链接) #include <iostream> using namespace std; //每一 ...

  4. js 大数计算

    js 大数计算 原理 JavaScript 安全整数 是 -253-1 ~ 253-1 ,即: -9007199254740991 ~ 9007199254740991; 换句话说,整数超过这个范围就 ...

  5. hdu 1042 N!(大数)

    题意:求n!(0 ≤ N ≤ 10000) 思路:大数,用数组存储 1.首先要考虑数据N!的位数,因为最大是10000!,可以计算一下大概是5+9000*4+900*3+90*2+10*1=38865 ...

  6. HDU 1042 N!(高精度阶乘、大数乘法)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submi ...

  7. HDU 1131 Count the Trees 大数计算

    题目是说给出一个数字,然后以1到这个数为序号当做二叉树的结点,问总共有几种组成二叉树的方式.这个题就是用卡特兰数算出个数,然后因为有编号,不同的编号对应不同的方式,所以结果是卡特兰数乘这个数的阶乘种方 ...

  8. hdu 1042 N!(大数的阶乘)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  9. hdu 1042 N! java大数及判断文件末尾

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submi ...

随机推荐

  1. Geometry Shader 实现 Wireframe 绘制边线的Shader

    最终效果: 参考了一个免费插件 https://assetstore.unity.com/packages/vfx/shaders/directx-11/ucla-wireframe-shader-2 ...

  2. linux centos7安装mysql

    1.下载并安装官方的 yum repository (新建了mysql文件夹) wget -i -c http://dev.mysql.com/get/mysql57-community-releas ...

  3. mycat重启报错Failed to connect to the Wrapper at port解决方法

    报错信息 ERROR | wrapper | 2018/05/11 14:01:55 | Startup failed: Timed out waiting for a signal from the ...

  4. Linux环境下修改MySQL数据库对表名大小写不敏感

    Linux系统中MySQL对数据库名称和表名是大小写敏感的,这就导致了一些麻烦,虽然已经建立了表和数据,但因为大小写导致无法找到表. MySQL数据库对表名大小写不敏感的设置方法如下: 1.查看MyS ...

  5. Modbus消息帧

    两种传输模式中(ASCII和RTU),传输设备以将Modbus消息转为有起点和终点的帧,这就允许接收的设备在消息起始处开始工作,读地址分配信息,判断哪一个设备被选中(广播方式则传给所以设备),判知何时 ...

  6. maven build过程中遇到的问题以及解决方案

    (1)不支持泛型以及@Override 问题来源:使用了低版本的jdk,默认情况下maven使用的是jdk1.5的版本,而泛型和@Override是后期版本才有的,需要更改maven默认的jdk版本. ...

  7. activity生命周期知识点整理

    activity生命周期知识点整理 Activity: 是一个应用组件,用户可与其提供的屏幕进行交互.窗口通常会充满屏幕,但也可以小于屏幕并浮动在其他窗口之上. 一个activity的什么周期: 启动 ...

  8. opencv总结

    2018-02-2623:59:02 唉,这软件我很烦躁,今天又搞了好几遍,出错提示的时候总是出问题! 而且,无论什么错误,都是提示一堆乱码! 定义ROI区域有两种方法,第一种是使用cv:Rect.顾 ...

  9. 安装mask-rcnn问题汇总

    1. I download file from https://github.com/waleedka/coco. Then I placed the file in Mask_RCNN-master ...

  10. HDU_1227_Fast Food_动态规划

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1227 Fast Food Time Limit: 2000/1000 MS (Java/Others)   ...