Equations(方程)

探讨了如何解决特定形式的四元二次方程组,即a*x1^2+b*x2^2+c*x3^2+d*x4^2=0,其中系数在[-50,50]区间内,提出两种高效算法:构建哈希表优化暴力求解及两部分求和法,将复杂度从n^4降低至2*n^2。

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

acm.hdu.edu.cn/showproblem.php?pid=1496 

Consider equations having the following form: 

a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 
a, b, c, d are integers from the interval [-50,50] and any of them cannot be 0. 

It is consider a solution a system ( x1,x2,x3,x4 ) that verifies the equation, xi is an integer from [-100,100] and xi != 0, any i ∈{1,2,3,4}. 

Determine how many solutions satisfy the given equation. 

Input

The input consists of several test cases. Each test case consists of a single line containing the 4 coefficients a, b, c, d, separated by one or more blanks. 
End of file.

Output

For each test case, output a single line containing the number of the solutions. 

Sample Input

1 2 3 -4
1 1 1 1

Sample Output

39088
0

 

题意:

给你a,b,c,d这4个数的值,然后问a*x1^2 + b*x2^2 +  c*x3^2 + d*x4^2 = 0的(x1,x2,x3,x4)解一共有多少种

思路:

第一种——>构建哈希表,优化暴力求解方程;

#include<stdio.h>
#include<string.h>
#include<algorithm>
typedef long long ll;
const ll N=2e6+10;
using namespace std;
int ha[N],p[110];
int main()
{
    int a,b,c,d;
    for(int i=1; i<101; i++)
        p[i]=i*i;
    while(~scanf("%d %d %d %d",&a,&b,&c,&d))
    {
        memset(ha,0,sizeof(ha));
        if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0))///全正全负都排除,剪枝
        {
            printf("0\n");
            continue;
        }
        for(int i=1; i<101; i++)
            for(int j=1; j<101; j++)
                ha[a*p[i]+b*p[j]+1000000]++;
        int sum=0;
        for(int i=1; i<101; i++)
            for(int j=1; j<101; j++)
                sum+=ha[-c*p[i]-d*p[j]+1000000];
        printf("%d\n",sum*16);///每个数都可正可负   16中情况
    }
    return 0;
}

第二种——>分开两部分求和,若两部分的和是0,则就相加那么多种,最后乘以16。这样就能从n^4(4层for循环)变成2*n^2(2层for循环);

#include<stdio.h>
#include<string.h>
#include<algorithm>
typedef long long ll;
const ll N=1e6+10;
using namespace std;
int z[N],f[N];///正   负
int main()
{
    int a,b,c,d;
    while(~scanf("%d %d %d %d",&a,&b,&c,&d))
    {
        memset(z,0,sizeof(z));
        memset(f,0,sizeof(f));
        if((a<0&&b<0&&c<0&&d<0)||(a>0&&b>0&&c>0&&d>0))///全正全负肯定无解
        {
            printf("0\n");
            continue;
        }
        for(int i=1; i<101; i++)
            for(int j=1; j<101; j++)
            {
                int k=a*i*i+b*j*j;
                if(k>=0)z[k]++;///k=0也要考虑在内    因为a,b可能一正一负相加最后为0;
                if(k<0)f[-k]++;
            }
        int sum=0;
        for(int i=1; i<101; i++)
            for(int j=1; j<101; j++)
            {
                int k=c*i*i+d*j*j;
                if(k<=0)sum+=z[-k];///!
                if(k>0)sum+=f[k];
            }
        printf("%d\n",sum*16);///16理由同上  (2^4=16);
    }
    return 0;
}

 

在我想东想西或者什么都不想的时候。等待的车,等待的人,就这样飞驰而去。

只要想到人生那么长,外面的世界那么精彩,我就想去看看。我的努力是想去体验一个更大的世界,我不想停下我追求生命意义的脚步。

希望我们成长的速度,比父母老去的速度快一些。这大概就是我们努力的意义吧。

赖床舒服,但可能迟到;熬夜很爽,但伤身体;玩手机有趣,可你也许会发现,时间流逝,你却没有进步。

人要明白,越努力,越幸运,自律的程度,决定人生的高度。

愿所有不满都化为将来成功的动力。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵩韵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值