4949. 末尾连续0

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)的复杂度

  • 代码

    /*
    * @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;
    }
    

猜你喜欢

转载自blog.csdn.net/qq_45859188/article/details/130045748