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. Java中HashMap的源码分析

    先来回顾一下Map类中常用实现类的区别: HashMap:底层实现是哈希表+链表,在JDK8中,当链表长度大于8时转换为红黑树,线程不安全,效率高,允许key或value为null HashTable ...

  2. 【vue开发】 计算属性传参

    <template> <div> {{test('zhende', 'np')}} </div> </template> <script> ...

  3. 半监督学习方法(Semi-supervised Learning)的分类

    根据模型的训练策略划分: 直推式学习(Transductive Semi-supervised Learning) 无标记数据就是最终要用来测试的数据,学习的目的就是在这些数据上取得最佳泛化能力. 归 ...

  4. Python3实现短信轰炸机

    短信轰炸机的基本原理:利用某些限制不严格的网站短信注册接口,用Python模拟请求,传入被炸人手机号码,实现轰炸 实现方式:利用requests模块.time模块.完成请求模拟 模块安装: 在终端窗口 ...

  5. python自动统计zabbix系统监控覆盖率

    脚本主要功能: 1)通过zabbix api接口采集所有监控主机ip地址: 2)通过cmdb系统(蓝鲸)接口采集所有生产主机IP地址.主机名.操作系统.电源状态: 3)以上2步返回数据对比,找出未监控 ...

  6. Redis生产环境节点宕机问题报错及恢复排错

    Redis故障发现 主观下线 当cluster-node-timeout时间内某节点无法与另一个节点顺利完成ping消息通信时,则将该节点标记为主观下线状态. 客观下线 当某个节点判断另一个节点主观下 ...

  7. Docker系列——InfluxDB+Grafana+Jmeter性能监控平台搭建(二)

    在上一篇博文中,主要是讲了InfluxDB的配置,博文链接:https://www.cnblogs.com/hong-fithing/p/14453695.html,今天来分享下Jmeter的配置. ...

  8. 使用pr进行视频的剪辑

    本人作为一个经常浪迹在各个小视频的网站的视频界"gai溜子",很是喜欢一些人上传的综艺或者电影的搞笑搞笑小桥段,有的剪辑下来的片段甚至会比我们自己去看还要有意思的多.我认为视频剪辑 ...

  9. 使用Portainer部署Docker容器实践

    一.背景 最近在使用rancher2.5.5部署Redis主从复制的时候,发现rancher会产生很多iptables的规则,这些规则导致我们在部署了rancher的机器上无法使用Redis的主从复制 ...

  10. IDEA 主题下载

    IDEA中主题可以更换,大家可以直接到 http://www.riaway.com/  网站或 http://color-themes.com/?view=index  网站,直接下载自己喜欢的主题. ...