UVa 623 500! (High precision factorial)

Source: Internet
Author: User
Tags message queue printf time limit

623-500!

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=564

In this days you can and more often happen to programs which perform some useful calculations being executed rats Her then trivial screen savers. Some of them check the system message queue and with finding it empty (for examples somebody is editing a file and S Tays idle for some time) to execute its own algorithm.

As an examples we can give programs which calculate primary.

One can also imagine a program which calculates a factorial of given numbers. In this case it's the time complexity of order O(n) which makes troubles, but the memory requirements. Considering the fact that 500! Gives 1135-digit number no standard, neither integer nor floating, and the data type is applicable here.

Your task is to write a programs which calculates a factorial of a given number.

Assumptions: Value of a number ' 'n' which factorial should be calculated of does to exceed 1000 (although 500! is the name of The problem, 500! is a small limit).

Input

Any number of lines, each containing value ' ' N ' for which you should provide value of n!

Output

2 lines for each input case. The should contain value ' n ' followed by character '!'. The second should contain calculated value n!.

Sample Input

Ten
100

Sample Output

10!
3628800
30!
265252859812191058636308480000000
50!
30414093201713378043612608166064768844377641568960512000000000000
100!
9332621544394415268169923885626670049071596826438162146859296389521759999322991560894146397615651828625369792082722375825 1185210916864000000000000000000000000

Complete code:

/*0.172s*/
  
#include <cstdio>
  
int factorial[1001][2800];
  
int main (void)
{
    int i, j, N;
    Factorial[0][0] = 1;
    Factorial[1][0] = 1;
    for (i = 2; i < 1001. i++) for
        (j = 0; J < 2800; J +)
        {
            Factorial[i][j] + = factorial[i-1][j] * I;
  if (Factorial[i][j] > 9)
            {
                factorial[i][j + 1] = factorial[i][j]/ten;
                FACTORIAL[I][J]%=
            }
    while (~SCANF ("%d", &n))
    {
        printf ("%d!\n", n);
        for (i = 2600 i > 0; i--)
            if (Factorial[n][i]) break
                ;
        for (; I >= 0; i--)
            printf ("%d", Factorial[n][i]);
        Putchar (' \ n ');
    }
    return 0;
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

01./*1.238s*/  
02.
03.import java.io.*;
04.import java.util.*;
05.import java.math.*;
06.
07.public class Main {
- static final int maxn = 1001;
A. static Scanner cin = new Scanner (new Bufferedinputstream (system.in));
10.
One. Public static void Main (string[] args) {
biginteger[] f = new BIGINTEGER[MAXN];
f[0] = Biginteger.one;
for (int i = 1; i < MAXN; ++i)
F[i] = f[i-1].multiply (biginteger.valueof (i));
While (Cin.hasnextint ()) {
int n = cin.nextint ();
System.out.println (n + "!");
System.out.println (F[n]);
. }
. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.