zoukankan      html  css  js  c++  java
  • 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. T105,2n109
     
    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
    ");continue;}
    36         if (n<=100000)
    37         {
    38             if (zj[n]) printf("0
    ");
    39             else printf("%d
    ",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
    ");
    49         else printf("%d
    ",n-1);
    50     }
    51 }

    11

  • 相关阅读:
    学点 C 语言(40): 函数 多参函数
    存取 ListBox 列表 回复 "徐强" 的问题
    博客园RSS订阅汇总
    博客园电子期刊2012年2月刊发布啦
    上周热点回顾(3.53.11)
    博客园电子期刊2012年3月刊发布啦
    上周热点回顾(3.264.1)
    上周热点回顾(3.193.25)
    上周热点回顾(4.24.8)
    上周热点回顾(2.273.4)
  • 原文地址:https://www.cnblogs.com/pblr/p/4733787.html
Copyright © 2011-2022 走看看