USACOFactorials

来源:http://ace.delos.com/usacoprob2?a=5LTgWx8eTT9&S=fact4

这题有什么好说的呢?不必麻烦的数学证明,不必高精度。

每次阶乘时把最后的0去掉,并且只需保存两三位数就够了,这样就不用担心只保存一位时相乘后只剩下0的情况。

/*
ID:ay27272
PROG:fact4
LANG:C++
*/

#include <iostream>
#include <cstdio>

using namespace std;

const int NN=10000;

int main()
{
    freopen("fact4.in","r",stdin);
    freopen("fact4.out","w",stdout);
    int n;
    scanf("%d", &n);

    int sum=1;
    for (int i=1; i<=n; i++)
    {
        sum = sum * i;
        while (sum % 10 == 0)
            sum /= 10;
        sum %= NN;
    }

    printf("%d\n", sum % 10);
    return 0;
}
原文地址:https://www.cnblogs.com/ay27/p/2924031.html