现在的位置: 首页 > 综合 > 正文

poj 1840 Eqs

2014年09月05日 ⁄ 综合 ⁄ 共 1458字 ⁄ 字号 评论关闭

题意比较简单啊,我这个英语菜鸟都可以看懂啊、、、就是说给出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: 
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

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;
}

抱歉!评论已关闭.