Skip to content

Commit 1cab82f

Browse files
committed
1840
1 parent 62646b1 commit 1cab82f

File tree

5 files changed

+143
-3
lines changed

5 files changed

+143
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
|| [POJ-1016](/reports/POJ1016-Numbers%20That%20Count) [POJ-1035](/reports/POJ1035-Spell%20checker) [POJ-3080](/reports/POJ3080-Blue%20Jeans) [POJ-1936](/reports/POJ1936-All%20in%20All) |
4949
| 排序(快排、归并排、堆排) | [POJ-1007](/reports/POJ1007-DNA%20Sorting) [POJ-2388](/POJ2388-Who's%20in%20the%20Middle) [POJ-1804](/reports/POJ1804-Brainman) [POJ-2299](/reports/POJ2299-Ultra-QuickSort) |
5050
| 并查集 | - |
51-
| 高效查找法<br/>(数的Hash、串的Hash、二分查找) | [POJ-1002](/reports/POJ1002-487-3279) [POJ-3349](/reports/POJ3349-Snowflake%20Snow%20Snowflakes) [POJ-3274](http://exp-blog.com/2018/06/20/pid-746/) [POJ-1840](http://exp-blog.com/2018/06/20/pid-756/) [POJ-2002](http://exp-blog.com/2018/06/20/pid-759/) [POJ-3432](http://exp-blog.com/2018/06/20/pid-764/) [POJ-2503](http://exp-blog.com/2018/06/20/pid-767/) |
51+
| 高效查找法<br/>(数的Hash、串的Hash、二分查找) | [POJ-1002](/reports/POJ1002-487-3279) [POJ-3349](/reports/POJ3349-Snowflake%20Snow%20Snowflakes) [POJ-3274](/reports/POJ3274-Gold%20Balanced%20Lineup) [POJ-1840](http://exp-blog.com/2018/06/20/pid-756/) [POJ-2002](http://exp-blog.com/2018/06/20/pid-759/) [POJ-3432](http://exp-blog.com/2018/06/20/pid-764/) [POJ-2503](http://exp-blog.com/2018/06/20/pid-767/) |
5252
| 哈夫曼树、优先队列 | [POJ-3253](http://exp-blog.com/2018/06/20/pid-771/) |
5353
|| - |
5454
| trie树(静态建树、动态建树) | [POJ-2513](http://exp-blog.com/2018/06/20/pid-774/) |

reports/POJ1840-Eqs/README.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
## [[POJ](http://poj.org/)] [[INDEX](https://github.com/lyy289065406/POJ-Solving-Reports)] [1840] [[Eqs](http://poj.org/problem?id=1840)]
2+
3+
> [Time: 5000MS] [Memory: 65536K] [难度: 初级] [分类: 高效查找法]
4+
5+
------
6+
7+
## 问题描述
8+
9+
给出一个5元3次方程,输入其5个系数,求它的解的个数
10+
11+
其中系数 `ai∈[-50,50]` 自变量 `xi∈[-50,0)∪(0,50]`
12+
13+
14+
> **注意** : 若 `x1=a, x2=b, x3=c, x4=d, x5=e` 时,与 `x1=b, x2=a, x3=c, x4=d, x5=e` 代入方程后都得到值0,那么他们视为不同的解。
15+
16+
17+
## 解题思路
18+
19+
直观的思路:暴力枚举,O(n^5)
20+
21+
题目Time Limit=5000ms,1ms大约可以执行1000条语句,那么5000ms最多执行500W次
22+
23+
每个变量都有100种可能值,那么暴力枚举,5层循环,就是要执行100^5=100E次,等着TLE吧。。。。
24+
25+
26+
27+
**要AC这题,就要对方程做一个变形**
28+
29+
![](/img/01.png)
30+
31+
32+
即先枚举x1和x2的组合,把所有出现过的 左值 记录打表,然后再枚举x3 x4 x5的组合得到的 右值,如果某个右值等于已经出现的左值,那么我们就得到了一个解
33+
34+
时间复杂度从 O(n^5)降低到 O(n^2+n^3),大约执行100W次
35+
36+
37+
------
38+
39+
40+
我们先定义一个映射数组 `hash[]`,初始化为0
41+
42+
对于方程左边,当 `x1=m`, `x2=n` 时得到sum,则把用 `hash[]` 记录sum : `hash[sum]++`,表示sum这个值出现了1次。
43+
44+
**之所以是记录“次数”,而不是记录“是否已出现”**
45+
46+
是因为我们**不能保证**函数的映射为 **1对1 映射**,更多的是存在 **多对1映射**
47+
48+
例如当 `a1=a2` 时,`x1=m`, `x2=n` 我们得到了sum,但 `x1=n`, `x2=m`时我们也会得到sum,但是我们说这两个是不同的解,这就是 多对1 的情况了,如果单纯记录sum是否出现过,则会使得 解的个数 减少。
49+
50+
51+
52+
其次,为了使得 搜索sum是否出现 的操作为o(1),我们把sum作为下标,那么hash数组的上界就取决于a1 a2 x1 x2的组合,四个量的极端值均为50
53+
54+
因此上界为 `50*50^3+50*50^3=12500000`,由于sum也可能为负数,因此我们对 `hash[]` 的上界进行扩展,扩展到25000000,当 `sum<0` 时,我们令 `sum+=25000000` 存储到 `hash[]`
55+
56+
由于数组很大,必须使用全局定义。
57+
58+
同时由于数组很大,用int定义必然会MLE,因此要用char或者short定义数组,推荐short。
59+
60+
61+
## AC 源码
62+
63+
64+
```c
65+
//Memory Time
66+
//49188K 532MS
67+
68+
#include<iostream>
69+
using namespace std;
70+
71+
short hash[25000001]; //hash[sum]表示值等于sum的的解的个数(多对1映射)
72+
73+
int main(void)
74+
{
75+
int a1,a2,a3,a4,a5; //系数
76+
while(cin>>a1>>a2>>a3>>a4>>a5)
77+
{
78+
memset(hash,0,sizeof(hash));
79+
80+
for(int x1=-50;x1<=50;x1++)
81+
{
82+
if(!x1)
83+
continue;
84+
85+
for(int x2=-50;x2<=50;x2++)
86+
{
87+
if(!x2)
88+
continue;
89+
int sum=(a1*x1*x1*x1 + a2*x2*x2*x2)*(-1);
90+
if(sum<0)
91+
sum+=25000000;
92+
hash[sum]++;
93+
}
94+
}
95+
96+
int solution=0;
97+
98+
for(int x3=-50;x3<=50;x3++)
99+
{
100+
if(!x3)
101+
continue;
102+
for(int x4=-50;x4<=50;x4++)
103+
{
104+
if(!x4)
105+
continue;
106+
for(int x5=-50;x5<=50;x5++)
107+
{
108+
if(!x5)
109+
continue;
110+
int sum=a3*x3*x3*x3 + a4*x4*x4*x4 + a5*x5*x5*x5;
111+
if(sum<0)
112+
sum+=25000000;
113+
if(hash[sum])
114+
solution+=hash[sum];
115+
}
116+
}
117+
}
118+
119+
cout<<solution<<endl;
120+
}
121+
return 0;
122+
}
123+
```
124+
125+
------
126+
127+
## 版权声明
128+
129+
 [![Copyright (C) EXP,2016](https://img.shields.io/badge/Copyright%20(C)-EXP%202016-blue.svg)](http://exp-blog.com) [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
130+
131+
132+
- Site: [http://exp-blog.com](http://exp-blog.com)
133+
- Mail: <a href="mailto:[email protected]?subject=[EXP's Github]%20Your%20Question%20(请写下您的疑问)&amp;body=What%20can%20I%20help%20you?%20(需要我提供什么帮助吗?)">[email protected]</a>
134+
135+
136+
------

reports/POJ1840-Eqs/img/01.png

7.46 KB
Loading

reports/POJ1840-Eqs/testdata/.empty

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (C)
2+
# Author: EXP
3+
# Site : http://exp-blog.com
4+

reports/POJ3274-Gold Balanced Lineup/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ In the range from cow \#3 to cow \#6 (of size 4), each feature appears in exactl
160160
## 测试数据
161161

162162
- 来源(已失效):[USACO 2007 March Gold](http://www.cppblog.com/Felicia/archive/2007/12/29/39923.html)
163-
- 输入:[input](/reports/POJ3080-Blue%20Jeans/testdata/input.dat)
164-
- 输出:[output](/reports/POJ3080-Blue%20Jeans/testdata/output.dat)
163+
- 输入:[input](/reports/POJ3274-Gold%20Balanced%20Lineup/testdata/input.dat)
164+
- 输出:[output](/reports/POJ3274-Gold%20Balanced%20Lineup/testdata/output.dat)
165165

166166

167167

0 commit comments

Comments
 (0)