[luoguP1134] 阶乘问题(数论)

传送门

我直接用 long long 暴力,居然过了

——代码

#include <cstdio>

int n;
long long x, ans = 1;

int main()
{
	int i;
	scanf("%d", &n);
	for(i = 1; i <= n; i++)
	{
		x = i;
		while(!(x % 10)) x /= 10;
		x %= 1000000000;
		ans *= x;
		while(!(ans % 10)) ans /= 10;
		ans %= 1000000000;
	}
	printf("%lld
", ans % 10);
	return 0;
}

 

有个比较好理解的方法是

因为末尾的0是由因子 2 和 因子 5 乘出来的

所以取出每一个数的因数 2 和 5,两者相抵消,最后再把没有抵消掉的乘回去

原文地址:https://www.cnblogs.com/zhenghaotian/p/7049956.html