题解 | #整除问题#

整除问题

https://www.nowcoder.com/practice/8e29045de1c84d349b43fdb123ab586a

主打一个分解质因数

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cmath>
using namespace std;

int main()
{
    int a, n, i, j;
    cin >> n >> a;
    // 1.先对n!分解质因数
    int yinzi[n], zancun;
    for (i = 0; i < n; i++)
    {
        yinzi[i] = 0;
    }
    for (i = 1; i <= n; i++) // 从1~n都是n!的因子,但是我们还要对其进行拆解,把因子改成质因子
    {
        zancun = i;
        for (j = 2; j < zancun; j++)
        {
            while (zancun % j == 0)
            {
                zancun /= j;
                yinzi[j]++;
                // cout << "因子:" << j << ",从i为:" << i << "来的" << endl;
            }
        }
        yinzi[zancun]++;
        // cout << "因子:" << zancun << ",从i为:" << i << "来的" << endl;
    }
    // for (i = 0; i < n; i++)
    // {
    //     cout << yinzi[i] << endl;
    // }
    // 2.对a分解质因数
    int temp[a];
    zancun = a;
    for (i = 0; i < zancun; i++)
    {
        temp[i] = 0;
    }
    for (i = 2; i <= zancun; i++)
    {
        while (zancun % i == 0)
        {
            zancun /= i;
            temp[i]++;
            // cout << "因子:" << i << endl;
        }
    }
    int minx = 10000;
    for (i = 2; i < a; i++)
    {
        // cout << "hdu" << endl;
        if (temp[i] != 0 && yinzi[i] != 0)
        {
            // cout << yinzi[i] << temp[i] << endl;
            minx = min(minx, yinzi[i] / temp[i]);
        }
    }
    cout << minx;

    return 0;
}

全部评论

相关推荐

野猪不是猪🐗:现在的环境就是这样,供远大于求。 以前卡学历,现在最高学历不够卡了,还要卡第一学历。 还是不够筛,于是还要求得有实习、不能有gap等等... 可能这个岗位总共就一个hc,筛到最后还是有十几个人满足这些要求。他们都非常优秀,各方面都很棒。 那没办法了,看那个顺眼选哪个呗。 很残酷,也很现实
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务