折半枚举+Hash(HDU1496升级版)

针对一个四元二次方程的非零整数解计数问题,文章提出了折半枚举结合Hash的解决方案。首先存储并排序X1和X2的解,然后对每个可能的(CX3^2 + DX4^2),在已排序的解中使用二分查找找到匹配项。当方程范围扩大导致数组无法存储时,使用Hash表来优化。代码实现中需要注意整数溢出问题,使用long long类型。这种方法适用于解决寻找特定和的数值对的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:N - 方程的解

给定一个四元二次方程:
Ax1^2+Bx2^2+Cx3^2+Dx4^2=0
试求−1000≤x1,x2,x3,x4≤1000非零整数解的个数。
−10000≤A,B,C,D≤10000

输出解的个数。

解法:
首先这道题直接用网上HDU1496的板子过不去,原因是1e10的数组开不了那么大的。所以这里只能换思路。新思路如下(很典型的折半枚举,也就是meet-in-middle):

  1. 把X1,X2的答案存下来(存在一个2000*2000的数组里面),然后排序
  2. 二分查找这个数组里面有多个数x了
  3. AX_1^2+BX_2^2=-(CX_3^2+DX_4^2)
  4. 左边式子的答案我们已经存下来了,接下来算右边的
  5. 右边答案算出来过后,我们直接在左边数组里面二分有多少个一样的数值,答案加上这个数值就ok了

当然这里用数组会稍显笨拙,可以用map,卡时间可以过。
注意s[a*i*i + b*j*j]++会爆int,因此需要将a改为long long

代码如下:

#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
map<ll, ll>s;

int main() {
    ll a, b, c, d;
    while (scanf("%lld %lld %lld %lld", &a, &b, &c, &d) != EOF) {
        if (a * b > 0 && b * c > 0 && c * d > 0) {
            printf("0\n");
            return 0;
        }
        for (ll i = 1; i <= 1000; i++) {
            for (ll j = 1; j <= 1000; j++) {
                //if (i == 0 || j == 0)continue;
                s[a*i*i + b*j*j]++;
            }
        }
        ll sum = 0;
        for (ll i = 1; i <= 1000; i++) {
            for (ll j = 1; j <= 1000; j++) {
                //if (i == 0 || j == 0)continue;
                ll t = c*i*i + d*j*j;
                sum += s[0 - t];
            }
        }
        printf("%lld\n", 16*sum);
    }
    return 0;
}

上面说了卡常数不是很严的做法,如果卡常数很严的话,比如x的范围变到4000,map就会T掉,这里直接采用hash的方法

关键词:数字hash

例题:Uva1152:4 Values whose Sum is 0

//hash数字编码
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
map<ll, ll>s;
int a[4005], b[4005], c[4005], d[4005];
int n, T, cnt;

//w[i]表示第i个结点存储的数(也就是a+b),st[i]表示第i个结点有多少种表示方法
const int hashsize = 1000003;
int hd[hashsize], nxt[16000005], w[16000005], st[16000005]; 
void in(int x) {
    int h = (x % hashsize + hashsize) % hashsize, u = hd[h];
    while (u) {
        if (w[u] == x) {
            st[u]++;
            return;
        }
        u = nxt[u];
    }
    nxt[++cnt] = hd[h];
    hd[h] = cnt;
    w[cnt] = x;
    st[cnt] = 1;
}

int srch(int x) {
    int h = (x % hashsize + hashsize) % hashsize;//查询的数是负数,所以要这么算;
    int u = hd[h];
    while (u) {
        if (w[u] == x) return st[u];
        u = nxt[u];
    }
    return 0;
}

int main() {
    scanf("%d", &T);
    while (T--) {
        cnt = 0; memset(hd, 0, sizeof(hd));
        scanf("%d", &n);
        int A, B, C, D;
        for (int i = 0; i < n; i++) {
            scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
        }
        for (ll i = 0; i < n; i++) {
            for (ll j = 0; j < n; j++) {
                in(a[i] + b[j]);
            }
        }
        ll sum = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++){
                sum += srch(-c[i] - d[j]);
            } 
        }
        printf("%lld\n", sum);
        if (T) printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值