P1134 [USACO3.2]阶乘问题 题解
题意
太简洁明了,我就不写了。
给你一个数
思路
末尾
先统计因子
这里拿样例举个例子:
统计出含有总共
温馨提示
请边算边模,并建议您使用快速幂来算余下因子
上代码
#include <bits/stdc++.h>
using namespace std;
int n,ct2,ct5,ans=1,ff;
int ksm(int x,int y)
{
int ret=1;
if(y==0) return ret;
while(y)
{
if(y&1)
{
ret=ret*x%10;
}
x=x*x%10;
y>>=1;
}
return ret;
}
int main()
{
//freopen("factorials.in","r",stdin);
//freopen("factorials.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int r1=i,r2=i,vt=i;
while(r1%2==0)
{
r1/=2,++ct2;vt/=2;
}
while(r2%5==0)
{
r2/=5,++ct5;vt/=5;
}
ans=ans*vt%10;
}
//printf("%d %d\n",ct2,ct5);
if(ct2>ct5) ff=ksm(2,(ct2-ct5));
else ff=ksm(5,(ct5-ct2));
//if(ct2==0||ct5==0) ff=1;
//printf("%d %d\n",ans,ff);
printf("%d",ans*ff%10);
return 0;
}