---
id: 1790
lang: en
translation_of: index.md
source_sha256: 0a43b1eb9e47845be6adc3f7b0443448d5575f401ead9727d309caa279521d37
title: "Codeforces 1247D: Power Products — Solution"
description: "Reduce each number's prime exponents mod k, hash both the result and its complement to a perfect k-th power, and count matches in a hash map."
tags: ["OI","Hacky Heuristics","Number Theory","Prime Factorization"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## Main Idea

It really is just <span class="slang" data-zh="您🐎" data-gloss="nín mǎ, the polite 您 “you” plus a horse emoji (马 mǎ, sounding like 妈 mā “mom”): a disguised 你妈 “your mom”. A mock-polite curse, here an intensifier like “goddamn”.">goddamn</span> <span class="slang" data-zh="乱搞" data-gloss="luàngǎo, “random messing”. Unproven heuristic or randomized tricks that happen to pass.">hacky heuristics</span>.

Let us consider under what circumstances a number can be called $x^k$: naturally, when the exponents of all its prime factors are multiples of $k$, that is, $b_i \equiv 0 \pmod k$. So we can factorize a number and write it as powers of its prime factors, except that the exponents are taken mod $k$. Then we hash it and put it into `unordered_map`. Make two hashes: one of the number itself, and one of what it takes to make it a complete $k$-th power.

The annoying part, though, is that if we go through all the primes, it will time out. So at this point we number all the primes, then work out the index differences between primes and multiply by the corresponding power of the hash.

<!--more-->

## Code

```cpp
// D.cpp
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;

using namespace std;

const int MAX_N = 1e5 + 200;
const ll UPPER = 1e10, bitnum = 673;

unordered_map<ull, int> ump;
int n, k, ai[MAX_N], primes[MAX_N], tot, rg, one_siz, idx[MAX_N];
ull pownum[MAX_N];
bool vis[MAX_N];

int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]), rg = max(rg, ai[i]);
    for (int i = 2; i <= rg; i++)
    {
        if (!vis[i])
            primes[++tot] = i, idx[i] = tot;
        for (int j = 1; j <= tot && 1LL * primes[j] * i <= rg; j++)
        {
            vis[i * primes[j]] = true;
            if (i % primes[j] == 0)
                break;
        }
    }
    for (int i = pownum[0] = 1; i <= tot; i++)
        pownum[i] = pownum[i - 1] * bitnum;
    ll ans = 0;
    for (int i = 1; i <= n; i++)
    {
        ull vec = 0, rev = 0;
        int tmp = ai[i], last_idx = 0;
        for (int j = 2; 1LL * j * j <= tmp; j++)
            if (tmp % j == 0)
            {
                int cnt = 0;
                while (tmp % j == 0)
                    tmp /= j, cnt++;
                vec = vec * pownum[idx[j] - last_idx] + cnt % k;
                rev = rev * pownum[idx[j] - last_idx] + ((k - (cnt % k)) % k);
                last_idx = idx[j];
            }
        if (tmp != 1)
        {
            vec = vec * pownum[idx[tmp] - last_idx] + 1 % k;
            rev = rev * pownum[idx[tmp] - last_idx] + ((k - 1) % k);
            last_idx = idx[tmp];
        }
        vec = vec * pownum[tot - last_idx], rev = rev * pownum[tot - last_idx];
        // cout << vec << " " << rev << endl;
        ans += ump[rev], ump[vec]++;
    }
    printf("%lld", ans);
    return 0;
}
```
