【二次剩余-欧拉准则】HDOJ Jacobi symbol 3589

本文介绍了一个关于Jacobisymbol计算的问题,包括二次剩余的概念、Legendre符号及其计算规则,并详细阐述了Jacobisymbol的定义和计算方法。此外还提供了一段AC代码作为实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Jacobi symbol

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


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 x 2 ≡ 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< =10 6,2 < n < =10 6,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
 

Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:   3583  3590  3588  3587  3586 
 


题意:

求J(a,n)

解题思路:

二次剩余,欧拉准则!

数论中,特别在同余理论裏,一个整数 X 对另一个整数 p 的二次剩余英语Quadratic residue)指 X 的平方 X^2 除以 p 得到的余数

当对于某个d及某个X,式子 X^2 \equiv d \pmod{p} 成立时,称d是模p的二次剩余”

当对于某个d及某个XX^2 \equiv d \pmod{p} 不成立时,称d是模p的二次非剩余”

  欧拉准则:

p是奇质数p不能整除d,则:

d是模 p的二次剩余 当且仅当

d^{ \frac{p-1}{2}} \equiv 1 \pmod{p}

d是模 p的非二次剩余当且仅当:

d^{ \frac{p-1}{2}} \equiv -1 \pmod{p}

勒让德符号表示,即为: d^{ \frac{p-1}{2}} \equiv \left( \frac{d}{p}\right) \pmod{p}

AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

const int MAXN = 1000000+10;
typedef long long LL;

bool is_prime[MAXN];
int prime[MAXN];

void init()
{
    int tot=0;
    is_prime[0]=is_prime[1]=true;
    for(int i=2;i<MAXN;i++){
        if(!is_prime[i]){
            prime[tot++]=i;
            for(int j=i+i;j<MAXN;j+=i){
                is_prime[j]=true;
            }
        }
    }
}

int fast_power(int a,int n,int M)
{
    LL res=1;
    LL x=a;
    while(n){
        if(n&1) res=res*x%M;
        x=x*x%M;
        n>>=1;
    }
    return res%M;
}

int solve(int a,int p)
{
    if(a%p==0) return 0;
    return fast_power(a,(p-1)/2,p)==1?1:-1;
}

int main ()
{
    int a,n;
    init();
    while(scanf("%d%d",&a,&n)!=EOF){
        int ans=1;
        if(is_prime[n]){
            for(int i=0;prime[i]<=n;i++){
                if(n%prime[i]) continue;
                int cnt=0;
                while(n%prime[i]==0){
                    n/=prime[i];
                    cnt++;
                }
                int t=solve(a,prime[i]);
                if(t==-1&&cnt%2==0) t=1;
                ans*=t;
            }
        }
        else ans=solve(a,n);
        printf("%d\n",ans);
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值