QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 256 MB Puntuación total: 100

#5620. 快乐快乐素数素数

Estadísticas

RILEY VASHTEE:[阅读显示屏上的内容] 找出序列中的下一个数字:

$313 \ 331 \ 367 \ \dots?$ 什么?

THE DOCTOR:$379$。

MARTHA JONES:什么?

THE DOCTOR:这是一个快乐素数序列——$379$。

MARTHA JONES:快乐什么?

THE DOCTOR:任何一个数字,当你取其各位数字的平方和并不断迭代,直到结果为 $1$ 时,这个数字就是快乐数。任何不满足这一条件的数字则不是。一个快乐素数既是快乐数又是素数。

THE DOCTOR:我不知道,谈论这些太简单了。难道他们现在不教娱乐数学了吗?

摘自《神秘博士》(Dr. Who),第 $42$ 集($2007$ 年)。

数字 $7$ 当然是素数。但它是快乐数吗?

$$7 \to 7^2 = 49$$ $$49 \to 4^2 + 9^2 = 97$$ $$97 \to 9^2 + 7^2 = 130$$ $$130 \to 1^2 + 3^2 = 10$$ $$10 \to 1^2 + 0^2 = 1$$

它是快乐数 $\odot$。正如所见,$7$ 是最小的快乐素数。请注意,就本题而言,$1$ 不是 素数。

对于本题,你需要编写一个程序来判断一个数字是否为快乐素数。

输入格式

输入的第一行包含一个整数 $P$ ($1 \le P \le 1000$),表示随后数据集的数量。每个数据集都应独立处理。

每个数据集包含一行输入。它包含数据集编号 $K$,后跟一个空格,再后跟快乐素数候选数 $m$ ($1 \le m \le 10000$)。

输出格式

对于每个数据集,输出一行。单行输出由数据集编号 $K$、一个空格、候选数 $m$、一个空格,以及 YES 或 NO 组成,表示 $m$ 是否为快乐素数。

样例

输入 1

4
1 1
2 7
3 383
4 1000

输出 1

1 1 NO
2 7 YES
3 383 YES
4 1000 NO

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.