Hey Everyone. This is my first post here
I am getting TLE in this problem https://www.spoj.com/problems/ABCDEF/
here is my code… I am using two unordered maps for storing the values of the two expressions and searching them at last.
#include <bits/stdc++.h>
using namespace std;
#define pp long long int
#define mod 1000000007
unordered_map<pp, pp> umap1;
unordered_map<pp, pp> umap2;
int main()
{
pp n; cin>>n;
pp arr[n+1];
for(pp i=0;i<n;i++) cin>>arr[i];
pp insert;
for(pp i=0;i<n;i++)
for(pp j=0;j<n;j++)
for(pp k=0;k<n;k++)
{
insert = (arr[i]*arr[j])+arr[k];
if(umap1.find(insert)==umap1.end()) umap1[insert] = 1;
else umap1[insert]++;
}
for(pp i=0;i<n;i++)
for(pp j=0;j<n;j++)
for(pp k=0;k<n;k++)
{
if(arr[k]!=0)
{
insert = ( arr[i] + arr[j] )* arr[k];
if(umap2.find(insert)==umap2.end()) umap2[insert] = 1;
else umap2[insert]++;
}
}
pp ans = 0;
for (auto x : umap1)
{
for(auto y: umap2)
{
if(x.first == y.first ) ans += (x.second*y.second);
}
}
cout<<ans<<"\n";
return 0;
}
Any help will be appreciated. Thanks in advance.