这道题一开始就采用将一万个解的表打好的话,虽然时间效率比较高,但是内存占用太大,就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 1231: [Usaco2008 Nov]mixup2 混乱的奶牛【状压dp】

    设f[i][j]为奶牛选取状态为i,最后一头选的为j,转移直接f[k][(1<<(k-1)|i]+=f[j][i] #include<iostream> #include< ...

  2. Nginx(二) 反向代理&负载均衡

    1.反向代理 当我们请求一个网站时,nginx会决定由哪台服务器提供服务,就是反向代理. nginx只做请求的转发,后台有多个tomcat服务器提供服务,nginx的功能就是把请求转发给后面的服务器, ...

  3. datatable-bootstrap 基本配置

    function doSearch() { if(dtable!=null){ dtable.fnClearTable(0); dtable.fnDraw(); // 重新加载数据 }else{ dt ...

  4. PWBI--Excel 数据源

    博客园地址: http://blog.sina.com.cn/s/blog_68c4467d0102w5cc.html http://www.cnblogs.com/asxinyu/p/Power_B ...

  5. 使用JS分页 <span> beta 2.0 未封装的分页

    <html> <head> <title>分页</title> <style> #titleDiv{ width:500px; backgr ...

  6. 300 Longest Increasing Subsequence 最长上升子序列

    给出一个无序的整形数组,找到最长上升子序列的长度.例如,给出 [10, 9, 2, 5, 3, 7, 101, 18],最长的上升子序列是 [2, 3, 7, 101],因此它的长度是4.因为可能会有 ...

  7. 258 Add Digits 各位相加

    给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字.例如:设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返回它.进阶:你可 ...

  8. python自动化--mock、webservice及webdriver模拟手机浏览器

    一.mock实现 自定义一个类,用来模拟未完成部分的开发代码 class Say(): def say_hello(self): pass 自定义返回值 import unittest from un ...

  9. Android基础TOP5_4:点击按钮更换样式,设置透明度

    在res/drawable创建两个样式 点击前/点击后 round: <?xml version="1.0" encoding="utf-8"?> ...

  10. Mac上简单常用Terminal命令

    方案1 SSH是一个非常伟大的工具,如果你要在互联网上远程连接到服务器,那么SSH无疑是最佳的候选.SSH是加密的,OpenSSH加密所有通信(包括密码),有效消除了窃听,连接劫持和其它攻击.本文将为 ...