Eqs
Time Limit: 5000 MS Memory Limit: 65536 K
Total Submit: 731(302 users) Total Accepted: 413(278 users) Rating: Special Judge: No
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
For each test case :
Line 1: Five coefficients a1, a2, a3, a4, a5, separated by blanks.
Process to the end of file.
Output
For each test case :
Line 1: A single integer that is the number of the solutions for the given equation.(The output will fit in 32 bit signed integers.)
Sample Input
37 29 41 43 47
Sample Output
654
思路:
每个数只是[-50,50]一共100个数,循环100 5次?
一定会超时,所以我们要用别的方法。
枚举x1,x2的值,并且记录下来。再枚举x3,x4,x5的值。
如果发现有互为相反数的,说明有一个解存在。
a1x13 + a2x23的范围即4 * 504,即25 000 000
(int超内存,即用short int 数组即可)
注释见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
short h[25000001];
#define FOR(x) for(int x = -50;x <= 50;x ++)
int main()
{
int a1,a2,a3,a4,a5;
while(scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5)!=EOF)
{
memset(h,0,sizeof(h));
FOR(x1)
{
if(x1) //非0为真,0为假;
FOR(x2)
{
if(x2)
{
int sum=(a1*x1*x1*x1+a2*x2*x2*x2)*(-1);
if(sum < 0) sum += 25000000; //数组没有负的,加25000000;
h[sum]++;
}
}
}
int res=0;
FOR(x3)
{
if(x3)
FOR(x4)
{
if(x4)
FOR(x5)
{
if(x5)
{
int sum=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
if(sum<0) sum+=25000000;
if(h[sum])
res+=h[sum];
}
}
}
}
printf("%d\n",res);
}
}
//这个题的key是sum,遍历得到的半边数列和的值;
C++标准规定,int占一个机器字长。在32位系统中int占32位,也就是4个字节,而在老式的16位系统中,int占16位,即2个字节。而C++标准中只限制规定short int不能超过int的长度,具体长度的可以由C++编译器的实现厂商自行决定。目前流行的32位C++编译器中,通常int占4字节,short int占2字节。其中short int可以简写为short。类似地,C++标准只限制了long int不得小于int的长度,具体也没有作出限制。
int 超内存,所以改成short int 数组的原因:通常int占4字节,short int占2字节?