UVa 623 500! (高精度阶乘)

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 these days you can more and more often happen to see programs which perform some useful calculations being executed rather then trivial screen savers. Some of them check the system message queue and in case of finding it empty (for examples somebody is editing a file and stays idle for some time) execute its own algorithm.

As an examples we can give programs which calculate primary numbers.

One can also imagine a program which calculates a factorial of given numbers. In this case it is 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, 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 not 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. First should contain value ``n" followed by character `!'. The second should contain calculated value n!.

Sample Input

10
30
50
100

Sample Output

10!
3628800
30!
265252859812191058636308480000000
50!
30414093201713378043612608166064768844377641568960512000000000000
100!
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

完整代码:

/*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] / 10;
                factorial[i][j] %= 10;
            }
        }
    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;
}

查看本栏目更多精彩内容: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 {  08.    static final int maxn = 1001;  09.    static Scanner cin = new Scanner(new BufferedInputStream(System.in));  10.  11.    public static void main(String[] args) {  12.        BigInteger[] f = new BigInteger[maxn];  13.        f[0] = BigInteger.ONE;  14.        for (int i = 1; i < maxn; ++i)  15.            f[i] = f[i - 1].multiply(BigInteger.valueOf(i));  16.        while (cin.hasNextInt()) {  17.            int n = cin.nextInt();  18.            System.out.println(n + "!");  19.            System.out.println(f[n]);  20.        }  21.    }  22.} 

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索close() scanner java
, for
, number
, biginteger
, of
, should
, 100的阶乘
, 1135
which
高精度阶乘、高精度阶乘算法、高精度阶乘和、高精度算法 求n的阶乘、高精度求阶乘,以便于您获取更多的相关知识。

时间: 2024-11-18 17:55:50

UVa 623 500! (高精度阶乘)的相关文章

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 11375 Matches:DP&amp;amp;高精度

11375 - Matches Time limit: 2.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2370 We can make digits with matches as shown below: Given N matches, find the number of different numbers representable us

UVa 10247 Complete Tree Labeling:组合数学&amp;amp;高精度

10247 - Complete Tree Labeling Time limit: 15.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1188 A complete k-ary tree is a k-ary tree in which all leaves have same d

UVa 748/POJ 1001 Exponentiation:浮点高精度求幂&amp;amp;正则表达式的应用

748 - Exponentiation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=97&page=show_problem&problem=689 http://poj.org/problem?id=1001 Problems involving the computation of exact values

UVa 10916 Factstone Benchmark:数学及阶乘的处理技巧

10916 - Factstone Benchmark Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=1857 Amtel has announced that it will release a 128-bit computer chip by 2010,

浪潮完成500万平方公里1Km分辨率高精度数值天气预报

近日,浪潮与某气象客户紧密合作,完成了区域覆盖面积超500万平方公里.水平分辨率达1Km*1Km的高精度数值天气预报,为气象业务提供了更加精准的天气预报产品,达到了国内领先国际先进的水平.这项工作是在浪潮为该客户建设的天梭超级计算机上完成的,该超算拥有8640个处理器核心,是目前国内数值天气预报业务领域部署的基于x86技术的性能最高的超级计算机,将更好地服务于经济生产.人民生活和防灾减灾等. 浪潮完成500万平方公里1Km分辨率高精度数值天气预报 天气预报:更高精度.更大规模 该客户之前采用的气

UVa 324 Factorial Frequencies:高精度

324 - Factorial Frequencies Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=260 In an attempt to bolster her sagging palm-reading business, Madam Phoenix

UVa 10198 Counting:组合数学&amp;amp;高精度

10198 - Counting Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1139 The Problem Gustavo knows how to count, but he is now learning how write numbers. As

UVa 10157 Expressions:组合数学&amp;amp;高精度

10157 - Expressions Time limit: 10.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem&problem=1098 Let X be the set of correctly built parenthesis expressions. The elements of X a