hdu5391 Zball in Tina Town (Wilson theorem)

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud

Zball in Tina town

Time limit:3000/1500 MS (java/others) Memory limit:262144/262144 K (java/others)
Total submission (s): 219 Accepted Submission (s): 144


Problem Descriptiontina Town was a friendly place. People there care on each of the other.

Tina has a ball called Zball. Zball is magic. It grows larger every day. On the first day, it becomes1 time as large as its original size. On the second day,it would become 2 times as large as the size on the first day . On the n-th day,it would become n times as large as the size on the (n-1)-th Day. Tina want to know it size on the (n-1)-th day modulo n.


Inputthe first line of input contains an integerT, representing the number of cases.

The followingTLines, each line contains an integerN, according to the description.
t≤ 105,< Span id= "mathjax-span-31" class= "mn" >2≤ n≤ 109


Outputfor each test case, output an integer representing the answer.


Sample Input2310


Sample Output20

Test instructions: requirements (n-1)! MoD n
According to Wilson's theorem, n is prime time (N-1)! MoD n =-1 = n-1, which is a necessary and sufficient condition

When n is not a prime number, it can be found that only 4 is not 0 and the rest is 0.

1 /**2 * Code generated by Jhelper3 * More info:Https://github.com/AlexeyDmitriev/JHelper4 * @author Xyiyy @Https://github.com/xyiyy5  */6 7#include <iostream>8#include <fstream>9 Ten //##################### One //Author:fraud A //Blog:http://www.cnblogs.com/fraud/ - //##################### - //#pragma COMMENT (linker, "/stack:102400000,102400000") the#include <iostream> -#include <sstream> -#include <ios> -#include <iomanip> +#include <functional> -#include <algorithm> +#include <vector> A#include <string> at#include <list> -#include <queue> -#include <deque> -#include <stack> -#include <Set> -#include <map> in#include <cstdio> -#include <cstdlib> to#include <cmath> +#include <cstring> -#include <climits> the#include <cctype> *  $ using namespacestd;Panax Notoginseng  - // the //Created by Xyiyy on 2015/8/7. + // A  the #ifndef icpc_scanner_hpp + #defineIcpc_scanner_hpp -  $ #endif //icpc_scanner_hpp $  - classhdu5391 { -  Public: the     voidSolve (Std::istream &inch, Std::ostream & out) { -         intT;Wuyi         inch>>T; the          while(t--) { -             intN; Wu             inch>>N; -             if(check (n)) out<< N-1<<Endl; About             Else if(n = =4) out<<2<<Endl; $             Else  out<<0<<Endl; -         } -     } -  A     BOOLCheckintx) { +          for(inti =2; I * I <= x; i++) { the             if(x% i = =0) { -                 return 0; $             } the         } the         return 1; the     } the }; -  in intMain () { theStd::ios::sync_with_stdio (false); theStd::cin.tie (0); About hdu5391 solver; theStd::istream &inch(std::cin); theStd::ostream & out(std::cout); theSolver.solve (inch, out); +     return 0; -}

hdu5391 Zball in Tina Town (Wilson theorem)

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.