51nod 1057 N的阶乘(大数-划分)
实话说,题目我做过,但是再次写到这道题目的时候,我就不再想起用这样的方法。所以,我认为记录下来是很有必要的,
1.可以强化理解
2.可以回顾
这道题目,用大数乘法做太过繁琐。划分其实是将答案划分成可以输出的数据,再以格式化输出。
那么,为什么这题能使用这种方法算大数,原因是虽然乘的数量多,但是其每个乘数不超过10000,
数组记录的每一位于其相乘可以用long long记录,如果以8位一个划分,那么最大也就12位。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define E 2.71828
#define MOD 100000000
#define N 11000
LL a[N];
int main()
{
int n;
scanf("%d",&n);
a[0] = 1;
int m = 1;
for(int i = 1; i <= n; i++)
{
LL t = 0;//printf("sdfs");
for(int j = 0; j < m; j++)
{
LL x = a[j]*i+t;
t = x / MOD;
a[j] = x % MOD;
}
if(t > 0) //jin wei
a[m++] = t;
}
printf("%lld",a[m-1]);
for(int i = m-2; i >= 0; i--)
printf("%0.8lld",a[i]);
printf("\n");
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。