洛谷P2726 阶乘 Factorials 数学
因为向要在末尾出现 零 只有在 有一对 2 和 5 的情况下
因为 5 比 2 多 只要将 5 的数 和 2 的个数记录下来,然后有几个5 就是有几个2被
拼掉了,然后乘上这写多余的2就行了
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdlib> 5 #include <string> 6 #include <algorithm> 7 #include <iomanip> 8 #include <iostream> 9 using namespace std ; 10 11 inline int read() 12 { 13 char ch = getchar() ; 14 int x = 0,f = 1 ; 15 while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ;ch = getchar() ; } 16 while(ch>='0'&&ch<='9') { x = x*10+ch-48 ; ch = getchar() ; } 17 return x*f ; 18 } 19 20 int n,ans,x,y2,y5 ; 21 22 int main() 23 { 24 n = read() ; 25 ans = 1 ; 26 for(int i=2;i<=n;i++) 27 { 28 x = i ; 29 while( x%5==0 ) 30 x/=5 ,y5++ ; 31 while( !(x&1) ) 32 x=x>>1,y2++ ; 33 if(y2>y5) y2-=y5,y5=0 ; 34 ans = ans*x%10 ; 35 } 36 y2 = y2-y5 ; 37 for(int i=1;i<=y2;i++) ans=ans*2% 10 ; 38 printf("%d ",ans) ; 39 return 0 ; 40 }