EricYang

Tech Spot of Eric

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Eqs
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5966 Accepted: 2885

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

Source

 

//写了枚举,超慢。

 

#include <iostream>

using namespace std;

int main()
{
    int a1,a2,a3,a4,a5;
    int cnt;

    while(cin>>a1>>a2>>a3>>a4>>a5)
    {
        cnt=0;
        for(int i=-50; i<=50; i++)
            for(int j=-50; j<=50; j++)
                for(int k=-50; k<=50; k++)
                   for(int m=-50; m<=50; m++)
                      for(int n=-50; n<=50; n++)
                      {
                          if(n!=0 && m!=0 && k!=0 && j!=0 && i!=0)
                              if(a1*i*i*i+a2*j*j*j+a3*k*k*k+a4*m*m*m+a5*n*n*n==0)
                                  cnt++;
                      }
        cout<<cnt<<endl;


    }

    return 0;

思路:hash的应用。暴力枚举的话会达到100^5,明显会超时。所以将方程分成-(a1x13+ a2x23 )和 a3x33+a4x43+ a5x53 两部分,若这两部分相等,则为方程的一个解。

#include <iostream>
#include <cstring>
using namespace std;

const int MAX_NUM=25000000;
char hash[MAX_NUM+1];

int main()
{
    int a1,a2,a3,a4,a5;
    int cnt;
    int temp,hashcode;


    while(cin>>a1>>a2>>a3>>a4>>a5)
    {
        cnt=0;
        memset(hash,0,sizeof(hash));
        //compute first 2 terms
        for(int i=-50; i<=50 ;i++)
        {
            for(int j=-50; j<=50; j++)
            {
                if(i!=0&&j!=0)
                {
                    temp=a1*i*i*i+a2*j*j*j;
                    hash[temp+MAX_NUM/2]++;
                }
            }
        }

        for(int i=-50; i<=50 ;i++)
        {
            for(int j=-50; j<=50; j++)
            {
                for(int k=-50; k<=50; k++)
                {
                    if(i!=0&&j!=0&&k!=0)
                    {
                        temp=a3*i*i*i+a4*j*j*j+a5*k*k*k;
                        if(temp<=MAX_NUM/2 && temp>= -MAX_NUM/2)
                            cnt+=hash[-temp+(MAX_NUM/2)];
                    }
                }
            }
        }

        cout<<cnt<<endl;


    }

    return 0;
}

posted on 2011-04-20 18:50  Eric-Yang  阅读(394)  评论(0编辑  收藏  举报