# UVA1434 YAPTCHA 题解
Immortal_Codex
·
·
题解
博客食用可能更佳
https://blog.csdn.net/yanghb916/article/details/160946315
老师才讲了这道题,就趁热打铁写篇题解。
:::info[零、前置知识]
威尔逊定理:
若 p 是一个质数,那么必定有:
(p-1)! \equiv -1 \pmod{p}
:::
::::info[一、思考过程]{open}
S_n = \sum_{k=1}^n \left\lfloor(\frac{(3k+6)! + 1}{3k + 7} - \left\lfloor \frac{(3k+6)!}{3k + 7} \right\rfloor \right\rfloor)
这道题给了一个看起来很难的式子,让我们求它的值。
我们再仔细观察一下,容易发现 3k + 6 = (3k + 7) - 1,那么我们不妨设 3k + 7 = p,则 3k + 6 = p - 1,则原式可转化为:
S_n = \sum_{k=1}^n \left\lfloor\frac{(p - 1)! + 1}{p} - \left\lfloor \frac{(p - 1)!}{p} \right\rfloor \right\rfloor)
我们就可以用分类讨论:
:::info[1. p 为质数]
容易发现,p 为质数是满足威尔逊定理:
(p-1)! \equiv -1 \pmod{p}
即:
(p - 1)! + 1 = q \times p(q \in \mathbb{Z})\\
(p - 1)! = q \times p-1(q \in \mathbb{Z})
那么:
\because q - 1 < q - \frac{1}{p} < q\\
\begin{align}
\frac{(p - 1)! + 1}{p} - \frac{(p - 1)!}{p}&= \frac{q \times p}{p} - \frac{q \times p - 1}{p} \nonumber\\
&= q - (q - \frac{1}{p}) \nonumber\\
\therefore\left\lfloor\frac{(p - 1)! + 1}{p} - \left\lfloor \frac{(p - 1)!}{p} \right\rfloor \right\rfloor)
&= q - (q - 1) \nonumber\\
&= 1 \nonumber
\end{align}\\
证明 p \mid (p - 1)!,其中 p \ge 10:
令 p = a \times b,(1 < a <= b)。
$(p-1)! = 1\times2\times3\times\dots\times a\times\dots\times b\times\dots\times (p - 1)
显然:$(a\times b) \mid(p-1)!$。
$$
\therefore a > 2\\
\therefore a \times b = a ^ 2 > 2 \times a\\
\text{又}\because k > 4\\
\therefore (p-1)!=1\times2\times\dots\times a\times\dots\times (2 \times a) \times\dots\times (p - 1)
$$
同样有 $(a\times b) \mid(p-1)!$。
综上:如果 $p$ 为合数,则 $p \mid (p - 1)!$,那么:
$$
\begin{align}
\frac{(p - 1)! + 1}{p} - \frac{(p - 1)!}{p}&= \frac{q \times p + 1}{p} - \frac{q \times p}{p} \nonumber\\
&= (q + \frac{1}{p}) - q \nonumber
\end{align}\\
\therefore q < q + \frac{1}{p} < q + 1\\
\therefore \frac{(p - 1)! + 1}{p} - \frac{(p - 1)!}{p} = q - q = 0\\
$$
答案就加零。
:::
**综上所述:**
- 当 $p$ 为质数时答案加 $1$;
- 当 $p$ 为合数时答案加 $0$。
::::
:::info[二、本题做法]
挨个遍历,判断 $3k + 7$ 为质数还是合数:
- 当 $3k + 7$ 为质数时答案加 $1$;
- 当 $3k + 7$ 为合数时答案加 $0$。
问题转化为有多少个 $k\in[1,n]$ 满足 $3k + 7$ 是质数。
~参考代码就不给了。~
:::
完结,撒花!
⎛⎝≥⏝⏝≤⎛⎝