【题目描述】
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的數組形式(非常緩慢)
迭代器大大加快了速度