QOJ.ac

QOJ

Limite de temps : 1.0 s Limite de mémoire : 512 MB Points totaux : 100 Hackable ✓

#12329. 巨大的字符串

Statistiques

考虑大小为 $k$ 的字母表 $\Sigma = \{s_0, \dots, s_{k-1}\}$。定义字符串序列 $\{T_i\}$ 如下:

  • $T_0 = s_0$;
  • $T_i = T_{i-1} s_{i \bmod k}$,对于所有整数 $i \ge 1$。

令 $S = T_0 T_1 T_2 \dots$ 为一个无限字符串,它是所有字符串 $\{T_i\}$ 按索引升序连接而成的。例如,若 $k = 3$,$s_0 = \text{“a”}$,$s_1 = \text{“b”}$,$s_2 = \text{“c”}$,则 $T_0 = \text{“a”}$,$T_1 = \text{“ab”}$,$T_2 = \text{“abc”}$,$T_3 = \text{“abca”}$,$T_7 = \text{“abcabcab”}$(依此类推),且 $S = \text{“aababcabcaabcab}\dots\text{”}$。

用 $S_n$ 表示字符串 $S$ 的长度为 $n$ 的前缀。给定数字 $n$ 和 $k$,计算字符串 $S_n$ 中不同非空子串的数量,其中 $|\Sigma| = k$。

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 10^5$),表示测试用例的数量。

接下来 $T$ 行,第 $i$ 行包含两个整数 $n_i$ 和 $k_i$ ($1 \le n_i \le 10^9, 1 \le k_i \le 10^9$),由空格分隔,分别表示第 $i$ 个测试用例中字符串 $S_{n_i}$ 的长度和字母表 $\Sigma$ 的大小。

输出格式

输出 $T$ 行。第 $i$ 行应包含一个整数,表示第 $i$ 个测试用例的答案。

样例

输入 1

7
1 3
2 3
3 3
4 3
5 3
6 3
7 3

输出 1

1
2
5
8
11
17
23

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#534Editorial Open集训队作业 解题报告 by 卢毅奇Qingyu2026-01-02 21:50:32 Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.