51nod 1057 N的阶乘(大数-划分)

来源:互联网 发布:物业app软件取名 编辑:程序博客网 时间:2024/05/17 23:52

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 11000LL 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;}
0 0
原创粉丝点击