UVA 568 Just the Facts (水)

时间:2023-03-08 22:08:43

题意:

  求一个数n的阶乘,其往后数第1个不是0的数字是多少。

思路:

  [1,n]逐个乘,出现后缀0就过滤掉,比如12300就变成123,继续算下去。为解决爆long long问题,将其余一个数mod,过滤掉前面过大的部分,因为计算出来也没用。这个mod应该是多少? 10亿就行。

 #include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N=;
const int mod=;
int ans[N]; void init()
{
LL fun=;
ans[]=;
for(int i=; i<N; i++)
{
fun*=i;
while(fun%==)
fun/=; //过滤掉后缀0
ans[i]=fun%; //取最后一位
fun%=mod;
}
} int main()
{
//freopen("input.txt", "r", stdin);
init();
int a;
while(~scanf("%d",&a))
printf("%5d -> %d\n",a,ans[a]);
return ;
}

AC代码