Hdoj 5391 Zball in Tina town

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5391

Test instructions: give you a N, ask you (N-1)! The value of% n.

This problem is a Wilson theorem, the content of Wilson's theorem is that if n is a prime number, then there must be (n-1)! % n = =-1, in fact, if the comparison wit know to play table students should also be able to see this law, then small series here to prove this Wilson theorem, first in this mod n multiplication, there are n-1 elements, if n is a prime number, then each element must exist an inverse element, that thinking, If each element has an inverse then the 22 pairing result should not be 1 ... Of course, there is no way to consider the 22 pairing, that is, the inverse is its own, x^2 MOD n = 1, the solution x = 1 or x = n-1, then we in the hierarchy in addition to the two special just good can be 22 paired, and because (n-1)%n = = N-1, so the result is n-1.

Then the prime number is solved, we look at the non-prime, think carefully, if there is a factor x, then X * (n/x) = = N, so if it is a non-prime, then there must be two factors, so that the product is n, then the whole class must be a multiple of n, so the answer is 0. But is it true that every non-prime is like this? Of course, we have to consider the special case, that is, there is only one factor X, and x^2 = = N, this obviously can not constitute a multiple of n, the value of the solution is 4.

So this problem we only need to determine whether it is a prime number, if the number of non-prime and then a special sentence of 4 is good.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64
int prime (int n)
{for
	(int i=2; i*i<=n; i++)
		if (n% i = = 0)
			return 0;
	return 1;
}
int main ()
{
	int T;
	scanf ("%d", &t);
	while (t--)
	{
		int n;
		scanf ("%d", &n);
		if (n = = 4) printf ("2\n");
		else if (!prime (n)) printf ("0\n");
		else printf ("%d\n", n-1);
	}
}


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.