n的阶乘定义为n!=n*(n -1)* (n - 2)* ...* 1。n的双阶乘定义为n!!=n*(n -2)* (n -4)* ...* 2或n!!=n(n - 2)*(n - 4)* ...* 1取决于n的奇偶性,但是阶乘的增长速度太快了,所以我们现在只想知道n!和n!!末尾的的个数
一个正整数n (n <= )
两个整数分别为n!和n!!末尾0的个数
10
2 1
5
1 0
- #include
- #include
- #define ll long long
- using namespace std;
-
- int main()
- {
- freopen("factorial.in", "r", stdin); //此处为海淀区赛指定文件名
- freopen("factorial.out", "w", stdout);//考场代码,必须加文件输入输出
-
- ll n, cnt_5, cnt_2;
- scanf("%lld", &n);
-
- cnt_5 = cnt_2 = 0;
-
- for(ll i = 1; i <= n; i++)
- {
- ll tmp = i;
-
- while(tmp % 5 == 0)
- {
- cnt_5++;
- tmp /= 5;
- }
-
- tmp = i;
-
- while(tmp % 2 == 0)
- {
- cnt_2++;
- tmp /= 2;
- }
- }
-
- printf("%lld ", min(cnt_2, cnt_5));
- cnt_5 = cnt_2 = 0;
-
- if(n % 2 != 0)
- {
- for(ll i = n; i >= 1; i -= 2)
- {
- ll tmp = i;
-
- while(tmp % 5 == 0)
- {
- cnt_5++;
- tmp /= 5;
- }
-
- tmp = i;
-
- while(tmp % 2 == 0)
- {
- cnt_2++;
- tmp /= 2;
- }
- }
- }
- else
- {
- for(ll i = n; i >= 2; i -= 2)
- {
- ll tmp = i;
-
- while(tmp % 5 == 0)
- {
- cnt_5++;
- tmp /= 5;
- }
-
- tmp = i;
-
- while(tmp % 2 == 0)
- {
- cnt_2++;
- tmp /= 2;
- }
- }
- }
-
- printf("%lld", min(cnt_5, cnt_2));
-
- return 0;
- }