哈理工OJ—1013 Eqs(数学怪题)

本文介绍了一种通过将时间复杂度转化为空间复杂度的方法来求解特定形式方程的解决方案。该方程形如a1x1^3 + a2x2^3 + a3x3^3 + a4x4^3 + a5x5^3 = 0,系数为[-50,50]区间内的整数。文章提供了完整的AC代码实现,展示了如何通过预处理和使用大型数组来高效计算所有可能的非零整数解。

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

http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1013
Eqs
Time Limit: 5000 MS Memory Limit: 65536 K
Total Submit: 566(229 users) Total Accepted: 330(212 users) Rating: Special Judge: No
Description
Consider equations having the following form:
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0
The coefficients are given integers from the interval [-50,50].
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}.

Determine how many solutions satisfy the given equation.
Input
For each test case :

Line 1: Five coefficients a1, a2, a3, a4, a5, separated by blanks.

Process to the end of file.

Output
For each test case :

Line 1: A single integer that is the number of the solutions for the given equation.(The output will fit in 32 bit signed integers.)

Sample Input
37 29 41 43 47
Sample Output
654
本题的做法就是把时间复杂度转化为空间复杂度,就是把五层for给肢解成一个2层for和一个三层for,加一个空间复杂度很高的数组,然后这个题就非常完美的解决掉了。
下面是AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

char num[20000005];

int main()
{
    int  a1,a2,a3,a4,a5;
    while(~scanf("%d %d %d %d %d",&a1,&a2,&a3,&a4,&a5))
    {
        int num1;
        memset(num,0,sizeof(num));
        for(int i=-50;i<=50;i++)
        {
            if(i==0)continue;
            for(int j=-50;j<=50;j++)
            {
                if(j==0)continue;
                num1=a1*i*i*i+a2*j*j*j;
                if(num1<0)
                {
                    num1+=20000000;//此处仔细想想
                }
                num[num1]++;
            }
        }
        int sum=0;
        for(int i=-50;i<=50;i++)
        {
            if(i==0)continue;
            for(int j=-50;j<=50;j++)
            {
                if(j==0)continue;
                for(int k=-50;k<=50;k++)
                {
                    if(k==0)continue;
                    num1=a3*i*i*i+a4*j*j*j+a5*k*k*k;
                    if(num1<0)num1+=20000000;
                    if(num[num1])
                    sum+=num[num1];
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值