[POJ 1840]Eqs[hash][枚举]

题目链接: [POJ 1840]Eqs[hash]

题意分析:

求表达式a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 中xi可能的组合个数。

解题思路:

本题x数据范围为100,五个一起枚举势必超时。观察发现,可以提前枚举x1和x2带入的值,将和存入,接着枚举x3、x4、x5的值进行比较即可。

即表达式a1x13+ a2x23 = -(a3x33+ a4x43+ a5x53)

使用数组存储和的个数,注意值可能是负数的情况,要将其变成正数存入。

本题枚举x1和x2可能范围为[-12500000,12500000]。所以数组开成25000000就行了,后期枚举排除掉超出范围的。

个人感受:

用map超时了= =,然后那么大的hash开成int会mle,要开成short或者char来记录。刚看到题就被表达式给震慑住了。。。。。

具体代码如下:

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#define lowbit(x) (x & (-x))
#define root 1, n, 1
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1  1
#define ll long long
#define pr(x) cout << #x << " = " << (x) << '\n';
using namespace std;

const int ADD = 12500000;
const int MAXN = 25000000 + 11;

int a[5];
short num[MAXN];

int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\apple\\Desktop\\in.txt", "r", stdin);
    #endif
    for (int i = 0; i < 5; ++i) scanf("%d", &a[i]);
    for (int i = -50; i <= 50; ++i) {
        int t = i * i * i;
        for (int j = -50; j <= 50; ++j) {
            if (i && j) {
                int sum = t * a[0] + j * j * j * a[1] + ADD;
                ++num[sum];
            }
        }
    }

    int ans = 0;
    for (int i = -50; i <= 50; ++i) {
        int t = i * i * i;
        for (int j = -50; j <= 50; ++j) {
            int t2 = j * j * j;
            for (int k = -50; k <= 50; ++k) {
                if (i && j && k) {
                    int sum = t * a[2] + t2 * a[3] + k * k * k * a[4] + ADD;
                    if (sum > 25000000 || sum < 0) continue;
                    ans += num[sum];
                }
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值