这道题一开始就采用将一万个解的表打好的话,虽然时间效率比较高,但是内存占用太大,就MLE
这里写好大数后,每次输入一个n,然后再老老实实一个个求阶层就好
java代码:
1 /** 2 * @(#)Main.java 3 * 4 * 5 * @author 6 * @version 1.00 2014/12/21 7 */ 8 import java.util.*; 9 import java.math.*; 10 11 public class Main { 12 //public static BigInteger a [] = new BigInteger[10001]; 13 public static void main(String [] args){ 14 Scanner input = new Scanner(System.in); 15 int n; 16 while(input.hasNext()){ 17 n = input.nextInt(); 18 BigInteger a [] = new BigInteger[2]; 19 a[0] = new BigInteger("1"); 20 for(int i = 1; i<=n ; i++){ 21 String s = Integer.toString(i); 22 a[i&1] = new BigInteger(s); 23 // a[i].valueOf(i); 24 a[i&1] = a[i&1].multiply(a[1-(i&1)]); 25 } 26 27 System.out.println(a[n&1]); 28 } 29 } 30 31 32 }
c++代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std ; 5 6 typedef long long LL ; 7 8 #define rep( i , a , b ) for ( int i = a ; i < b ; ++ i ) 9 #define For( i , a , b ) for ( int i = a ; i <= b ; ++ i ) 10 #define rev( i , a , b ) for ( int i = a ; i >= b ; -- i ) 11 12 const int M = 10000 ; 13 14 struct BigInt { 15 int digit[10000] ; 16 int length ; 17 18 BigInt () { 19 length = 0 ; 20 memset ( digit , 0 , sizeof digit ) ; 21 } 22 23 BigInt ( LL number ) { 24 length = 0 ; 25 memset ( digit , 0 , sizeof digit ) ; 26 while ( number ) { 27 digit[length ++] = number % M ; 28 number /= M ; 29 } 30 } 31 32 int operator [] ( const int index ) const { 33 return digit[index] ; 34 } 35 36 int& operator [] ( const int index ) { 37 return digit[index] ; 38 } 39 40 BigInt fix () { 41 while ( length && digit[length - 1] == 0 ) -- length ; 42 return *this ; 43 } 44 45 BigInt operator + ( const BigInt& a ) const { 46 BigInt c ; 47 c.length = max ( length , a.length ) + 1 ; 48 int add = 0 ; 49 rep ( i , 0 , c.length ) { 50 add += a[i] + digit[i] ; 51 c[i] = add % M ; 52 add /= M ; 53 } 54 return c.fix () ; 55 } 56 57 BigInt operator - ( const BigInt& a ) const { 58 BigInt c ; 59 c.length = max ( a.length , length ) ; 60 int del = 0 ; 61 rep ( i , 0 , c.length ) { 62 del += digit[i] - a[i] ; 63 c[i] = del ; 64 del = 0 ; 65 if ( c[i] < 0 ) { 66 int tmp = ( c[i] - 1 ) / M + 1 ; 67 c[i] += tmp * M ; 68 del -= tmp ; 69 } 70 } 71 return c.fix () ; 72 } 73 74 BigInt operator * ( const BigInt& a ) const { 75 BigInt c ; 76 c.length = a.length + length ; 77 rep ( i , 0 , length ) { 78 int mul = 0 ; 79 For ( j , 0 , a.length ) { 80 mul += digit[i] * a[j] + c[i + j] ; 81 c[i + j] = mul % M ; 82 mul /= M ; 83 } 84 } 85 return c.fix () ; 86 } 87 88 void show () { 89 printf ( "%d" , digit[length - 1] ) ; 90 rev ( i , length - 2 , 0 ) printf ( "%04d" , digit[i] ) ; 91 printf ( "\n" ) ; 92 } 93 94 } ; 95 96 97 int main () 98 { 99 int n; 100 while (~scanf("%d" , &n)) { 101 BigInt big[2]; 102 big[0] = BigInt(1); 103 for(int i = 1 ; i<=n ; i++) 104 big[i&1] = big[1-(i&1)] * BigInt(i); 105 big[n&1].show(); 106 } 107 return 0 ; 108 }
时间: 2024-09-29 19:31:55