zoukankan      html  css  js  c++  java
  • HDU ACM 1496 Equations

    Equations

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3978    Accepted Submission(s): 1602


    Problem Description
    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
     
    Author
    LL
     
    Recommend
    LL
     
    解题思路:暴力+hash,主要是想练练hash,一直认为hash是高端黑,用线性探测再散列处理冲突,记得不知道谁说过处理余数的数一般用上素数,不要忘了最后乘上16,因为四个数有可能是正负
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define MAXN 50001
     4 int num[MAXN], store[MAXN];
     5 
     6 int hash(int cur)
     7 {
     8     int temp = cur%MAXN;
     9     if(temp < 0) temp += MAXN;
    10     while(num[temp] != 0 && store[temp] != cur)
    11     {
    12         temp = (temp+1)%MAXN;
    13     }
    14     return temp;
    15 }
    16 
    17 int main()
    18 {
    19 //    freopen("input.txt", "r", stdin);
    20     int rate[101], a, b, c, d, i, j, sum, temp, res;
    21     for(i=0; i<101; ++i) rate[i] = i*i;
    22     while(scanf("%d%d%d%d", &a, &b, &c, &d) != EOF)
    23     {
    24         if((a>0&&b>0&&c>0&&d>0) || (a<0&&b<0&&c<0&&d<0))
    25         {
    26             printf("0
    ");
    27             continue;
    28         }
    29         memset(num, 0, sizeof(num));
    30         for(i=1; i<101; ++i)
    31             for(j=1; j<101; ++j)
    32             {
    33                 temp = a*rate[i]+b*rate[j];
    34                 res = hash(temp);
    35                 store[res] = temp;
    36                 num[res]++;
    37             }
    38             
    39             sum = 0;
    40             
    41         for(i=1; i<101; ++i)
    42             for(j=1; j<101; ++j)
    43             {
    44                 temp = -(c*rate[i]+d*rate[j]);
    45                 res = hash(temp);
    46                 sum += num[res];
    47             }
    48             
    49         printf("%d
    ", sum*2*2*2*2);
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    数组与字符串的相互转换
    数组新增,修改json数据
    百度Ueditor设置图片自动压缩
    微信小程序——自定义图标组件
    微信小程序——自定义导航栏
    微信小程序——网盘图片预览
    微信小程序——星星评分
    微信小程序——页面中调用组件方法
    Vue路由获取路由参数
    C#随机颜色和随机字母
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/3164964.html
Copyright © 2011-2022 走看看