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 become2 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

题意:求 从1乘到n-1模n的结果。

只要判断n是否是素数,如果是就输出n-1,否则输出0。n=4,不满足这规律,要特判。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<map>
 4 #include<cmath>
 5 using namespace std;
 6 int zj[100000];
 7 int ssb[100000];
 8 int pn;
 9 void p()
10 {
11     int i,j;
12     zj[1]=0;
13     zj[0]=0;
14     pn=0;
15     for (i=2;i<=100000;i++)
16     {
17         if (!zj[i]) ssb[pn++]=i;
18         for (j=0;j<pn;j++)
19         {
20             if (i*ssb[j]>100000) break;
21             zj[i*ssb[j]]=1;
22             if (i%ssb[j]==0) break;
23         }
24     }
25 }
26 int main()
27 {
28     p();
29     int n,m,t,i,flag;
30     scanf("%d",&t);
31     while (t--)
32     {
33         flag=0;
34         scanf("%d",&n);
35         if (n==3||n==4) {printf("2\n");continue;}
36         if (n<=100000)
37         {
38             if (zj[n]) printf("0\n");
39             else printf("%d\n",n-1);
40             continue;
41         }
42         flag=0;
43         for (i=0;i<pn;i++)
44         {
45             if (ssb[i]*ssb[i]>n||flag) break;
46             if (n%ssb[i]==0) flag=1;
47         }
48         if (flag) printf("0\n");
49         else printf("%d\n",n-1);
50     }
51 }

11

时间: 2024-10-01 20:39:46

hdu 5391 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

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

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,

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 (打表,水)

题意: Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大1倍.在第二天的时候,它会变大2倍.在第n天的时候,它会变大n倍.zball原来的体积是1.Tina想知道,zball在第n−1天时的体积对n取模是多大呢? 思路:如果n是一个合数,那它肯定是有因子的,即有两个比它小的数字a和b,使得a*b=n,那么答案必为0.如果是质数,打表可知是n-1.数字小的直接算,防止意外.数字都不会超过int. #include <cstdio> #include

hdu 5392 Zball in Tina Town(素数)

题意:求(x-1)!modx(x<10^9),哥德巴赫猜想,打表得当x为素数时,结果为x-1,合数时为0:x=4时为2特判: 思路:判断素数的时候脑子抽了,想成素数大数了,然后一直TLE; 当要判断的数的平方根为10^8级别时再考虑大数情况... #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int t,n,m; int main(){ int i,j,k,f

(hdu)5391 Zball in Tina Town

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5391 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 t

hdoj 5391 Zball in Tina Town

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5391 相关数论结论: 威尔逊定理——当且仅当p为素数时:( p -1 )! ≡ p-1 ( mod p ) 这道题一开始看到的时候以为是暴力的题目,快速阶乘取模,后来其他题做不出来回头看这道题, 发现可以找规律,当p是合数的时候,P的因子全都可以在P前面的数字的因子里找到, 当p是素数的时候,(p-1)!%p = p-1,就尝试写了一下,2A: 因为4是个例外,怕还有其他例外情况,在100以内小规模

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.