设为首页 加入收藏

TOP

POJ 1840 数学
2015-07-20 17:52:18 来源: 作者: 【 】 浏览:1
Tags:POJ 1840 数学

Eqs
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 12182 Accepted: 5951

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

Source

Romania OI 2002


题目链接:http://poj.org/problem?id=1840


题意就是给出5个系数,求使等式成立的可能个数,注意等式中x不是一定都相同的



看完别人的题解,感觉自己真的好傻,我想到的是4重循环,想不到别人用三重循环


思路:

原试子变形就是a1x13+ a2x23+ a3x33=( a4x43+ a5x53),用两个循环,一个求左边,一个求右边(因为4重循环超时),

对于数据,看到别人用的 25000000,这有技巧的,第一次算右边的,最大就是50^4*2=12500000,乘以2因为可能是负数,

而负数数组不能存储,所以当是负数的时候就+25000000,这里我强调一下,-1+N 不是等于1 !!!!!刚才纠结了一下

,还有大牛们说用int 会超类存,用short,具体看代码吧



#include
  
   
#include
   
     #include
    
      #include
     
       using namespace std; #define N 25000000 short a[N]; int main() { int b[5],i,j,k,s; scanf("%d%d%d%d%d",&b[0],&b[1],&b[2],&b[3],&b[4]); { for(i=-50;i<=50;i++) { if(i==0) continue; for(j=-50;j<=50;j++) { if(j==0) continue; k=i*i*i*b[0]+j*j*j*b[1]; if(k<0) //负数不能存储 k+=N; a[k]++; } } int ans=0; for(i=-50;i<=50;i++) { if(i==0) continue; for(j=-50;j<=50;j++) { if(j==0) continue; for(k=-50;k<=50;k++) { if(k==0) continue; s=i*i*i*b[2]+j*j*j*b[3]+k*k*k*b[4]; if(s<0) s+=N; ans+=a[s]; } } } printf("%d\n",ans); } return 0; } 
     
    
   
  








】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++设计模式之建造者模式(二) 下一篇poj 2187 Beauty Contest , 旋转..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: