【每日一题16】AcWing 1381. 阶乘

Day16 AcWing 1381. 阶乘

思路

  1. 设$n!$末尾有$k$个0,求$\frac {n!}{10^k} mod 10$

拓展

  1. AcWing 197. 阶乘分解
  2. LaTex数学公式笔记—数学结构

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
int n;
cin >> n;
int res = 1, d2 = 0, d5 = 0;
for (int i = 1; i <= n; i ++ )
{
int x = i;
while (x % 2 == 0) x /= 2, d2 ++ ;
while (x % 5 == 0) x /= 5, d5 ++ ;
res = res * x % 10;
}

for (int i = 0; i < d2 - d5; i ++ ) res = res * 2 % 10;
cout << res << endl;

return 0;
}