Description
Background
In 1801, Carl Friedrich Gauss (1777-1855) published his "Disquisitiones Arithmeticae", which basically created modern number theory and is still being sold today. One of the many topics treated in his book was the problem of quadratic residues.
Consider a prime number p and an integer a !≡ 0 (mod p). Then a is called a quadratic residue mod p if there is an integer x such that
x 2 ≡ a (mod p),
and a quadratic non residue otherwise. Lagrange (1752-1833) introduced the following notation, called the "Legendre symbol":
For the calculation of these symbol there are the following rules, valid only for distinct odd prime numbers p, q and integers a, b not divisible by p:
The statements 1. to 3. are obvious from the definition, 4. is called the Completion Theorem, and 5. is the famous Law of Quadratic Reciprocity for which Gauss himself gave no less than six different proofs in the "Disquisitiones Arithmeticae". Knowing these facts, one can calculate all possible Legendre symbols as in the following example:
In 1801, Carl Friedrich Gauss (1777-1855) published his "Disquisitiones Arithmeticae", which basically created modern number theory and is still being sold today. One of the many topics treated in his book was the problem of quadratic residues.
Consider a prime number p and an integer a !≡ 0 (mod p). Then a is called a quadratic residue mod p if there is an integer x such that
x 2 ≡ a (mod p),
and a quadratic non residue otherwise. Lagrange (1752-1833) introduced the following notation, called the "Legendre symbol":
![](https://i-blog.csdnimg.cn/blog_migrate/e6c2ac62715a8dde1c44a5d914805494.jpeg)
For the calculation of these symbol there are the following rules, valid only for distinct odd prime numbers p, q and integers a, b not divisible by p:
![](https://i-blog.csdnimg.cn/blog_migrate/6f532a4307d5944406a3015795891d4e.jpeg)
The statements 1. to 3. are obvious from the definition, 4. is called the Completion Theorem, and 5. is the famous Law of Quadratic Reciprocity for which Gauss himself gave no less than six different proofs in the "Disquisitiones Arithmeticae". Knowing these facts, one can calculate all possible Legendre symbols as in the following example:
![](https://i-blog.csdnimg.cn/blog_migrate/2c375d6c4fcb2efbf5b5bb41ad3a2a80.jpeg)
Input
The first line contains the number of scenarios.
For each scenario, there is one line containing the integers a and p separated by a single blank, where 2 < p < 10 9 is an odd prime, and a satisfies both a !≡ 0 (mod p) and |a| <= 10 9.
For each scenario, there is one line containing the integers a and p separated by a single blank, where 2 < p < 10 9 is an odd prime, and a satisfies both a !≡ 0 (mod p) and |a| <= 10 9.
Output
Start the output for every scenario with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing (a/p), followed by a blank line.
Sample Input
3 29 79 2 29 1 3
Sample Output
Scenario #1: -1 Scenario #2: -1 Scenario #3:1
题意: 已知a, p(p为素数),求 x^2≡a(mod p) 是否成立,成立输出1,否则输出-1; 题解: 假设p是素数,a是整数. 如果存在一个整数x使得x^2≡a(mod p) (即x^2-a可以被p整除),那么就称a在p的剩余类中是平方剩余的. 欧拉定理说:如果p是奇素数,则a平方剩余当且仅当 a^((p-1)/2)≡1 (mod p). 在{1,2,...,p-1}中恰好有 (p-1) / 2 个数是平方剩余的. 勒让德符号:(a / p) = 1(相应的,-1) 如果 a是平方剩余(相应的,如果 a不是平方剩余). */ //标程: #include <cstdio> #include <iostream> using namespace std; typedef long long ll; ll pow_mod(ll a, ll b, ll c) { ll ans=1; while(b) { if(b % 2) ans = ans * a % c; a= a * a % c; b /= 2; } return ans; } int main() { // freopen("a.txt","r",stdin); ll a,n,ans; int t; cin >> t; for(int i = 1; i <= t; ++i) { scanf("%lld%lld",&a,&n); ans = pow_mod(a,(n-1)/2,n); printf("Scenario #%d:\n",i); if(ans == n-1) ans = -1; else ans = 1; printf("%lld\n\n",ans); } return 0; }