POJ - 1840 - Eqs = 思维

http://poj.org/problem?id=1840

题意:求 \(a_1x_1^3+a_2x_2^3+a_3x_3^3+a_4x_4^3+a_5x_5^3=0\) 的整数解,其中所有变量的取值都是 \([-50,50]\) ,且 \(x_i \neq 0\)

暴力枚举,但是要怎么分两半呢?事实证明是前半部分分2个,后半部分分3个会更好,为什么呢?

大概是多了一个 \(\log_{2}{100}\)吧,也是差不多7倍常数了。

前半部分分两个是:
\(O(n^2\log(n^2)+n^3\log(n^2))\)

前半部分分三个就白白多了7倍常数,实属逗比。

可惜POJ用不了unordered_map,待会手写一发hash看看?

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;

map<int, int> M;

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    int a1, a2, a3, a4, a5;
    scanf("%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5);
    for(int x1 = -50; x1 <= 50; ++x1) {
        if(x1 == 0)
            continue;
        int p1 = a1 * x1 * x1 * x1;
        for(int x2 = -50; x2 <= 50; ++x2) {
            if(x2 == 0)
                continue;
            int p2 = a2 * x2 * x2 * x2;
            M[p1 + p2]++;
        }
    }
    ll ans = 0;
    for(int x3 = -50; x3 <= 50; ++x3) {
        if(x3 == 0)
            continue;
        int p3 = a3 * x3 * x3 * x3;
        for(int x4 = -50; x4 <= 50; ++x4) {
            if(x4 == 0)
                continue;
            int p4 = a4 * x4 * x4 * x4;
            for(int x5 = -50; x5 <= 50; ++x5) {
                if(x5 == 0)
                    continue;
                int p5 = a5 * x5 * x5 * x5;
                map<int, int>::iterator it = M.find(-p3 - p4 - p5);
                if(it != M.end())
                    ans += it->second;
            }
        }
    }
    printf("%lld\n", ans);
}

猜你喜欢

转载自www.cnblogs.com/Inko/p/11729194.html