POJ_1808_Quadratic Residues

Quadratic Residues
Time Limit: 1000MS
Memory Limit: 30000K
Total Submissions: 1614
Accepted: 542

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 
x2 ≡ 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 < 109 is an odd prime, and a satisfies both a !≡ 0 (mod p) and |a| <= 109.

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

Source

TUD Programming Contest 2003, Darmstadt, Germany

  • 判定平方剩余是否有解
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long           LL ;
typedef unsigned long long ULL ;
const int    maxn = 1000 + 10  ;
const int    inf  = 0x3f3f3f3f ;
const int    npos = -1         ;
const int    mod  = 1e9 + 7    ;
const int    mxx  = 100 + 5    ;
const double eps  = 1e-6       ;

LL pow_mod(LL a, LL k, LL m){
	LL r=1%m;
	while(k){
		if(1&k)r=(r*a)%m;
		a=(a*a)%m;
		k>>=1;
	}
	return r%m;
}
LL mod_sqr(LL a, LL n){
	if(1==pow_mod(a,(n-1)/2,n))
		return 1;
	return -1;
}
int T, kase, a, p;
int main(){
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	while(~scanf("%d",&T)){
		for(kase=1;kase<=T;kase++){
			scanf("%lld %lld",&a,&p);
			printf("Scenario #%d:\n%d\n\n",
				kase,mod_sqr((a%p+p)%p,p));
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值