n!末尾有多少个0

  • https://leetcode.com/problems/factorial-trailing-zeroes/
int trailingZeroes(int n) {
    // n!末尾0的个数 等于 [1..n]中因子5的个数
    // 即 n/5 + n/25 + n/125 + ...
    int ans = 0;
    while (n /= 5) {
        ans += n;
    }
    return ans;
}

满足x!末尾刚好K个0的数x有多少个

参见二分搜索的应用