[HDOJ5391]Zball in Tina Town

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5391

  考的数论中的威尔逊定理,题目直接给出了威尔逊定理的表达式的语言描述。

  在初等数论中,威尔逊定理给出了判定一个自然数是否为素数的充分必要条件。即:当且仅当p为素数时:( p -1 )! ≡ -1 ( mod p ),但是由于阶乘是呈爆炸增长的,其结论对于实际操作意义不大。

  本题特别注意的是n=4的时候,要特判一下,是个trick。cout的话,也是会超时的。还有,本题数据给的是2<=n<=10^9,需要考虑的是如果打表的话,复杂的是O(nsqrt(n)),如果单独判的话是O(n),所以不选择打表。代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <deque>
#include <queue>

using namespace std;
typedef long long LL;

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int flag = 0;
        int n;
        scanf("%d", &n);
        int m = int(sqrt(n));
        for (int i = 2; i < m; i++)
        {
            if (n % i == 0)
            {
                flag++;
                break;
            }
        }
        if(n == 0 || n == 1) {
            printf("1\n");
        }
        else if(n == 2) {
            printf("2\n");
        }
        else if(!flag) {
            printf("%d\n", n-1);
        }
        else {
            printf("0\n");
        }
    }
}

时间: 2024-10-11 20:40:15

[HDOJ5391]Zball in Tina Town的相关文章

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天的时候,

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 i

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.

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

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