D - Equations(hash)

D - Equations

HDU - 1496

Consider equations having the following form:

a*x1^2+b*x2^2+c*x3^2+d*x4^2=0
a, b, c, d are integers from the interval [-50,50] and any of them cannot be 0.

It is consider a solution a system ( x1,x2,x3,x4 ) that verifies the equation, xi is an integer from [-100,100] and xi != 0, any i ∈{1,2,3,4}.

Determine how many solutions satisfy the given equation.

Input

The input consists of several test cases. Each test case consists of a single line containing the 4 coefficients a, b, c, d, separated by one or more blanks.
End of file.

Output

For each test case, output a single line containing the number of the solutions.

Sample Input

1 2 3 -4
1 1 1 1

Sample Output

39088
0

思路:原等式可以转化为:a*x1^2+b*x2^2=-(c*x3^2+d*x4^2),假设a*x1^2+b*x2^2=k,则c*x3^2+d*x4^2=-k,让等式左边的值对应于数组下标,对应数组的值加1,表示情况数,接着遍历-(c*x3^2+d*x4^2),加上遍历到对应下标为-(c*x3^2+d*x4^2)的数组值,作为增加的情况数,有一点需要注意的是,数组下标大于等于零,所以把等式两边对应的值都加上1000000保证下标为非负数。另外,对于x的值,平方后结果不变,所以计算x在区间[-100,-1]和在区间[1,100]的结果是样的,所以所有x1,x2,x3,x4的结果应该乘以2*2*2*2=16作为总结果。

AC代码:

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=1000000+5;
const int MOD = 1e5+ 7;
int n,m;
int has[2*N];
int main()
{
    int a,b,c,d;
    while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
    {
        if(a>0&&b>0&&c>0&&d>0||a<0&&b<0&&c<0&&d<0){
            cout<<0<<endl;
            continue;
        }
        memset(has,0,sizeof(has));
        for(int i=1;i<=100;i++)
        for(int j=1;j<=100;j++){
            has[a*i*i+b*j*j+1000000]++;
        }
        int sum=0;
        for(int i=1;i<=100;i++)
            for(int j=1;j<=100;j++)
            sum+=has[1000000-c*i*i-d*j*j];
        printf("%d\n",sum*16);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/clz16251102113/article/details/81190273