本文迁移自老博客,原始链接为 https://seven.blog.ustc.edu.cn/usacofactorials/
求阶乘最后的非零位,n<=4220 解法:直接模拟,每次乘法后保留后四位数
#include<iostream>
#include<algorithm>
#include<fstream>
using namespace std;
ofstream fout ("fact4.out");
ifstream fin ("fact4.in");
int n;
int main()
{
fin>>n;
int t = 1;
for(int i = 2 ; i <= n ; i++)
{
t = t * i;
while(t % 10 == 0)
t /= 10;
t %= 10000;
}
fout<<t%10<<endl;
return 0;
}