【枚舉】【STL】 ABCDEF (ABCDEF.c/cpp/pas)

【题目描述】

LazyChild有n个在[-30000,30000]区间内的整数,他想知道有多少个六元组(a,b,c,d,e,f)满足:

(a × b + c) ÷ d – e = f

【输入文件】

第一行一个整数n。

第二行n个整数。

【输出文件】

一行一个整数,表示有多少个满足要求的六元组。

【样例输入】

2

2 3

【样例输出】

4

【数据规模和约定】

对于30%的数据,1 <= n <= 10

对于100%的数据,1 <= n <= 100


#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <map>
#include <iostream>
using namespace std;

int n;
int x[105];
map <long, long> cnt;
map <long, long>::iterator it1;
long long ans;

void init_file()
{
    freopen("ABCDEF.in", "r", stdin);
    freopen("ABCDEF.out", "w", stdout);
}

void read_data()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%d", x + i);
}

void work()
{
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            for(int k = 0; k < n; k++)
            {
                long tmp = x[i] * x[j] + x[k];
                it1 = cnt.find(tmp);
                if (it1 == cnt.end())
                    cnt.insert(make_pair(tmp, 1));
                else
                    it1 -> second ++;
            }
    for(int i = 0; i < n; i++)
    {
        if (x[i] == 0) continue;
        for(int j = 0; j < n; j++)
            for(int k = 0; k < n; k++)
            {
                long tmp = (x[j] + x[k]) * x[i];
                it1 = cnt.find(tmp);
                if (it1 != cnt.end())
                    ans += it1 -> second;
            }
    }
    cout << ans;
}

int main()
{
    init_file();
    read_data();
    work();
    return 0;
}

原題中要求所有(a × b + c) ÷ d – e = f的數目

相當於求出所有的a * b + c = (f + e) * d

但是需要注意 d不為0(0不為除數)

於是枚舉兩邊的值 用map存取

不要用map的數組形式(非常緩慢)

迭代器大大加快了速度

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值