这道题一开始就采用将一万个解的表打好的话,虽然时间效率比较高,但是内存占用太大,就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 ;
}
05-22 01:04