hdu5391-Zball in Tina Town-威尔逊定理(假证明)

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 11 time as large as its original size. On the second day,it will become 22 times as large as the size on the first day. On the n-th day,it will become nn 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. 

InputThe first line of input contains an integer TT, representing the number of cases.

The following TT lines, each line contains an integer nn, according to the description. 
T105,2n109T≤105,2≤n≤109 
OutputFor each test case, output an integer representing the answer.Sample Input

2
3
10

Sample Output

2
0

翻译:求(n-1)!%n
前置技能:威尔逊定理
威尔逊定理概念:当且仅当p为素数,(p-1)! ≡ -1 (mod p) → (p-1)!+1 = 0 (mod p)
网上关于定理的证明,什么缩系,死都看不懂,只好记住公式,举举例子说服自己这定理是真的。
1.当p为合数时,(p-1)! %p = 0。假设p=a*b,(p-1)! = 1*2*3*4*...*(p-1),其中有两个数是a和b,则(p-1)!%(a*b)=0;
多出一个1的时候,没办法被p整除。
2.当p为素数时,假设p=7,(p-1) != 1*2*3*4*5*6,关于大家所说的2到p-2这些数两两配对,2和4配对,8%7=1;3和5配对,
15%7=1;配对后模p结果为1,最后一个数模p结果为p-1由同余定理可知再补一个1就可以被p整除
再举例p=11,(p-1)! = 1*2*3*4*5*6*7*8*9*10,两两配对,2*6%11=1; 3*4%11=1; 5*9%11=1; 7*8%11=1; 1*10%11=10;
显然10!再加1就可以被11整除。
3.特例:p=4时,3! = 1*2*3 = 6; 6%4=2, 虽然p是合数,但(p-1)%p !=0

 1 #include <iostream>
 2 #include<stdio.h>
 3 #include <algorithm>
 4 #include<string.h>
 5 #include<cstring>
 6 #include<math.h>
 7 #define inf 0x3f3f3f3f
 8 #define ll long long
 9 using namespace std;
10 
11 bool flag(int x)
12 {
13     int q=sqrt(x);
14     for(int i=2;i<=q;i++)
15     {
16         if(x%i==0)
17             return false;
18     }
19     return true;
20 }
21 
22 int main()///hdu5391,威尔逊定理
23 {
24     int t;
25     int n;
26     scanf("%d",&t);
27     while(t--)
28     {
29         scanf("%d",&n);
30         if(n==4)
31             printf("2\n");
32         else if(flag(n))
33             printf("%d\n",n-1);
34         else printf("0\n");
35     }
36     return 0;
37 }

 

 
posted @ 2019-02-12 17:26  守林鸟  阅读(172)  评论(0编辑  收藏  举报