---
id: 2180
lang: en
translation_of: index.md
source_sha256: 17027518004e80220f69447ac1e9bab6489a6292c062ca01e91367b2394e3f77
title: "Universal OJ#450. [National Training Team Homework 2018] Repeater — Solution"
description: "For d = 1 the answer is k^n; for d = 2, a generating function and a binomial expansion via parity; for d = 3, a roots of unity filter and brute-force expansion."
tags: ["OI","Math","Generating Function","Roots of Unity Filter"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The problem name 复读机 (a tape-replay study device, also a nickname for someone who parrots others) has no official English title here; rendered literally as Repeater."
  - "变通奇偶性 rendered as “we can get around the parity condition”."
---

## Main Idea

First, for $d = 1$, the answer is just $k^n$.

For $d = 2$, it turns out we can actually just multiply generating functions directly: $A(x) = \sum_{i = 0}^\infty \frac{1}{i!} x^i [2 | i]$, and then the answer is $A^k(x)[x^n]$. We notice we can get around the parity condition, so $A(x) = \frac{e^x + e^{-x}}{2}$, and then we try a binomial expansion:

<!--more-->

$$
\begin{aligned} A^k(x) &= (\frac{e^x + e^{-x}}{2})^k = \frac{1}{2^k} (e^x + e^{-x})^k \\ &= \frac{1}{2^k} \sum_{i = 0}^k {k \choose i} e^{ix} e^{-(k - i)x} \\ &= \frac{1}{2^k} \sum_{i = 0}^k {k \choose i} e^{(2i - k)x} \end{aligned}
$$

Then the $n$-th term is $\frac{1}{2^k} \sum_{i = 0}^k {k \choose i} (2i - k)^n$. This one is still pretty easy to compute.

For $d = 3$, it's the same formula as before, but we find there's no way to do it with parity, so we can consider using the roots of unity filter:

$$
\begin{gathered} \frac{1}{d} \sum_{i = 0}^{d - 1} (\omega_d^k)^i = [d | k] \\ \begin{aligned} A(x) &= \sum_{i = 0}^\infty \frac{1}{i!} x^i \frac{1}{d} \sum_{j = 0}^{d - 1} (\omega_d^i)^j \\ &= \frac{1}{d} \sum_{i = 0}^{d - 1} \omega_d^i e^{\omega_d^1 x} \end{aligned} \end{gathered}
$$

Since $k$ is fairly small here, we can just consider expanding the binomials directly by brute force.

## Code

```cpp
// UOJ450.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 501000, mod = 19491001, g = 7;

int n, k, d, fac[MAX_N], fac_inv[MAX_N], inv[MAX_N], omega[3];

int fpow(int bas, int tim)
{
    int ret = 1;
    while (tim)
    {
        if (tim & 1)
            ret = 1LL * ret * bas % mod;
        bas = 1LL * bas * bas % mod;
        tim >>= 1;
    }
    return ret;
}

const int inv2 = fpow(2, mod - 2);

int binomial(int n_, int k_) { return 1LL * fac[n_] * fac_inv[k_] % mod * fac_inv[n_ - k_] % mod; }

int main()
{
    scanf("%d%d%d", &n, &k, &d);
    if (d == 1)
        printf("%d\n", fpow(k, n)), exit(0);
    inv[0] = inv[1] = 1;
    for (int i = 2; i <= k; i++)
        inv[i] = 1LL * inv[mod % i] * (mod - mod / i) % mod;
    for (int i = fac[0] = fac_inv[0] = 1; i <= k; i++)
        fac[i] = 1LL * fac[i - 1] * i % mod, fac_inv[i] = 1LL * fac_inv[i - 1] * inv[i] % mod;
    if (d == 2)
    {
        int ans = 0;
        for (int j = 0; j <= k; j++)
            ans = (1LL * binomial(k, j) * fpow((2LL * j + mod - k) % mod, n) % mod + ans) % mod;
        ans = 1LL * ans * fpow(inv2, k) % mod;
        printf("%d\n", ans);
    }
    else
    {
        int ans = 0;
        omega[0] = 1, omega[1] = fpow(g, (mod - 1) / 3), omega[2] = 1LL * omega[1] * omega[1] % mod;
        for (int i = 0; i <= k; i++)
            for (int j = 0; i + j <= k; j++)
            {
                int p = (0LL + (k - i - j) + 1LL * omega[1] * i % mod + 1LL * omega[2] * j % mod) % mod;
                ans = (0LL + ans + 1LL * binomial(k, i) * binomial(k - i, j) % mod * fpow(p, n) % mod) % mod;
            }
        printf("%lld\n", 1LL * ans * fpow(fpow(d, k), mod - 2) % mod);
    }
    return 0;
}
```
