HDU 1042 大数计算

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

HDU 1042 大数计算的相关文章

HDU 1042 大数阶乘

B - 2 Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1042 Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, process to the end of file. Outpu

hdu 1042 N!(大数阶乘,转化为100000这样的比较大的进制)

N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 54172    Accepted Submission(s): 15365 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one

HDU 1042 N! (大数阶乘,还是Java大法好,C也不能错过!!!)

N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 73270    Accepted Submission(s): 21210 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in o

HDU - 1042 - N! - JAVA

http://acm.hdu.edu.cn/showproblem.php?pid=1042 大数嘛,直接用JAVA. 为什么要开64路?好像我觉得会快一点--其实并没有快-- import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while

HDU 4927 大数运算

模板很重要 #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define MAXN 9999 #define MAXSIZE 10 #define DLEN 4 class BigInt { private: int a[500]; //可以控制大数的位数 i

一步一步写算法(之大数计算)

原文:一步一步写算法(之大数计算) [ 声明:版权所有,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 我们知道在x86的32位cpu上面,int表示32位,如果核算成整数的话,大约是40多亿.同样,如果在64位cpu上面,能表示的最大整数就是64位二进制,表示的数值要大得多.那么在32位如果想表示大整数怎么办呢?那只能靠我们自己想办法了. 首先我们回顾一下我们手算整数的加减.乘除法是怎么做到的: (1)记住9*9之间的乘法口诀 (2)记住个位与个位之间的加

HDU 1250 大数加法

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6948    Accepted Submission(s): 2285 Problem Description A Fibonacci sequence is calculated by adding the previous two members the

HDU 1042 N! 参考代码

请问DIY题目做不来的队员:听进去了吗?去消化吸收了吗?能百度一下吗? 请问集训队员:有兴趣吗?有团队合作精神吗?有责任感吗?能坚持吗?能自主学习吗?能承受挫败吗? HDU 1042 N! 题意:Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! (题目链接) #include <iostream> using namespace std; //每个数组元素存放5位数 const int MAX=1000000; //

重温当年入门战之大数计算

大数计算: 由于编程语言提供的基本数值数据类型表示的数值范围有限,不能满足较大规模的高精度数值计算,因此需要利用其他方法实现高精度数值的计算,于是产生了大数运算. 大数计算简析:        大数计算实现的理论是,首先提取输入值赋予指定String字符串. 通过String.charAt(index)提取每一位的值,赋予int数组. 然后求相乘每一位的值和进位. 直到最后每一位都求出来. 代码实现:         1 import java.awt.*; 2 import java.awt.