zoukankan      html  css  js  c++  java
  • poj 1840(五元三次方程组)

    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
    题意很好懂,倘若用暴力写的话明显不行;这道题我大一(现在还是大一)学长出了很多次,当时水平不行,也不懂哈希算法,下学期学了数据结构,学了哈希表,一看
    别人的代码就明白了怎么整的了,首先将方程拆分,分成左右两部分,构建一个哈希数组(hash),hash数组里面的位序代表左边的结果,hash数组对应的数字的大小
    就代表这个结果的次数,然后右边的结果如果在这哈希数组对应,则 ans=ans+hash【i】
    注意:因为位序不能为负数,左边结果的范围为【-50*50*50*50*2,50*50*50*2】将负数变为正数,加上最大值就可以了,右边同样,hash数组过大,用
    short 定义
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 using namespace std;
     6 short hash[25000200];
     7 int main()
     8 {
     9     int a,b,c,d,e;
    10     int i,j,k;
    11     while(cin>>a>>b>>c>>d>>e)
    12     {
    13         int sum;
    14         //memset(hash,0,sizeof(hash));
    15         for(i=-50;i<=50;i++)
    16         {
    17             for(j=-50;j<=50;j++)
    18             {
    19                 if(j!=0&&i!=0)
    20                 {
    21                     sum=-(a*i*i*i+b*j*j*j);
    22                     if(sum<0)
    23                         sum=sum+25000000;
    24                     hash[sum]++;
    25                 }
    26             }
    27         }
    28         int ans=0;
    29         for(i=-50;i<=50;i++)
    30         {
    31             for(j=-50;j<=50;j++)
    32             {
    33                 for(k=-50;k<=50;k++)
    34                 {
    35                     if(i!=0&&j!=0&&k!=0)
    36                     {
    37                         sum=c*i*i*i+d*j*j*j+e*k*k*k;
    38                         if(sum<0)
    39                             sum=sum+25000000;
    40                         if(hash[sum]>0)
    41                             ans=ans+hash[sum];
    42                     }
    43                 }
    44             }
    45         }
    46         cout<<ans<<endl;
    47     }
    48 }
  • 相关阅读:
    iOS开发-UIColor和UIImage之间的转换
    iOS开发-给用户好的印象
    iOS开发之国际化(本地化)
    git学习之创建分支和合并分支
    iOS设置StatusBar字体颜色问题
    iOS开发技巧,细节(二)
    iOS开发,[NSBundle mainBundle] pathForResource方法返回空的问题解决
    【MM MRP】PA-SCM525 Unit2 Lot-Size$Rounding Profile(MRP_2)
    【MM MRP】PA-I (MRP_1)
    【MM 发票】MM MIRO界面中的ITEM LIST的布局可自己定义(转)
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/5682986.html
Copyright © 2011-2022 走看看