威尔逊定理例题汇总
hdu 5391 Zball in Tina Town
Problem Description
Tina Town is a friendly place. People there care about each other.
Tina has a ball called zball. Zball is magic. It grows larger every day. On the first day, it becomes 1 time as large as its original size. On the second day,it will become 2 times as large as the size on the first day. On the n-th day,it will become n times as large as the size on the (n-1)-th day. Tina want to know its size on the (n-1)-th day modulo n.
Input
The first line of input contains an integer T, representing the number of cases.
The following T lines, each line contains an integer n, according to the description.
$ T \leq 10^5,2 \leq n \leq 10^9 $
Output
For each test case, output an integer representing the answer.
Sample Input
1 | 2 |
Sample Output
1 | 2 |
思路:
求 $ (n-1)! \; mod \; n $。注意:特判 n = 4 时,$ ans = 2 $。
AC代码:
1 |
|
hdu 2973 YAPTCHA
Problem Description
The math department has been having problems lately. Due to immense amount of unsolicited automated programs which were crawling across their pages, they decided to put Yet-Another-Public-Turing-Test-to-Tell-Computers-and-Humans-Apart on their webpages. In short, to get access to their scientific papers, one have to prove yourself eligible and worthy, i.e. solve a mathematic riddle.
However, the test turned out difficult for some math PhD students and even for some professors. Therefore, the math department wants to write a helper program which solves this task (it is not irrational, as they are going to make money on selling the program).
The task that is presented to anyone visiting the start page of the math department is as follows: given a natural n, computewhere $ [x] $ denotes the largest integer not greater than x.
Input
The first line contains the number of queries t $ (t \leq 10^6) $. Each query consist of one natural number n $ (1 \leq n \leq 10^6) $.
Output
For each n given in the input output the value of $ S_n $.
Sample Input
1 | 13 |
Sample Output
1 | 0 |
思路:
威尔逊定理:$ (p-1)! \equiv -1\;(mod\;p) \Leftrightarrow (p-1)!+1 \equiv 0 \;(mod\;p) $。
①当 $ 3k + 7 $ 是素数时,$ (3k+7)|[(3k+6)!+1] $,令 $ m=\frac{(3k+6)!+1}{3k+7} $,
则 $ \left [ \frac{(3k+6)!}{3k+7} \right ] = \left [ \frac{m(3k+7)-1}{3k+7} \right ] = \left [ m-\frac{1}{3k+7} \right ] = m-1 $,
此时,$ T_k = \left [ m - (m-1) \right ] =1 $;
②当 $ 3k + 7 (\ge10)$ 是合数时,由威尔逊定理的相关证明可知当 $ p > 4 $ 且p为合数时,$ (p-1)! \equiv 0 \;(mod \; p) $,令 $ m=\frac{(3k+6)!}{3k+7} $,则 $ \frac{(3k+6)!+1}{3k+7} = \frac{m(3k+7)+1}{3k+7} = m+\frac{1}{3k+7} $,
此时,$ T_k = \left [ m+\frac{1}{3k+7}-m \right ] = \left [ \frac{1}{3k+7} \right ] = 0 $。n最大只有 $ 10^6 $,预处理打表即可。
AC代码:
1 |
|
- 本文链接: http://blog.wzomg.cn/posts/54c860b1.html
- 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!