[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");
        }
    }
}
View Code

 

posted @ 2015-08-15 23:53  Kirai  阅读(157)  评论(0编辑  收藏  举报