uva 10831 - Gerg's Cake(勒让德记号)

题目链接:uva 10831 - Gerg‘s
Cake

题目大意:给定a和p,p为素数,问说是否存在x,使得x2≡a%p

解题思路:勒让德记号,判断ap?12≡1%p

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

ll pow_mod (ll a, ll n, ll mod) {
    ll ans = 1;
    while (n) {
        if (n&1)
            ans = ans * a % mod;
        a = a * a % mod;
        n /= 2;
    }
    return ans;
}

int legendre (ll a, ll p) {
    a %= p;
    if (a == 0)
        return 0;
    if (pow_mod(a, (p-1)/2, p) == 1)
        return 1;
    else
        return -1;
}

int main () {
    ll a, p;
    while (scanf("%lld%lld", &a, &p) == 2 && a != -1 && p != -1) {
        if (legendre(a, p) < 0)
            printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;
}

uva 10831 - Gerg's Cake(勒让德记号)

时间: 2024-08-08 02:34:45

uva 10831 - Gerg's Cake(勒让德记号)的相关文章

UVA 10831 - Gerg&#39;s Cake(数论)

UVA 10831 - Gerg's Cake 题目链接 题意:说白了就是给定a, p,问有没有存在x^2 % p = a的解 思路:求出勒让德标记,判断如果大于等于0,就是有解,小于0无解 代码: #include <stdio.h> #include <string.h> long long a, p; long long pow_mod(long long x, long long k, long long mod) { long long ans = 1; while (k

uva 10831 - Gerg&amp;#39;s Cake(勒让德符号)

题目链接:uva 10831 - Gerg's Cake 题目大意:给定a和p.p为素数,问说是否存在x,使得x2≡a%p 解题思路:勒让德记号,推断ap?12≡1%p #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; ll pow_mod (ll a, ll n, ll mod) { ll ans = 1; while

优质题表(机密版)

转载请注明出处:http://www.cnblogs.com/dashuzhilin/p/4556803.html 思维题: poj 1528 poj 1597 poj 2538 poj 2608 poj 2612 poj 2361 poj 2339 poj 2664 uva 10894 uva 10921   uva 10922   uva 10929 uva 10931   uva 10800   uva 10878 uva 10976   uva 10323   uva 201 poj 2

[UVA] 10167 - Birthday Cake

 Problem G. Birthday Cake  Background Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them.Now we put the cake onto a Descartes coordinate. Its center is at (0,0), and the cake's length of radius is 100. There are 2N

Brute Force --- UVA 10167: Birthday Cake

 Problem G. Birthday Cake  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=13&problem=1108&mosmsg=Submission+received+with+ID+14413715 Mean: http://luckycat.kshs.kh.edu.tw/

uva 10167 Birthday Cake(暴力/枚举)

uva 10167 Birthday Cake Background Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them.Now we put the cake onto a Descartes coordinate. Its center is at (0,0), and the cake's length of radius is 100. There are 2N (N

UVa 1629 Cake slicing (记忆化搜索)

题意:一个矩形蛋糕上有好多个樱桃,现在要做的就是切割最少的距离,切出矩形形状的小蛋糕,让每个蛋糕上都有一个樱桃,问最少切割距离是多少. 析:很容易知道是记忆化搜索,我们用dp[u][d][l][r]来表示,上界是u,下界是d,左边是l,右边是r,然后不断切割,不过要注意切的时候是按缝隙切, 缝隙多一条,那么我们可以补上一条,用0来补齐,然后就进行计算就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #in

UVA Birthday Cake (一条直线平分草莓)

 Problem G. Birthday Cake  Background Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them.Now we put the cake onto a Descartes coordinate. Its center is at (0,0), and the cake's length of radius is 100. There are 2N

UVA 1629 Cake slicing

题解: 记忆化搜索 如何判断每个区域只有一个点?转化为区间和,只要区间和为1即可 代码: #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define LL long long #define CLR(x) memset(x,0,sizeof x) #define MC(x,y) memcpy(x,