Jacobi symbol

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 625    Accepted Submission(s): 258

Problem Description
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 introduced the following notation, called the Legendre symbol, L (a,p):

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 Jacobi symbol, J (a, n) ,is a generalization of the Legendre symbol ,L (a, p).It defines as :
1.  J (a, n) is only defined when n is an odd.
2.  J (0, n) = 0.
3.  If n is a prime number, J (a, n) = L(a, n).
4.  If n is not a prime number, J (a, n) = J (a, p1) *J (a, p2)…* J (a, pm), p1…pm is the prime factor of n.

 
Input
Two integer a and n, 2 < a< =106,2 < n < =106,n is an odd number.
 
Output
Output J (a,n)
 
Sample Input
3 5
3 9
3 13
 
Sample Output
-1
0
1
 
Author
alpc41
 
Source
/*
题意:裸的雅可比符号,雅可比符号是勒让德符号的延伸,J(a,n)如果n是素数那么J(a,n)=L(a,n);否则J(a,n)=J(a,p1)*J(a,p2)*...J(a,pm);
p1...pm是n的质因子,勒让德符号:定义为 L(a,n)=0 n mod a=0;
L(a,n)=1 存在X使得 X^2 mod a=0;
L(a,n)=-1 不存在X使得 X^2 mod a=0;
#错误:求雅可比符号的时候,按照定义爆的,不知道哪里错了...分解质因子板套错了 #改进:勒让德符号n是偶数的时候要特判,特别要注意的时候质因子也有偶数,就是2 */
#include<bits/stdc++.h>
#define ll long long
using namespace std;
/**********************勒让德符号************************/
ll exp(ll a,ll b,ll p)
{
ll res=;
for(;b;b>>=)
{
if(b&)
res=(res*a)%p;
a=(a*a)%p;
}
return res;
} int cal(int a,int n)
{
if(a%n==)
return ;
else
return exp(a,(n-)/,n)==?:-;
}
/**********************勒让德符号************************/ /***********************筛素数*************************/
const int M = ;
int p[M], pNum=;
bool f[M]; void Prime()
{
int i, j;
for(i = ; i < ; i++)
{
if(!f[i])//i是素数
{
p[pNum++] = i; //将素数打到数组中
}
for(j = ; j < pNum && p[j] * i < M; j++ ) //将i的倍数都调出来因为,素数的倍数肯定不是素数
{
f[p[j]*i] = ;
if(!(i%p[j]))
break;
}
}
}
/***********************筛素数*************************/
int a,n;
int cur;
int main(){
Prime();
// freopen("in.txt","r",stdin);
while(scanf("%d%d",&a,&n)!=EOF){
if(f[n]==){//如果n是素数
printf("%d\n",cal(a,n));
continue;
}
cur=;
for(int i=;n!=&&i<pNum;i++){
if(n%p[i]==){//这个是质因子
int total=;
while(n%p[i]==){
total++;
n/=p[i];
}
int tmp=cal(a,p[i]);
if(total%==&&tmp==-)//如果n里面有偶数个的p,那么p乘偶数肯定不是奇数,就不符合勒让德符号定义了
tmp=;
cur*=tmp;
}
}
printf("%d\n",cur);
}
return ;
}

Jacobi symbol(裸雅可比符号)的更多相关文章

  1. hdu3589 Jacobi symbol(二次剩余 数论题)

    本题的注意点:n=p1*p2*p3......Pm 解法:直接利用公式a^((p-1)/2)=(a/p)mod p 即可求解. #include<stdio.h> #include< ...

  2. HDU 3589 Jacobi symbol

    彻底对数学绝望了 #include <cstdio> #include <cmath> int flag[1005],p[500],a; int d[100]; int ini ...

  3. PHP7函数大全(4553个函数)

    转载来自: http://www.infocool.net/kb/PHP/201607/168683.html a 函数 说明 abs 绝对值 acos 反余弦 acosh 反双曲余弦 addcsla ...

  4. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

  5. DotNet加密方式解析--非对称加密

    新年新气象,也希望新年可以挣大钱.不管今年年底会不会跟去年一样,满怀抱负却又壮志未酬.(不过没事,我已为各位卜上一卦,卦象显示各位都能挣钱...).已经上班两天了,公司大部分人还在休假,而我早已上班, ...

  6. PHP类和函数注释大全

    每次要用PHP的某个功能的时候,都要去查一下,于是决定将PHP所有类和函数都整理出来,加上注释 大致实现 将php.jar文件解压,取出目录stubs 将stubs中的所有php文件中的注释去掉,并做 ...

  7. php小数加减精度问题,比特币计算精度问题

    php小数加减精度问题,比特币计算精度问题 在php开发时,有小数加减的场景.结果发现不能够等于预想的值,bccomp比较二个高精确度数字.语法: int bccomp(string left ope ...

  8. 【加解密专辑】对接触到的PGP、RSA、AES加解密算法整理

    先贴代码,有空再整理思路 PGP加密 using System; using System.IO; using Org.BouncyCastle.Bcpg; using Org.BouncyCastl ...

  9. C++ 实现Biginteger

    网上C++版Biginteger参差不齐,一下子没有找到一个令人满意Biginteger,最近用c++改写了一下C#版 BigInteger,可以用于RSA大素数的生成,分享给大家.也请大家批评指正改 ...

随机推荐

  1. 【BZOJ】 2463 [中山市选2009]谁能赢呢?(博弈论)

    Description   小明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移动到的 ...

  2. margin:0px auto和text-align:center区别

    (1)margin:0px auto :作用于块级元素,对块级元素进行居中 (2)text-align:center:作用于内联元素,必须放在要居中的内联元素所在的块级元素. 例: (1) <d ...

  3. oracle排序的几种方法

    1.创建数据库表 CREATE TABLE USER_INFO(  USERID      VARCHAR2(10 BYTE)                 NOT NULL,  USERNAME  ...

  4. Java面向对象 其他对象

     Java面向对象  其他对象 知识概要:             (1)可变参数 (2)静态导入 (3)System (4)Runtime (5)Date  Calendar (6)Math 本 ...

  5. asp.net(C#)利用QRCode生成二维码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="QRCode.aspx.cs&q ...

  6. TreeViewItem实现整行选中 (两种用法)

    用法一 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quo ...

  7. 第四章 MySQL高级查询(二)

    第四章 MySQL高级查询(二) 一.EXISTS子查询 在执行create 或drop语句之前,可以使用exists语句判断该数据库对像是否存在,返回值是true或false.除此之外,exists ...

  8. 从头编写 asp.net core 2.0 web api 基础框架 (5) EF CRUD

    第1部分:http://www.cnblogs.com/cgzl/p/7637250.html 第2部分:http://www.cnblogs.com/cgzl/p/7640077.html 第3部分 ...

  9. 基于Redis位图实现系统用户登录统计

    项目需求,试着写了一个简单登录统计,基本功能都实现了,日志数据量小.具体性能没有进行测试~ 记录下开发过程与代码,留着以后改进! 1. 需求 1. 实现记录用户哪天进行了登录,每天只记录是否登录过,重 ...

  10. Appium-desktop安装与使用

    Appium-desktop是什么? 项目描述: Appium Server and Inspector in Desktop GUIs for Mac, Windows, and Linux. Ap ...