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\n", cnt);
40     }
41     return 0;
42 }

但是发现有人低内存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\n", cnt);
40     }
41     return 0;
42 }

时间: 2024-12-24 15:41:26

swust oj 166--方程的解数(hash法)的相关文章

[Swust OJ 404]--最小代价树(动态规划)

题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 以下方法称为最小代价的字母树:给定一正整数序列,例如:4,1,2,3,在不改变数的位置的条件下把它们相加,并且用括号来标记每一次加法所得到的和. 例如:((4+1)+ (2+3))=((5)+(5))=10.除去原数不4,1,2,3之外,其余都为中间结果,如5,5,10,将中间结果相加

swust oj 1026--Egg pain&#39;s hzf

题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf is crazy about reading math recently,and he is thinking about a boring problem. Now there are n integers Arranged in a line.For each integer,he wants to know

SWUST OJ Euclid&#39;s Game(0099)

Euclid's Game(0099) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 1855 Accepted: 589 Description Starts with two unequal positive numbers (M,N and M>N) on the board. Two players move in turn. On each move, a player has to write on the boar

swust oj 649--NBA Finals(dp,后台略(hen)坑)

题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two teams, Lakers and Celtics, playing a series of NBA Finals until one of the teams wins n games. Assume that the probability of Lakers winning a game is

线段树 [SWUST OJ 764] 校门外的树 Plus Plus

校门外的树 Plus Plus(0764) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 214 Accepted: 15 Description 西南某科技大学的校门外长度为 L 的公路上有一排树,每两棵相邻的树之间的间隔都是 1 米.我们可以把马路看成一个数轴,马路的一端在数轴 1 的位置,另一端在 L 的位置:数轴上的每个整数点,即 1,2,……,L,都种有一棵树. 现在要将这排树的某一段涂成某种颜色,给定 N 组区间[ 

背包 [POJ 2184 SWUST OJ 145] Cow Exhibition

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9479   Accepted: 3653 Description "Fat and docile, big and dumb, they look so stupid, they aren't much  fun..."  - Cows with Guns by Dana Lyons The cows want to prove to

[Swust OJ 137]--波浪数(hash+波浪数构造)

题目链接:http://acm.swust.edu.cn/problem/137/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 波浪数是在一对数字之间交替转换的数,如1212121,双重波浪数则是指在两种进制下都是波浪数的数,如十进制数191919是一个十进制下的波浪数,它对应的十一进制数121212也是一个波浪数,所以十进制数191919是一个双重波浪数. 类似的可以定义三重波浪数,三重波浪数在三种不同的进制中都是波浪数,甚

方程的解数(codevs_1735)——hash

这题挺简单的吧,其实只需要一步--就可以把暴力搜索的m^6降到m^3--那就是--请看代码 #include<iostream> #include<cstdio> #include<cstring> using namespace std; inline int read(){ int t=1,num=0;char c=getchar(); while(c>'9'||c<'0'){if(c=='-')t=-1;c=getchar();} while(c>

swust oj 1126--神奇的矩阵(BFS,预处理,打表)

题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈特13成功解决了填数矩阵问题.不知道他这一周又从哪儿弄来一神奇的矩阵,于是逃课潜心研究了一周,终于发现了其中的奥秘:该矩阵有2行.4列,即8个小方块,每个小方块上有一个数字,即:1 2 3 45 6 7 8对于这个神奇的矩阵,有3种变换方式,具体如下:变换A:上下两行数字互换,如上图可变为:5 6