Skip to content

Instantly share code, notes, and snippets.

@MinecraftFuns
Created October 8, 2020 06:29
Show Gist options
  • Save MinecraftFuns/87c69bba4d17bded05c3a6dd62707598 to your computer and use it in GitHub Desktop.
Save MinecraftFuns/87c69bba4d17bded05c3a6dd62707598 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#define watch(x) std::cout << "$ LINE @" << __LINE__ << " FUNC @" << __FUNCTION__ << "\n$ " \
<< (#x) << ": " << x << std::endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
template <typename Tp>
inline void read(Tp &x)
{
static char c;
static bool neg;
x = 0, c = getchar(), neg = false;
for (; !isdigit(c); c = getchar())
{
if (c == '-')
{
neg = true;
}
}
for (; isdigit(c); c = getchar())
{
x = x * 10 + c - '0';
}
if (neg)
{
x = -x;
}
}
const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
int n, k;
int a[N];
unordered_map<int, int> front, back;
inline void add(unordered_map<int, int> &mp, int x)
{
++mp[x];
}
inline void del(unordered_map<int, int> &mp, int x)
{
auto iter = mp.find(x);
if (iter->second == 1)
{
mp.erase(iter);
}
else
{
--iter->second;
}
}
int main()
{
read(n), read(k);
for (int i = 1; i <= n; ++i)
{
read(a[i]);
}
add(front, a[1]);
for (int i = 3; i <= n; ++i)
{
add(back, a[i]);
}
ll ans = 0;
ll fx, bx;
for (int i = 2; i <= n - 1; ++i)
{
if (a[i] % k == 0)
{
fx = (ll)a[i] / k, bx = (ll)a[i] * k;
if (bx < (ll)INF)
{
auto fi = front.find((int)fx), bi = back.find((int)bx);
if (fi != front.end() && bi != back.end())
{
ans += (ll)fi->second * bi->second;
}
}
}
add(front, a[i]);
del(back, a[i + 1]);
}
printf("%lld\n", ans);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment