您的位置:首页 > 其它

hdu 5391 Zball in Tina Town(威尔逊定理)

2015-08-17 08:57 288 查看

题意:

求(n−1)!modn(n−1)!\mod n

解析:

如果n为合数,显然答案为0.

如果n为素数,那么由威尔逊定理可得答案为 n - 1

注意有个trick为 n = 4

注意:

判断素数可以用快速判素数的方法。

mymy codecode

[code]#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

bool isPrime(int num) {  
    if (num == 2 || num == 3) {
        return true;  
    }
    if (num % 6 != 1 && num % 6 != 5) {
        return false;  
    }
    for (int i = 5; i*i <= num; i += 6) {
        if (num % i == 0 || num % (i+2) == 0) {
            return false;
        }
    }
    return true;  
}

ll n;
int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%lld", &n);
        if(n == 4) {
            puts("2");
            continue;
        }
        if(isPrime(n)) printf("%lld\n", n-1);
        else puts("0");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: