Quadratic Residues

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: 

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.

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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值