这道题一开始就采用将一万个解的表打好的话,虽然时间效率比较高,但是内存占用太大,就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. bzoj 1618: [Usaco2008 Nov]Buying Hay 购买干草【背包】

    好像是完全背包吧分不清了-- 好像是把数组二维压一维的时候,01背包倒序,完全背包正序 ```cpp include include using namespace std; const int N= ...

  2. bzoj 1593: [Usaco2008 Feb]Hotel 旅馆【线段树】

    参考:https://blog.csdn.net/u010336344/article/details/53034372 神一样的线段树 线段树上维护:ll从左开始最长空段:rr从右开始最长空段:le ...

  3. [SDOI2011]消防(单调队列,树的直径,双指针)

     消防 2011年  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 大师 Master   题目描述 Description 某个国家有n个城市,这n个城市中任意两个都连通且有 ...

  4. CSS3常用知识点

    CSS3常用知识点 1 css3选择器 1.1 属性选择器 /* E[attr~=val] 表示的一个单独的属性值 这个属性值是以空格分隔的*/ .attr2 a[class~="kawa& ...

  5. Ubuntu 16.04 安装OpenSSH7.4

      前几天突然收到接到网安总队下发通知说我们在aws里面的服务器存在重大漏洞及安全隐患.必须在规定时间内修改.我们收到邮件打开Excel发现这些问题 是由于OpenSSH版本太低导致的.于是便安排紧急 ...

  6. JS 封装插件

    媒体播放器插件: mediaelement-and-player.js 轮播图插件: swiper.min.js

  7. 模拟 HDOJ 4552 Running Rabbits

    题目传送门 /* 模拟:看懂题意,主要是碰壁后的转向,笔误2次 */ #include <cstdio> #include <algorithm> #include <c ...

  8. lua调用java java调用lua[转载]

    转载:http://dualface.github.io/blog/2013/01/01/call-java-from-lua/LuaJavaBridge - Lua 与 Java 互操作的简单解决方 ...

  9. C# 写的正整数四则运算计算器

    实际上没能做出来负数.括号.小数的功能,才写这么个标题 大神直接略过,也欢迎指指点点-.- 输入一个四则运算表达式,计算出结果,想想不难,实现起来也不是很容易的. 流程:1.for循环输入的四则运算字 ...

  10. IIS网站部署步骤以及常见异常解决方案

    一. 简述 如果VS调试代码每次都使用浏览器打开,修改脚本和样式等还可以刷新页面显示最新修改,但是修改后台代码的话就需要停止调试再重新使用浏览器打开才能显示后台的最新修改,就比较麻烦.这里推荐附加到I ...