UVA OJ 623 500!

500! 

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

算法竞赛入门经典上的解法如下:会超时。

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 3000;
int a[maxn];
int main()
{
	int n;
	int i, j;
	int c, s;
	while (scanf("%d", &n) == 1)
	{

		memset(a,0,sizeof(a));
		a[0] = 1;
		for (i = 2; i <= n; i++)
		{
			c = 0;
			for (j = 0; j < maxn; j++)
			{
				s = a[j] * i + c;
				a[j] = s % 10;
				c = s/10;
			}
		}
		for (i = maxn; i >= 0;i--)
		if (a[i])
			break;
		printf("%d!\n",n);
		for (j = i; j >= 0; j--)
			printf("%d",a[j]);
		printf("\n");
	}

	return 0;
}

  修改后如下:

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 500;
int a[maxn];
int main()
{
	int n;
	int i, j;
	int len;
	int c, s;
	while (scanf("%d", &n) == 1)
	{

		memset(a, 0, sizeof(a));
		a[0] = 1;
		len = 1;
		for (i = 2; i <= n; i++)
		{
			c = 0;
			for (j = 0; j < len; j++)
			{
				s = a[j] * i + c;
				a[j] = s % 1000000;
				c = s / 1000000;
			}
			if (c)
				a[len++] = c;
		}
		printf("%d!\n", n);
		printf("%d",a[len-1]);
		for (j = len-2; j >= 0; j--)
			printf("%06d", a[j]);
		printf("\n");
	}

	return 0;
}

  

时间: 2024-10-20 12:32:29

UVA OJ 623 500!的相关文章

UVa OJ 127 - &quot;Accordian&quot; Patience (“手风琴”纸牌)

UVa OJ 127 - "Accordian" Patience ("手风琴"纸牌) Time limit: 3.000 seconds 限时:3.000秒 Problem 问题 You are to simulate the playing of games of "Accordian" patience, the rules for which are as follows: 模拟玩一个"手风琴"纸牌游戏,规则如下: D

uva oj 567 - Risk(Floyd算法)

1 /* 2 一张有20个顶点的图上. 3 依次输入每个点与哪些点直接相连. 4 并且多次询问两点间,最短需要经过几条路才能从一点到达另一点. 5 6 bfs 水过 7 */ 8 #include<iostream> 9 #include<cstring> 10 #include<vector> 11 #include<cstdio> 12 #include<queue> 13 using namespace std; 14 15 struct

UVA OJ Bicoloring 10004

/*这题后面那个visit的判断有点浪费时间.没有优化好..后来看了飞神的解题报告,在DFS算法中进行优化 for(i=0; i<n; i++)     {         if(!color[i]&&map[x][i])         {             color[i]=-color[x];             if(dfs(i,n))                 return 1;             else                 return

UVA OJ 10035 - Primary Arithmetic

Primary Arithmetic Children are taught to add multi-digit numbers from right-to-left one digit at a time. Many find the "carry" operation - in which a 1 is carried from one digit position to be added to the next - to be a significant challenge.

UVa OJ 455 Periodic Strings

 Periodic Strings  A character string is said to have period k if it can be formed by concatenating one or more repetitions of another string of length k. For example, the string "abcabcabcabc" has period 3, since it is formed by 4 repetitions o

uva oj 12171(hdu 2771)[离散化]sculptrue

以前对离散化的理解不够,所以把端点和区间区分来考虑但是做完这题以后有了新的认识: 先来看一个问题:给你以下的网格,你需要多少空间去存储红点区间的信息呢? 只需要图上所示的1,2,3,4个点就足够表示红点所在区间了,为什么不是一个区间的第一个红点和最后一个红点呢?(如果这样记录的话则必须加一区间点,记录区间内部信息,因为端点可能是两个区间的交集而区间内可能只被操作了一次)这样做的好处是空白区域的长度也能轻易计算出来. 为了计算总区间两端空白的长度,增加A和B点. 原数据找离散后的值直接二分,没必要

(DS 《算法入门经典》)UVA 11991 Easy Problem from Rujia Liu?(求第k个v出现的索引)

题目大意: 求第k个v出现的索引 解题思路: 如果能构造出一个数据结构,使得data[v][k]就是第k个v出现的索引值即可求解.data[v]表示数v出现的索引数组, data[v][k]表示第k个v出现的索引. Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 200

[UVA] 11991 - Easy Problem from Rujia Liu? [STL应用]

11991 - Easy Problem from Rujia Liu? Time limit: 1.000 seconds Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 2006, Beijing 2007 and Wuhan 2009, or UVa OJ con

uva Easy Problem from Rujia Liu?

Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, regional contests like Xi'an 2006, Beijing 2007 and Wuhan 2009, or UVa OJ contests like Rujia Liu's Presents 1 and 2), he occasionally sets