POJ--Eqs

本文介绍了一种通过将问题拆分为两部分并利用乘法原理减少计算量的方法,以解决特定数学组合问题的高效算法实现。该算法适用于寻找满足特定等式的数字组合数量,通过对输入范围内的数字进行组合计算,并利用哈希表存储中间结果来加速查找过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目要求:给出五个数字x1, x2, x3, x4, x5求满足等式的x的值有多少种组合,其中x∈[-50, 0) ∪ (0, 50]

思路:首先考虑暴力的情况是五重for循环就是10亿的复杂度,一定会TLE,但是如果把他们分成两部分,第一部分求得x1,x2,x3的所有组合,然后求得x4,x5的所有组合,想对应的组合使用乘法就可以了。计算量:10000000刚刚好。

#include<vector>
#include<map>
#include<cstring>
#include<set>
#include<string>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<ctype.h>
#include<fstream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<stack>
#include<queue>
#include<ctime>
//#include<conio.h>
using namespace std;


const int INF_MAX=0x7FFFFFFF;
const int INF_MIN=-(1<<30);


const double eps=1e-10;
const double pi=acos(-1.0);


#define MP make_pair
#define PB push_back   //a.pb( )
#define chmin(a,b) ((a)<(b)?(a):(b))
#define chmax(a,b) ((a)>(b)?(a):(b))




template<class T> inline T gcd(T a,T b)//NOTES:gcd(
  {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<class T> inline T lcm(T a,T b)//NOTES:lcm(
  {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){
    while (!Q.empty()) Q.pop();}
inline int random(int l, int r){return rand() % (r - l + 1) + l;}


typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
int dir_4[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
int dir_8[8][2]={{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1},{1,0},{1,1}};
//下,左下,左,左上,上,右上,右,右下。


//******* WATER ****************************************************************


int add = 12500000;
short hash[25000000 + 10];


int main() {
    int a, b, c, d, e;
    while(~scanf("%d%d%d%d%d", &a, &b, &c, &d, &e)) {
        int ans = 0;
        memset(hash, 0, sizeof(hash));
        for(int i = -50; i <= 50; i++) {
            for(int j = -50; j <= 50; j++) {
                for(int k = -50; k <= 50; k++) {
                    if(i && j && k) {
                        int sum = i*i*i*a + j*j*j*b + k*k*k*c;
                        if(sum <= add && sum >= -add) {
                            //cout << sum << endl;
                            hash[sum + add]++;
                        }
                    }
                }
            }
        }
        //cout << "here" << endl;
        for(int i = -50; i <= 50; i++) {
            for(int j = -50; j <= 50; j++) {
                if(i && j) {
                    int sum = -(i*i*i*d + j*j*j*e);
                    ans += hash[sum + add];
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值