4949. 末尾连续0

摘要
Title: 4949. 末尾连续0
Tag: 阶乘
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

4949. 末尾连续0

  • 题意

    给定一个正整数 m,请你统计一共有多少个正整数 n满足,n的阶乘的末尾连续 0的数量恰好为 m
    输出满足条件的 n的数量以及所有满足条件的 n
    例如,当 m=1时,满足条件的正整数 n共有 5个,分别是 5,6,7,8,9,其中5!=120,6!=720,7!=5040,8!=40320,9!=362880

  • 思路

    比较经典的阶乘问题
    某个数的尾部0的个数 = 质因子中2和5的个数最小值
    n的阶乘有多少质因子p:之前写过公式,log(n)的复杂度

  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    /*
    * @Author: NEFU AB-IN
    * @Date: 2023-04-09 17:17:53
    * @FilePath: \Acwing\98cp\c\test.cpp
    * @LastEditTime: 2023-04-09 18:35:14
    */
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #undef int

    #define SZ(X) ((int)(X).size())
    #define ALL(X) (X).begin(), (X).end()
    #define IOS \
    ios::sync_with_stdio(false); \
    cin.tie(nullptr); \
    cout.tie(nullptr)
    #define DEBUG(X) cout << #X << ": " << X << '\n'
    typedef pair<int, int> PII;

    const int N = 1e9 + 10, INF = 0x3f3f3f3f;

    // n的阶乘有多少质因子p
    int func(int n, int p)
    {
    int ans = 0;
    while (n)
    ans += n / p, n /= p;
    return ans;
    }

    signed main()
    {
    IOS;
    int m;
    cin >> m;
    vector<int> v;
    for (int i = 1; i <= N; ++i)
    {
    int res = min(func(i, 2), func(i, 5));
    if (res == m)
    v.push_back(i);
    else if (res > m)
    break;
    }
    cout << SZ(v) << '\n';
    for (auto i : v)
    cout << i << " ";
    return 0;
    }
使用搜索:谷歌必应百度