POJ 1840(哈希,数学,思维)
来源:互联网 发布:软件咨询服务购买合同 编辑:程序博客网 时间:2024/05/17 01:02
Eqs
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 14985 Accepted: 7353
Description
Consider equations having the following form:
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0
The coefficients are given integers from the interval [-50,50].
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}.
Determine how many solutions satisfy the given equation.
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0
The coefficients are given integers from the interval [-50,50].
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}.
Determine how many solutions satisfy the given equation.
Input
The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.
Output
The output will contain on the first line the number of the solutions for the given equation.
Sample Input
37 29 41 43 47
Sample Output
654
Source
Romania OI 2002
题意:给出系数a1-----a5,求有多少满足等式的整数解
题解:并不会呀,智障选手看到数学就懵蔽了,查了题解,原来是使用hash表做,ORZ,这么神奇
http://blog.csdn.net/lyy289065406/article/details/6647387
要AC这题,就要对方程做一个变形
即先枚举x1和x2的组合,把所有出现过的 左值 记录打表,然后再枚举x3 x4 x5的组合得到的 右值,如果某个右值等于已经出现的左值,那么我们就得到了一个解
时间复杂度从 O(n^5)降低到 O(n^2+n^3),大约执行100W次
我们先定义一个映射数组hash[],初始化为0
对于方程左边,当x1=m , x2= n时得到sum,则把用hash[]记录sum : hash[sum]++,表示sum这个值出现了1次
这里我觉得就不要开静态数组做hash了,直接 map更好,虽然效率不如hash表的O(1)查询,但是log(N)也是可以水过的,静态hash表比map快6倍。。。。。。,毕竟牺牲了大量空间
STL版:
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<string>#include<bitset>#include<utility>#include<functional>#include<iomanip>#include<sstream>#include<ctime>using namespace std;#define N int(1e5)#define inf int(0x3f3f3f3f)#define mod int(1e9+7)typedef long long LL;map<int, int>Hash;int pow(int x){return x*x*x;}int main(){#ifdef CDZSCfreopen("i.txt", "r", stdin);//freopen("o.txt","w",stdout);int _time_jc = clock();#endifint a[10];while (~scanf("%d%d%d%d%d", &a[1], &a[2], &a[3], &a[4], &a[5])){Hash.clear();for (int i = -50; i <= 50; i++){if (!i)continue;for (int j = -50; j <= 50; j++){if (!j)continue;int sum = (a[1] * pow(i) + a[2] * pow(j));Hash[sum]++;}}int ans = 0;for (int i = -50; i <= 50; i++){if (!i)continue;for (int j = -50; j <= 50; j++){if (!j)continue;for (int k = -50; k <= 50; k++){if (!k)continue;int sum = a[3] * pow(i) + a[4] * pow(j) + a[5] * pow(k);if(Hash.count(-sum))ans += Hash[-sum];}}}printf("%d\n", ans);}return 0;}
静态数组Hash表:
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<string>#include<bitset>#include<utility>#include<functional>#include<iomanip>#include<sstream>#include<ctime>using namespace std;#define N int(25e6)#define inf int(0x3f3f3f3f)#define mod int(1e9+7)typedef long long LL;short Hash[N];int pow(int x){return x*x*x;}int main(){#ifdef CDZSCfreopen("i.txt", "r", stdin);//freopen("o.txt","w",stdout);int _time_jc = clock();#endifint a[10];while (~scanf("%d%d%d%d%d", &a[1], &a[2], &a[3], &a[4], &a[5])){memset(Hash, 0, sizeof(Hash));for (int i = -50; i <= 50; i++){if (!i)continue;for (int j = -50; j <= 50; j++){if (!j)continue;int sum = (a[1] * pow(i) + a[2] * pow(j));if (sum < 0)sum += N;Hash[sum]++;}}int ans = 0;for (int i = -50; i <= 50; i++){if (!i)continue;for (int j = -50; j <= 50; j++){if (!j)continue;for (int k = -50; k <= 50; k++){if (!k)continue;int sum = a[3] * pow(i) + a[4] * pow(j) + a[5] * pow(k);sum = -sum;if (sum < 0)sum += N;ans += Hash[sum];}}}printf("%d\n", ans);}return 0;}
0 0
- POJ 1840(哈希,数学,思维)
- poj 1844 Sum ( 数学思维 )
- poj 1840 (数学)
- cf(思维,数学)
- poj 2719 Faulty Odometer (数学思维)
- cf(数学思维题)
- LightOJ1104Birthday Paradox(思维+数学)
- zzulioj 1898 (数学+思维)
- hdu6108(数学思维+因子)
- Friend-Graph(数学,思维)
- POJ 1840 Eqs(Hash+思维)
- [poj]1118 Lining Up [思维][数学][尺取]
- POJ 1840 数学
- POJ 1840 - Eqs(数学)
- POJ 3329 - 数学 + 哈希
- POJ数学(ZZ)
- POJ 2853 (数学)
- hdu 4104 Discount (思维 数学归纳法)
- elasticsearch JAVA客户端操作---搜索的过滤、分组高亮,elasticsearchjava
- 数学建模中的整数规划总结及姜启源第4章(1-3)的解析
- (LeetCode 18) 4Sum
- Android基础:手势识别:GestureDetector
- Android视频播放的开发资源
- POJ 1840(哈希,数学,思维)
- 蓝桥杯(Java) 入门训练 圆的面积
- AFNetWorking框架使用的“两个大坑”——修改请求方式和响应方式
- Mac OS X 中安装 brew
- L2-1. 紧急救援 题目详解
- android应用程序如何调用支付宝接口
- Hadoop最新版本2.7.0的部署-学习记录01
- iOS 中各种折线.曲线.图形绘制方法集锦(折线图.走势图.柱状图.动态曲线图.形状图.划线图)
- MVC模式