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;
}

  

UVA OJ 623 500!的更多相关文章

  1. uva oj 567 - Risk(Floyd算法)

    /* 一张有20个顶点的图上. 依次输入每个点与哪些点直接相连. 并且多次询问两点间,最短需要经过几条路才能从一点到达另一点. bfs 水过 */ #include<iostream> # ...

  2. UVa OJ 194 - Triangle (三角形)

    Time limit: 30.000 seconds限时30.000秒 Problem问题 A triangle is a basic shape of planar geometry. It con ...

  3. UVa OJ 175 - Keywords (关键字)

    Time limit: 3.000 seconds限时3.000秒 Problem问题 Many researchers are faced with an ever increasing numbe ...

  4. UVa OJ 197 - Cube (立方体)

    Time limit: 30.000 seconds限时30.000秒 Problem问题 There was once a 3 by 3 by 3 cube built of 27 smaller ...

  5. UVa OJ 180 - Eeny Meeny

    Time limit: 3.000 seconds限时3.000秒 Problem问题 In darkest <name of continent/island deleted to preve ...

  6. UVa OJ 140 - Bandwidth (带宽)

    Time limit: 3.000 seconds限时3.000秒 Problem问题 Given a graph (V,E) where V is a set of nodes and E is a ...

  7. 548 - Tree (UVa OJ)

    Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...

  8. UVa OJ 10300

    Problem A Ecological Premium Input: standard input Output: standard output Time Limit: 1 second Memo ...

  9. UVa OJ 10071

    Problem B Back to High School Physics Input: standard input Output: standard output A particle has i ...

随机推荐

  1. MQ 架构与细节

    MQ 架构与细节 MQ 是什么? MQ:MessageQueue,消息队列的简称,用于进程间通信或同一进程的不同线程间的通信方式. 什么时候该使用MQ? 数据驱动的任务依赖 上游不关心执行结果 上游关 ...

  2. 攻防世界 reverse elrond32

    tinyctf-2014 elrond32 1 int __cdecl main(int a1, char **arg_input) 2 { 3 if ( a1 > 1 && c ...

  3. sqli-labs系列——第六关

    less6 这个本质上跟第五关相同都是使用报错注入,这一关使用的是双引号闭合 还是使用updatexml()这个函数 ?id=1" union select updatexml(1,conc ...

  4. springboot的拦截器报错plicationFilterChain.java:193) ~[tomcat-embed-core-9.0.36.jar:9.0.36]

    解决方案: spingboot的拦截器"index.html"少"/",太粗心了

  5. 体验用yarp当网关

    Yarp是微软开源的一个用.net实现的反向代理工具包,github库就叫reverse-proxy(反向代理)(吐槽一下微软起名字233333) nuget包preview9之前都叫Microsof ...

  6. E. 【例题5】平铺方案

    E . [ 例 题 5 ] 平 铺 方 案 E. [例题5]平铺方案 E.[例题5]平铺方案 解析 由于最近赶进度,解析写的就很简略 通过推算得出递推式 a [ i ] = a [ i − 1 ] + ...

  7. [Fundamental of Power Electronics]-PART I-1.引言-1.2 1.3 电力电子技术的几个应用、本书内容

    1.2 电力电子技术的几个应用 高效开关变换器面临的功率范围从 (1)小于1瓦(电池供电的便携式设备内的DC-DC转换器)到(2)计算机及办公设备中的几十,几百,数千瓦到(3)变速电机驱动器中上千瓦及 ...

  8. Windows上Docker Toolbox修改镜像源

    https://blog.csdn.net/weixin_36242811/article/details/90515835

  9. 并发编程(ReentrantLock&&同步模式之顺序控制)

    4.13 ReentrantLock 相对于 synchronized 它具备如下特点 可中断 可以设置超时时间 可以设置为公平锁 支持多个条件变量,即对与不满足条件的线程可以放到不同的集合中等待 与 ...

  10. JavaScript课程——Day01

    1.网页由三部分组成: 1.1.HTML:超文本标记语言,专门编写网页内容的语言.(结构) 1.2.CSS:层叠样式表.(样式) 1.3.javaScript:网页交互的解释性脚本语言,专门编写客户端 ...