Poj 1808 quadratic residues secondary surplus

Source: Internet
Author: User

 

/** Poj1808.c * created on: 2011-10-12 * Author: bjfuwangzhu * // **> consider the same formula as X2 limit N (mod m), where M> 1, (m, n) = 1. *> If there is a solution to the same formula, n is called the quadratic residue of the Modulo M. If there is no solution to the same formula, n is called the quadratic non-residue of the Modulo M. *> If P is an odd prime number, the number of Quadratic Residue and quadratic non-residue of the modulo p is exactly "half to half ", *> The following table shows the quadratic surplus and non-surplus of several small prime numbers: *> P residual non-surplus> 3 1 2> 5 2, 3, 3> 7 *> 1, 2, 4, 5, 6> 11, 10> 13 *>,>, *> if n is the quadratic residue of the modulo p, then n ^ (p-1)/2) limit 1 (mod P ). If n is the quadratic non-residue of the modulo p, *> then n ^ (p-1)/2) decimal-1 (mod P ). */# Include <stdio. h> # define ll long longint modular_exp (int A, int B, int c) {ll res, temp; Res = 1% C, temp = A % C; while (B) {If (B & 1) {res = res * temp % C;} temp = temp * temp % C; B >>=1 ;} return (INT) RES ;} void solve (int A, int p) {A = (a % P + p) % P; If (modular_exp (A, (p-1)/2, P) = 1) {puts ("1"); return;} puts ("-1");} int main () {# ifndef online_judgefreopen ("data. in "," r ", stdin); # endifint T, I, A, P; scanf (" % d ", & T); for (I = 1; I <= T; I ++) {scanf ("% d", & A, & P); printf ("Scenario # % d: \ n ", i); solve (A, P); printf ("\ n");} 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.