Jacobi symbol(裸雅可比符号)

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

2010 ACM-ICPC Multi-University Training Contest(15)——Host by NUDT

/*
题意:裸的雅可比符号,雅可比符号是勒让德符号的延伸,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=1;
    for(;b;b>>=1)
    {
        if(b&1)
        res=(res*a)%p;
        a=(a*a)%p;
    }
    return res;
}

int cal(int a,int n)
{
    if(a%n==0)
    return 0;
    else
    return exp(a,(n-1)/2,n)==1?1:-1;
}
/**********************勒让德符号************************/

/***********************筛素数*************************/
const int M = 1000100;
int p[M], pNum=0;
bool f[M];

void Prime()
{
    int i, j;
    for(i = 2; i < 1000010; i++)
    {
        if(!f[i])//i是素数
        {
            p[pNum++] = i; //将素数打到数组中
        }
        for(j = 0; j < pNum && p[j] * i < M; j++ ) //将i的倍数都调出来因为,素数的倍数肯定不是素数
        {
            f[p[j]*i] = 1;
            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]==0){//如果n是素数
            printf("%d\n",cal(a,n));
            continue;
        }
        cur=1;
        for(int i=0;n!=1&&i<pNum;i++){
            if(n%p[i]==0){//这个是质因子
                int total=0;
                while(n%p[i]==0){
                    total++;
                    n/=p[i];
                }
                int tmp=cal(a,p[i]);
                if(total%2==0&&tmp==-1)//如果n里面有偶数个的p,那么p乘偶数肯定不是奇数,就不符合勒让德符号定义了
                    tmp=1;
                cur*=tmp;
            }
        }
        printf("%d\n",cur);
    }
    return 0;
}
时间: 2024-08-26 13:26:11

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

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

先贴代码,有空再整理思路 PGP加密 using System; using System.IO; using Org.BouncyCastle.Bcpg; using Org.BouncyCastle.Bcpg.OpenPgp; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities.IO; using System.Linq; namespace Server5.V2.Common { public static c

bininteger

//************************************************************************************// BigInteger Class Version 1.03//// Copyright (c) 2002 Chew Keong TAN// All rights reserved.//// Permission is hereby granted, free of charge, to any person obtain

C++ 实现Biginteger

网上C++版Biginteger参差不齐,一下子没有找到一个令人满意Biginteger,最近用c++改写了一下C#版 BigInteger,可以用于RSA大素数的生成,分享给大家.也请大家批评指正改的不好的地方. 其中有几个类型未在CPP中: typedef unsigned char Byte; #define null 0 typedef unsigned int Uint; typedef unsigned __int64 Uint64; //最大长度200 : 200 Uint=200

【嵌入式开发】 嵌入式开发工具简介 (裸板调试示例 | 交叉工具链 | Makefile | 链接器脚本 | eclipse JLink 调试环境)

作者 : 韩曙亮 博客地址 : http://blog.csdn.net/shulianghan/article/details/42239705  参考博客 : [嵌入式开发]嵌入式 开发环境 (远程登录 | 文件共享 | NFS TFTP 服务器 | 串口连接 | Win8.1 + RedHat Enterprise 6.3 + Vmware11) 开发环境 : -- 操作系统 : Vmware11 + RedHat6.3 企业版 + Win8.1; -- 硬件 : OK-6410-A 开发

hdu 3589(二次剩余+雅可比符号)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3589: 题意:就是一个裸的雅可比符号: 具体参考:百度百科  百度百科 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> using namespace std; int a[10]; int fac[100500],sum[100500]; in

栈(裸题)

Stack    Aizu - ALDS1_3_A Write a program which reads an expression in the Reverse Polish notation and prints the computational result. An expression in the Reverse Polish notation is calculated using a stack. To evaluate the expression, the program

从零开始写一个arm下的裸板程序

从零开始写一个arm下的裸板程序.我们整个程序是基于uboot运行的. 所有我们可以借助uboot中的printf来输出,默认开发版的标准输出是串口. 电脑的默认标准输出的屏幕. 1.需要创建的文件由include文件夹,用来存放头文件. 2.创建一个hw.h头文件. 3.编写一个common.h,它定义了借用uboot的printf的宏.和NULL这个宏的定义. 4.hw.c 硬件相关的文件. 5.main.c c文件. 6.start.s 汇编文件. 7.ld.lds 链接脚本, 8.Mak

SZOJ 167 Lca裸题

一道.......一道我改了一周的裸题 无根树建双向边 无根树建双向边 无根树建双向边 重要的事情说三遍(微笑) 还有要开longlong 还有双向边不是双倍边(微笑) 我真是,能把自己气吐血10次就不把自己气吐血9次 [问题描述] 已知一棵nn个点的树,点从1开始标号,树上每条边都有一个正整数边权. 有qq个询问,每个询问由type,u,vtype,u,v三个正整数构成. 当type=1type=1时,询问uu到vv路径上所有边权的二进制异或和. 当type=2type=2时,询问uu到vv路

嵌入式 hi3518c裸板uboot烧写、kernel烧写、fs烧写小结

1.在uboot中我可以添加自己的命令,添加的方法是找到一个uboot的命令,然后模仿着去增加属于自己的命令代码以及实现函数就可以 2.记住在使用printf进行调试的时候,在遇到指针或者字符串的时候最好使用“%x”,以为我不知道指针或者字符串中是否包含不可见字符,如果有不可见字符会导致错误,而且错误不好查找 3.对于uboot中的环境变量,其实是放在uboot里面的,也就是环境变量占用的是uboot的空间,如果不需要去实时修改环境变量的值那么就可以不用env这个分区:但是如果需要修改环境变量,