bzoj1053: [HAOI2007]反素数ant

把数分解成质数乘积的形式,它的约数个数就可以表示成每个质因数的指数+1的和

明显一个可能是答案的数较小的质因数的指数应比较大质因数的指数小

然后暴力枚举出这样的数就行了

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define MAXP 10
long long n;
int p[MAXP] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
struct qtype
{
    long long data;
    int ct[MAXP];
    qtype(long long _data, int *_ct)
        : data(_data)
    {
        memcpy(ct, _ct, sizeof(ct));
    }
    bool operator> (const qtype &x)const
    {
        return data > x.data;
    }
};
int ct[MAXP] = {0};
priority_queue<qtype, vector<qtype>, greater<qtype> > q;
int main()
{
    scanf("%lld", &n);
    q.push(qtype(1, ct));
    long long ans = 1, mct = 0, last = 0;
    while (!q.empty())
    {
        long long a = q.top().data;
        memcpy(ct, q.top().ct, sizeof(ct));
        q.pop();
        if (a == last)
            continue;
        last = a;
        long long t = 1;
        for (int i = 0; i < MAXP; ++i)
            t *= ct[i] + 1;
        for (int i = 0; i < MAXP && a * p[i] <= n; ++i)
        {
            if (i && ct[i] + 1 > ct[i - 1])
                continue;
            ++ct[i];
            q.push(qtype(a * p[i], ct));
            --ct[i];
        }
        if (t > mct)
            mct = t, ans = a;
    }
    printf("%lld\n", ans);
    return 0;
}


阅读更多
文章标签: bzoj
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭