KiaKio 与平方数AI (CI翻译)
Kia and Kio spent the summer at the port of Mehragan, where $n$ lighthouses stand on the cliffs facing the dark sea.
Kia 和 Kio 在 Mehragan 港度过了夏天,那里有 $n$ 座灯塔,矗立在面朝漆黑大海的悬崖上。AI (CI翻译)
The lighthouses of Mehragan do not give light. Every night a number is written in fire on each of them, and the sailors read their way from those numbers.
Mehragan 的灯塔并不发光。每晚,每座灯塔上都会用火焰写出一个数字,水手们通过这些数字辨认航路。AI (CI翻译)
The law of the lighthouses is this: if a lighthouse shows $x$ tonight, then tomorrow night it shows the sum of the squares of the decimal digits of $x$.
灯塔遵循这样的规律:如果一座灯塔今晚显示的是 $x$,那么明晚它将显示 $x$ 的各个十进制数位的平方和。AI (CI翻译)
For example, a lighthouse showing $23$ will show $2^2+3^2=13$ tomorrow, then $1^2+3^2=10$, and then $1$.
例如,一座显示 $23$ 的灯塔,明晚将显示 $2^2+3^2=13$,之后是 $1^2+3^2=10$,再之后是 $1$。AI (CI翻译)
On night $0$ of the season, lighthouse $i$ shows the number $a_i$. From that night on, the law is applied once every night, forever.
在这个季节的第 $0$ 晚,灯塔 $i$ 显示数字 $a_i$。从那一晚开始,每晚都会应用一次上述规律,永远持续下去。AI (CI翻译)
Kio calls two lighthouses $i$ and $j$ in tune if there exists a night after which, forever, both of them show exactly the same number on every single night.
Kio 称两座灯塔 $i$ 和 $j$ 同步,当且仅当存在某一晚,使得从那一晚起,它们在之后的每一晚都显示完全相同的数字,永远如此。AI (CI翻译)
Kia asks: how many pairs $(i, j)$ with $i \lt j$ are in tune?
Kia 想知道:有多少对满足 $i \lt j$ 的灯塔 $(i, j)$ 是同步的?AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.
每个测试包含多组测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 1000$)。接下来是各组测试用例的描述。AI (CI翻译)
Each test case consists of two lines.
每组测试用例包含两行。AI (CI翻译)
The first line of each test case contains a single integer $n$ ($1 \le n \le 1000$) — the number of lighthouses.
每组测试用例的第一行包含一个整数 $n$($1 \le n \le 1000$)——灯塔的数量。AI (CI翻译)
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the number shown by each lighthouse on night $0$.
第二行包含 $n$ 个整数 $a_1, a_2, \ldots, a_n$($1 \le a_i \le 10^9$)——各座灯塔在第 $0$ 晚显示的数字。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.
保证所有测试用例的 $n$ 之和不超过 $1000$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, print a single integer — the number of pairs $(i, j)$ with $i \lt j$ such that lighthouses $i$ and $j$ are in tune.
对于每组测试用例,输出一个整数——满足 $i \lt j$ 且灯塔 $i$ 和 $j$ 同步的数对 $(i, j)$ 的数量。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case:
在第一组测试用例中:AI (CI翻译)
- Lighthouse
$1$starts at$7$:$7 \to 49 \to 97 \to 130 \to 10 \to 1$, and it stays at$1$forever. - Lighthouses
$2$and$4$both start at$4$, so they show the same number on every night. - Lighthouse
$3$starts at$16$and lighthouse$5$starts at$2$; each of them is at a different point of the cycle Kio found, and never matches anybody.
- 灯塔
$1$从$7$开始:$7 \to 49 \to 97 \to 130 \to 10 \to 1$,之后永远保持为$1$。 - 灯塔
$2$和$4$都从$4$开始,因此它们每晚显示的数字都相同。 - 灯塔
$3$从$16$开始,灯塔$5$从$2$开始;它们分别处于 Kio 发现的循环中的不同位置,且永远不会与任何其他灯塔显示相同的数字。
So the only pair in tune is $(2, 4)$, and the answer is $1$.
因此,唯一同步的灯塔对是 $(2, 4)$,答案为 $1$。AI (CI翻译)
In the second test case, every lighthouse sooner or later reaches $1$ and stays there, so all of them are pairwise in tune, which gives $6$ pairs.
在第二组测试用例中,每座灯塔迟早都会到达 $1$ 并保持不变,因此所有灯塔都两两同步,共有 $6$ 对。AI (CI翻译)
In the fourth test case, on night $1$, lighthouse $2$ shows $2^2+0^2 = 4$, and lighthouse $1$ shows $2^2 = 4$. From night $1$ on, they are identical forever. Lighthouse $3$ starts at $4$, so it is one night ahead of both of them and is in tune with neither.
在第四组测试用例中,第 $1$ 晚,灯塔 $2$ 显示 $2^2+0^2 = 4$,灯塔 $1$ 显示 $2^2 = 4$。从第 $1$ 晚开始,它们显示的数字将永远相同。灯塔 $3$ 从 $4$ 开始,因此它比另外两座灯塔都领先一晚,与它们中的任何一座都不同步。AI (CI翻译)
样例
4
5
7 4 16 4 2
4
1 7 10 100
3
4 16 37
3
2 20 4
1
6
0
1
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。