题解 P1134 【阶乘问题】

· · 题解

emm看到这道题第一想法,模拟嘛!

于是果断写了一个模拟

话说记得开long long哦!

还有模数取大点。。

思路:

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#define Rint register int
#define mem(a,b) memset(a,(b),sizeof(a))
#define Temp template<typename T>
using namespace std;
typedef long long LL;
Temp inline void read(T &x){//快速读入,请忽略
    x=0;T w=1,ch=getchar();
    while(!isdigit(ch)&&ch!='-')ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(isdigit(ch))x=(x<<3)+(x<<1)+(ch^'0'),ch=getchar();
    x=x*w;
}
LL n,ans=1;//定义n,ans
int main(){
    read(n);//输入n
    for(register LL i=1;i<=n;i++){//从1循环到n
        ans=ans*i;//按照题意模拟阶乘
        while(ans%10==0)ans/=10;//除去尾部多余的0
        ans%=100000000;//模上一个大数防止溢出long long
    }
    cout<<ans%10<<endl;//输出最后一位就可以辣!
    return 0;
}