---
id: 2352
lang: en
translation_of: index.md
source_sha256: 3453ed428a6ed81da086b19dc39c718a505f13406650304c04f695643e0eb65a
title: "P5330: [SNOI2019] Number Theory — Solution"
description: "Revisiting a problem from a year ago: mark B on the cycles modulo Q, take prefix sums, then count the full cycles plus the two ends."
tags: []
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "这个元素 $T$ 经历了多少次（$cnt$）循环 is worded loosely in the original; rendered literally as “within $T$, how many cycles ($cnt$) this element goes through”."
---

## Main Idea

Going back over a problem I did a year ago, it really does feel easy now.

Consider writing it as $d' \in A, d = d' + xP$, then count how many $x$ make $d$ land in $B$. Brute force just enumerates directly. To optimize the brute force, we notice that there must be a cycle here, of length $\text{lcm}(P, Q)$, which allows a slight optimization.

Let's think of this modulo $Q$ as a circular strip, then, for starting position $start_pos$, mark the $d \in B$ on this paper strip and take prefix sums. To make it easy to tell the order in which prefix sums are added and subtracted, we also need to mark ranks.

When computing, for $A_i$, we need to know, within $T$, how many cycles ($cnt$) this element goes through, and then add in the total of the complete cycles. Once that's done, we compute the beginning and the end. The starting position is of course $A_i \bmod Q$, and the end is $(A_i + cnt * P \bmod loop) \bmod Q$. Then just do a bit of prefix-sum work and you're done.

<!--more-->

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

using namespace std;

const int MAX_N = 1e6 + 200;

typedef long long ll;

int P, Q, n, m, A[MAX_N], B[MAX_N], prefix[MAX_N], rnk[MAX_N], tot[MAX_N], key[MAX_N];
bool vis[MAX_N];
ll T;

int main()
{
    scanf("%d%d%d%d%lld", &P, &Q, &n, &m, &T);
    int len = Q / __gcd(P, Q);
    for (int i = 1; i <= n; i++)
        scanf("%d", &A[i]);
    for (int i = 1; i <= m; i++)
        scanf("%d", &B[i]), prefix[B[i]] = 1, key[B[i]] = 1;
    for (int start_pos = 0; start_pos < Q; start_pos++)
    {
        if (vis[start_pos])
            continue;
        int last_pos = start_pos;
        rnk[last_pos] = 1, vis[start_pos] = true;
        for (int curt = (start_pos + P) % Q; curt != start_pos; last_pos = curt, curt = (curt + P) % Q)
            vis[curt] = true, rnk[curt] = rnk[last_pos] + 1, prefix[curt] += prefix[last_pos];
        tot[start_pos] = prefix[last_pos];
        for (int curt = (start_pos + P) % Q; curt != start_pos; curt = (curt + P) % Q)
            tot[curt] = prefix[last_pos];
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (A[i] >= T)
            continue;
        ll loop_cnt = (T - A[i] - 1) / P;
        ll lft = A[i] % Q, rig = (A[i] + loop_cnt % len * P) % Q;
        ans += loop_cnt / len * tot[lft];
        if (rnk[rig] >= rnk[lft])
            ans += prefix[rig] - prefix[lft] + key[lft];
        else
            ans += tot[lft] - (prefix[lft] - prefix[rig] - key[lft]);
    }
    printf("%lld\n", ans);
    return 0;
}
```
