hdu5391 Zball in Tina Town(威尔逊定理)

转载请注明出处: 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.

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

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 }
时间: 08-14

hdu5391 Zball in Tina Town(威尔逊定理)的相关文章

hdu 5391 Zball in Tina Town 威尔逊定理 数学

Zball in Tina Town Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) 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 lar

hdu5391 Zball in Tina Town(素数筛法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5391 题目大意:一个球初始体积为1,一天天变大,第一天变大1倍,第二天变大2倍,第n天变大n倍.问当第 n-1天的时候,体积变为多少.注意答案对n取模. 思路:题目意思搞了好久,其实就是第一天是1,第二天是1*2,第三天是1*2*3,也就是当第n天的时候是n!.那么答案就是(n-1)! % n. 题目里面n是10^9,不可能直接弄阶乘.这时可以想想,能不能找找规律看. 打表以后发现,如果是素数,答案

HDU 5391 Zball in Tina Town【威尔逊定理】

<题目链接> 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 siz

Zball in Tina Town(判素数)

Zball in Tina Town Accepts: 397 Submissions: 2463 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Problem Description Tina Town is a friendly place. People there care about each other. Tina has a ball called zball.

BC - Zball in Tina Town (质数 + 找规律)

Zball in Tina Town Accepts: 541 Submissions: 2463 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) 问题描述 Tina Town 是一个善良友好的地方,这里的每一个人都互相关心. Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大11倍.在第二天的时候,它会变大22倍.在第nn天的时候,

Zball in Tina Town

Zball in Tina Town Accepts: 356 Submissions: 2463 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Problem Description Tina Town is a friendly place. People there care about each other. Tina has a ball called zball.

【HDU5391】Zball in Tina Town

[题目大意] 一个球初始体积为1,一天天变大,第一天变大1倍,第二天变大2倍,第n天变大n倍.问当第 n-1天的时候,体积变为多少.注意答案对n取模. [题解] 根据威尔逊定理:(n-1)! mod n =-1 所以当n为质数时,答案就是n-1 否则答案显然是0 注意4要特判一下,ans[4]=2 这里有一种很魔性的判素数的方法,详见代码 1 /************* 2 HDU 5391 3 by chty 4 2016.11.4 5 *************/ 6 #include<i

hdu 5391 Zball in Tina Town(打表找规律)

问题描述 Tina Town 是一个善良友好的地方,这里的每一个人都互相关心. Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大11倍.在第二天的时候,它会变大22倍.在第nn天的时候,它会变大nn倍. zball原来的体积是11.Tina想知道,zball在第n-1n−1天时的体积对nn取模是多大呢? Tina是一个蠢蠢的女孩子,当然不会算啦,所以她请你帮她算出这个答案呢. 输入描述 第一行一个正整数TT,表示数据组数 接下来TT行,每行一个正整

hdu 5391 Zball in Tina Town

点击此处即可传送 hdu 5391 唉,我以为带7的基本上都是素数,所以一直拿1007算,结果....唉,一把辛酸泪啊,算了,不说了,上正事: 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,