Quadratic Residue
Time limit1sMemory limit128 MB
For each test case, compute the Legendre symbol (a/p) for an odd prime p and integer a, using quadratic reciprocity.
- Level
Medium7 of 10
- Topics
- Number theory, Math, Divide and conquer
- Solved
- No attempts yet
Problem
In 1801, Carl Friedrich Gauss (1777-1855) published "Disquisitiones Arithmeticae", which laid the foundations of modern number theory and is still in print today. One of the topics it treats most often is the quadratic residue.
You are given a prime and an integer with . For to be a quadratic residue modulo , there must exist an integer such that
Legendre (1752-1833) introduced the following Legendre symbol.
The Legendre symbol can be computed using the following properties, which hold only for distinct odd primes , and integers , not divisible by .
For example, the Legendre symbol can be computed as follows.
Input
The input consists of several test cases. The first line contains the number of test cases . Each of the following test cases consists of two integers and . Here is an odd prime, and satisfies and .
Output
For each test case, print "Scenario #i:" on the first line, where is the test case number starting from 1. On the next line, print the value of the Legendre symbol (either 1 or -1). Print one blank line between the outputs of consecutive test cases.