Quadratic Residues POJ - 1808 二次剩余定理

\(\color{#0066ff}{题目链接 }\)

link

\(\color{#0066ff}{ 题解 }\)

结论题

\((\frac{a}{p})=a^{\frac{p-1}{2}}\mod p\)

若等于1则为二次剩余

#include<cstdio>
#include<cctype>
#define LL long long
LL in() {
    char ch; LL x = 0, f = 1;
    while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}
int ksm(LL x, LL y, LL mod) {
    ((x %= mod) += mod) %= mod;
    LL re = 1LL;
    while(y) {
        if(y & 1) re = re * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return re == 1? 1 : -1;
}
int main() {
    int T = in();
    for(int i = 1; i <= T; i++) {
        LL n = in(), m = in();
        printf("Scenario #%d:\n%d\n\n", i, ksm(n, (m - 1) >> 1, m));
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/olinr/p/10303902.html
今日推荐