"Wilson theorem" HDU 5391 zball in Tina town

Source: Internet
Author: User

HDU 5391 Zball in Tina Town (Wilson theorem)

Test instructions: Give an n, Beg (n-1). %n equals how much.

Wilson's theorem: When and only if N is a prime number: (n-1)!≡-1 (mod n)
Solution: According to Wilson's theorem, p is the prime number of the answer is n-1. Think of the non-Prime, whose n can be multiplied by the preceding number, then the answer is 0. But the 4 answer exception is 2.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
const int N = 1E5+10;
typedef long long LL;

int main ()
{
    int t;
    scanf ("%d", &t);
    while (t--)
    {
        int i, n;
        int s = 1;
        scanf ("%d", &n);
        for (i = 2; i*i <= n; ++i)
        {
            if (!) ( n% i))
            {
                s = 0;
                break;
            }
        }
        if (n = = 4)
            puts ("2");
        else if (s)
            printf ("%d\n", n-1);
        Else
            puts ("0");
    }
    return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.