威尔逊定理的应用

题目: 求 (n-1)!mod(n)的值。

很显然当 n 是合数时  结果为0(当然这里有个特例4)

当 n 为素数时, 直接用 威尔逊定理  结果为n-1.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define LL long long
using namespace std;

bool is_prime(LL n)
{
    for(int i=2; i*i<=n; i++)
    {
        if(n%i==0)
        return false;
    }
    return true;
}
int main()
{
    int T;
    LL n;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lld", &n);
        if(n==4)
        printf("2\n");
        else if(is_prime(n))
        {
            printf("%lld\n", n-1);
        }
        else 
        printf("0\n");
    }
    return 0;
}

 

posted @ 2015-08-29 16:18  草滩小恪  阅读(517)  评论(0编辑  收藏  举报