转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Zball in Tina Town
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 219 Accepted Submission(s): 144
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.
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≤105,2≤n≤109
The following T lines, each line contains an integer n, according to the description.
T≤105,2≤n≤109
Output
For each test case, output an integer representing the answer.
Sample Input
2
3
10
Sample Output
2
0
题意:要求(n-1)! mod n
根据威尔逊定理,n为素数的时候(n-1)! mod n = -1 = n - 1,这是个充要条件
在n不是素数的时候,可以发现,只有4不为0,其余的时候都为0
1 /** 2 * code generated by JHelper 3 * More info: https://github.com/AlexeyDmitriev/JHelper 4 * @author xyiyy @https://github.com/xyiyy 5 */ 6 7 #include <iostream> 8 #include <fstream> 9 10 //##################### 11 //Author:fraud 12 //Blog: http://www.cnblogs.com/fraud/ 13 //##################### 14 //#pragma comment(linker, "/STACK:102400000,102400000") 15 #include <iostream> 16 #include <sstream> 17 #include <ios> 18 #include <iomanip> 19 #include <functional> 20 #include <algorithm> 21 #include <vector> 22 #include <string> 23 #include <list> 24 #include <queue> 25 #include <deque> 26 #include <stack> 27 #include <set> 28 #include <map> 29 #include <cstdio> 30 #include <cstdlib> 31 #include <cmath> 32 #include <cstring> 33 #include <climits> 34 #include <cctype> 35 36 using namespace std; 37 38 // 39 // Created by xyiyy on 2015/8/7. 40 // 41 42 #ifndef ICPC_SCANNER_HPP 43 #define ICPC_SCANNER_HPP 44 45 #endif //ICPC_SCANNER_HPP 46 47 class hdu5391 { 48 public: 49 void solve(std::istream &in, std::ostream &out) { 50 int t; 51 in >> t; 52 while (t--) { 53 int n; 54 in >> n; 55 if (check(n))out << n - 1 << endl; 56 else if (n == 4)out << 2 << endl; 57 else out << 0 << endl; 58 } 59 } 60 61 bool check(int x) { 62 for (int i = 2; i * i <= x; i++) { 63 if (x % i == 0) { 64 return 0; 65 } 66 } 67 return 1; 68 } 69 }; 70 71 int main() { 72 std::ios::sync_with_stdio(false); 73 std::cin.tie(0); 74 hdu5391 solver; 75 std::istream &in(std::cin); 76 std::ostream &out(std::cout); 77 solver.solve(in, out); 78 return 0; 79 }