poj 1840 Eqs
2016-08-11 19:40
281 查看
Eqs
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.
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
Sample Output
Source
Romania OI 2002
题意是
给出一个5元3次方程,输入其5个系数,求它的解的个数
思路:
a1*x1^3+a2*x2^3=-(a3*x3^3+a4*x4^3+a5*x5^3),所以只需判断左右两边相等即可。
这里我用了一种方法就是把所有的负数都给pass掉,然后在总数上*2即可,为什么呢?
我可以给大家举个例子
当a1*x1^3+a2*x2^3+a3*x3^3+a4*x4^3+a5*x5^3 = 1与
a1*x1^3+a2*x2^3+a3*x3^3+a4*x4^3+a5*x5^3
= -1
是否解的个数相同??
很容易想就是相同,,,因为只是一个“-”的关系吧
然后你在想
a1*x1^3+a2*x2^3+a3*x3^3+a4*x4^3 = a5*x5^3与
a1*x1^3+a2*x2^3+a3*x3^3+a4*x4^3
= -a5*x5^3
解的个数是否相同
当然也相同,因为他们两两之间没有关系吧!可以把等号右边看成是一个常量
与之相同的就是a1*x1^3+a2*x2^3=-(a3*x3^3+a4*x4^3+a5*x5^3)与
a1*x1^3+a2*x2^3=
a3*x3^3+a4*x4^3+a5*x5^3也相同吧。。。。。。
故而我们可以找他们相同的个数就好了,,,
我用的方法是找出所有两边是正数的解,当然了两边是负数的解和正数一样了,故而要*2,是吧!!
代码菜鸟,如有错误,请多包涵!!!!
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 15441 | Accepted: 7584 |
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.
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
题意是
给出一个5元3次方程,输入其5个系数,求它的解的个数
思路:
a1*x1^3+a2*x2^3=-(a3*x3^3+a4*x4^3+a5*x5^3),所以只需判断左右两边相等即可。
这里我用了一种方法就是把所有的负数都给pass掉,然后在总数上*2即可,为什么呢?
我可以给大家举个例子
当a1*x1^3+a2*x2^3+a3*x3^3+a4*x4^3+a5*x5^3 = 1与
a1*x1^3+a2*x2^3+a3*x3^3+a4*x4^3+a5*x5^3
= -1
是否解的个数相同??
很容易想就是相同,,,因为只是一个“-”的关系吧
然后你在想
a1*x1^3+a2*x2^3+a3*x3^3+a4*x4^3 = a5*x5^3与
a1*x1^3+a2*x2^3+a3*x3^3+a4*x4^3
= -a5*x5^3
解的个数是否相同
当然也相同,因为他们两两之间没有关系吧!可以把等号右边看成是一个常量
与之相同的就是a1*x1^3+a2*x2^3=-(a3*x3^3+a4*x4^3+a5*x5^3)与
a1*x1^3+a2*x2^3=
a3*x3^3+a4*x4^3+a5*x5^3也相同吧。。。。。。
故而我们可以找他们相同的个数就好了,,,
我用的方法是找出所有两边是正数的解,当然了两边是负数的解和正数一样了,故而要*2,是吧!!
#include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <vector> #define MAX 14997 using namespace std; int x1, x2, x3, x4, x5; vector<int> ha[15000]; int hash ( int x ) { return x%MAX; } void init () { int i; for ( i = 0;i < 15000; i++ ) { ha[i].clear(); } } int main() { int i, j, k; while ( ~scanf ( "%d %d %d %d %d", &x1, &x2, &x3, &x4, &x5 ) ) { /*Initialization data 初始化数据*/ init(); for ( i = -50; i <= 50 ; i++ ) { if( i == 0 ) continue; for ( j = -50; j <= 50 ; j++ ) { if ( j == 0 ) continue; for ( k = -50; k <= 50 ; k++ ) { if( k == 0 ) continue; int tem = i*i*i*x1+j*j*j*x2+k*k*k*x3; /* hash */ int pos = hash ( tem ); if ( tem < 0 ) { continue; } else { /*Storage of data 存储数据*/ ha[pos].push_back(tem); } } } } int sum = 0; for ( i = -50; i <= 50 ; i++ ) { if( i == 0 ) continue; for ( j = -50; j <= 50 ; j++ ) { if ( j == 0 ) continue; int tem = i*i*i*x4+j*j*j*x5; int pos = hash ( tem ); if ( tem < 0 ) { continue; } else { for ( k = 0; k < ha[pos].size(); k++ ) { if( ha[pos][k] == tem ) { sum++; } } } } } printf ( "%d\n", sum*2 ); } }
代码菜鸟,如有错误,请多包涵!!!!
相关文章推荐
- STL的应用 POJ 1840 Eqs 题解
- poj1840——Eqs
- POJ 1840 Eqs
- poj 1840 Eqs
- POJ 1840Eqs(hash)
- POJ-1840-Eqs
- [POJ 1840]Eqs[hash][枚举]
- STL的应用 POJ 1840 Eqs 题解
- POJ 1840 Eqs
- Eqs POJ - 1840
- poj 1840 Eqs(二分)
- POJ-1840-Eqs【哈希】
- POJ 1840 Eqs
- POJ1840--------Eqs
- POJ1840: Eqs(hash问题)
- poj 1840 Eqs 二分
- POJ 1840 Eqs(简单hash入门)
- POJ 1840 Eqs 暴力哈希
- POJ 1840 Eqs (hash)
- POJ 1840 Eqs