poj 1840 Eqs

题意比较简单啊,我这个英语菜鸟都可以看懂啊、、、就是说给出a1,a2,a3,a4,a5,然后再[-50,50]的区间内找到x1,x2,x3,x4,x5使得a1*x1^3+ a2*x2^3+ a3*x3^3+ a4*x4^3+ a5*x5^3=0。直接枚举的话会是100^5=100E次一定会超时的啊、、后来看了一下大神的思路就是化简之后再枚举时间降为O(n^2+n^3)看似简单的化简得到的结果却是很大的啊!可以省很多的时间啊、、

这题的思路就是枚举所有的情况,两边进行比较啊、、如果相同就加上所有情况出现的次数、、、

注意数组得定义成short型啊,要不超内存啊、、、

Eqs
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 10745 Accepted: 5213

Description

Consider equations having the following form: 
a1x1 3+ a2x2 3+ a3x3 3+ a4x4 3+ a5x5 3=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

The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.

Output

The output will contain on the first line the number of the solutions for the given equation.

Sample Input

37 29 41 43 47

Sample Output

654
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

short hash[25000001];//int会超内存啊
int main()
{
    int a1, a2, a3, a4, a5;
    int i, j, k, sum, cnt;
    while(~scanf("%d %d %d %d %d",&a1, &a2, &a3, &a4, &a5))
    {
        memset(hash , 0 , sizeof(hash));
        for(i = -50; i <= 50; i++)
        {
            if(!i)
                continue;
            for(j = -50; j <= 50; j++)
            {
                if(!j)
                    continue;
                sum = (-1)*(a1*i*i*i + a2*j*j*j);
                if(sum < 0)
                    sum += 25000000;
                hash[sum] ++;
            }
        }
        cnt = 0;
        for(i = -50; i <= 50; i++)
        {
            if(!i)
                continue;
            for(j = -50; j <= 50; j++)
            {
                if(!j)
                    continue;
                for(k = -50; k <= 50; k++)
                {
                    if(!k)
                        continue;
                    sum = (a3*i*i*i + a4*j*j*j + a5*k*k*k);
                          if(sum < 0)
                              sum += 25000000;
                    if(hash[sum])
                        cnt+= hash[sum];
                }
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值