[USACO3.2.1] Factorials阶乘【模拟】

显然我们只关心最后的非零位,所以我们只需要在每次乘阶乘的时候去掉后面的很多个零:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define rep(i,x,y) for(ll i=(x);i<=(y);i++)
#define repl(i,x,y) for(ll i=(x);i<(y);i++)
#define repd(i,x,y) for(ll i=(x);i>=(y);i--)
using namespace std;

const ll Inf=1e9;

ll n,ans=1;

inline ll read() {
    ll x=0;char ch=getchar();bool f=0;
    while(ch>'9'||ch<'0'){if(ch=='-')f=1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return f?-x:x;
}

int main() {
	n=read();
	
	rep(i,1,n) {
		ans*=i;
		while(ans%10==0) ans/=10;
		ans%=Inf;
	}
	
	printf("%lld\n",ans%10);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/yanzhenhuai/article/details/82960243
今日推荐