B - 宝石
对于三个数的乘积组合,可以先处理两个数的乘积,对于第 i 个宝石后面任
意两个宝石的乘积标记一下,然后对于第三个要用到的宝石可以枚举去找到,当
第 i 个宝石在后面能找到一个宝石整除且被标记过,那么答案加一, 因为要多次
查询乘积是否存在,可以使用 C++ 自带的 STL 中的 map 和 unordered_map
来实现,对于 unordered_map 的查询操作我们一般看作 O(1),在本题中时间
效率也是要高出 map。
时间复杂度: O ( n 2 ) O(n^2) O(n2)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1510;
int a[N];
int main()
{
unordered_set<LL> b;
int n;cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int res=0;
for(int i=n-1;i>=1;i--)
{
for(int j=i+1;j<=n;j++) b.insert(a[i+1]*a[j]);
for(int j=i+1;j<=n;j++)
{
if(!a[i] && b.count(0))
{
res++;
break;
}
if(!a[j]) continue;
if(a[i]%a[j]) continue;
if(b.count(a[i]/a[j]))
{
res++;
break;
}
}
}
cout<<res;
return 0;
}