• [Swust OJ 166]--方程的解数(hash法)


    题目链接:http://acm.swust.edu.cn/problem/0166/

    Time limit(ms): 5000      Memory limit(kb): 65535
     

    有如下方程组: A1*x1^3+A2*x2^3+A3*x3^3+A4*x4^3+A5*x5^3=0,其中A1…A5都在[-50,50]内。 如果(x1,x2,x3,x4,x5)(其中-50<=xi<=50,xi!=0)能让该等式成立,就说(x1,x2,x3,x4,x5)为其一组解,现在的问题是要问你该等式共有多少组解。

    Description

    输入包括5个系数 A1,A2,A3,A4,A5


    注意:这里是多组输入哈,太坑爹了!

    Input

    输出该方程解的数目

    Output
    1
    23 45 36 13 57
    Sample Input
    1
    1436
     
     
    解题思路:明显的hash,但是想了想,这里的系数[-50,50],x范围[-50,50],
         那么求得的解的变换范围为[-12500000,12500000]数组大小25000000(内存直接爆了)
         表示智商捉急~~然后晓得了还有short 这个东东(http://bbs.csdn.net/topics/370037447
         然后卡到内存ac了
    代码如下:
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define maxn 25000010
     4 short hash[maxn];
     5 int main(){
     6     int a[6], x1, x2, x3, x4, x5, cnt, temp;
     7     while (scanf("%d%d%d%d%d", &a[1], &a[2], &a[3], &a[4], &a[5]) != EOF){
     8         cnt = 0;
     9         memset(hash, 0, sizeof(hash));
    10         for (x1 = -50; x1 <= 50; x1++){
    11             if (x1){
    12                 for (x2 = -50; x2 <= 50; x2++){
    13                     if (x2){
    14                         temp = a[1] * x1*x1*x1 + a[2] * x2*x2*x2;
    15                         if (temp<0) 
    16                             temp += maxn;
    17                         hash[temp]++;
    18                     }
    19                 }
    20             }
    21         }
    22         for (x3 = -50; x3 <= 50; x3++){
    23             if (x3){
    24                 for (x4 = -50; x4 <= 50; x4++){
    25                     if (x4){
    26                         for (x5 = -50; x5 <= 50; x5++){
    27                             if (x5){
    28                                 temp = -(a[3] * x3*x3*x3 + a[4] * x4*x4*x4 + a[5] * x5*x5*x5);
    29                                 if (temp<0) 
    30                                     temp += maxn;
    31                                 if (hash[temp])
    32                                     cnt += hash[temp];
    33                             }
    34                         }
    35                     }
    36                 }
    37             }
    38         }
    39         printf("%d
    ", cnt);
    40     }
    41     return 0;
    42 }
    View Code

    但是发现有人低内存ac了,然后,然后,把每次求得的值MOD一个数,然后~~~(智商啊)

     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <string.h>
     4 #define maxn 200005
     5 int hash[maxn][10], num[maxn];
     6 int main(){
     7     int i, j, k, l, cnt, tmp, mark, a[5];
     8     while (~scanf("%d%d%d%d%d", &a[0], &a[1], &a[2], &a[3], &a[4])){
     9         memset(num, 0, sizeof(num));
    10         cnt = 0;
    11         for (i = -50; i <= 50; i++){
    12             if (i){
    13                 for (j = -50; j <= 50; j++){
    14                     if (j){
    15                         tmp = a[0] * i*i*i + a[1] * j*j*j;
    16                         mark = abs(tmp) % maxn;
    17                         hash[mark][num[mark]] = tmp;
    18                         num[mark]++;
    19                     }
    20                 }
    21             }
    22         }
    23         for (i = -50; i <= 50; i++){
    24             if (i){
    25                 for (j = -50; j <= 50; j++){
    26                     if (j){
    27                         for (k = -50; k <= 50; k++){
    28                             if (k){
    29                                 tmp = a[2] * i*i*i + a[3] * j*j*j + a[4] * k*k*k;
    30                                 mark = abs(tmp) % maxn;
    31                                 for (l = 0; l < num[mark]; l++)
    32                                 if (tmp == hash[mark][l]) cnt++;
    33                             }
    34                         }
    35                     }
    36                 }
    37             }
    38         }
    39         printf("%d
    ", cnt);
    40     }
    41     return 0;
    42 }
    View Code
  • 相关阅读:
    js浅拷贝和深拷贝
    使用slice和concat对数组的深拷贝和浅拷贝
    JS数组常用方法---8、concat方法
    JS数组常用方法---7、join方法
    js中将类数组转换为数组的几种方法
    JS 使用const声明常量的本质(很多人都有误解)
    JS中对象数组按照对象的某个属性进行排序
    vue源码分析参考---2、数据代理
    vue源码分析参考---1、准备工作
    ES6课程---5、形参默认值
  • 原文地址:https://www.cnblogs.com/zyxStar/p/4572513.html
Copyright © 2020-2023  润新知