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-24 04:44:16

uva 10831 - Gerg&#39;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&#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

UVA 11825 - Hackers&amp;#39; Crackdown 状态压缩 dp 枚举子集

UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:11825 - Hackers' Crackdown 题意: 有一个由编号0~n-1的n台计算机组成的网络,一共同拥有n种服务,每台计算机上都执行着所有服务,对于每台计算机,你能够选择停止一项服务,这个行为会导致与这台计算机和与他相连的其它计算机上的这项服务都停止(原来已经停止的继续保持停止状态). 求最多能使多少个服务瘫痪(即没有不论什么一台计算机在执行这项服务). 分析: 题目说白了.就

【UVA】434-Matty&amp;#39;s Blocks

一道非常easy想复杂的题,给出主视图和右视图,计算最少能用几个正方体组成相应的视图,以及最多还能加几块正方体. 求最多加入事实上就是求出最多的正方体数减去最少的,主要就是最少的不好求. 一開始各种模拟就是不正确,之后发现,仅仅须要统计两个视图的高度个数就能够了(简直了) 14390495 434 Matty's Blocks Accepted C++ 0.016 2014-10-21 11:35:11 #include<cstdio> #include<cstring> #inc

uva 11825 Hackers&amp;#39; Crackdown (状压dp,子集枚举)

题目链接:uva 11825 题意: 你是一个黑客,侵入了n台计算机(每台计算机有同样的n种服务),对每台计算机,你能够选择终止一项服务,则他与其相邻的这项服务都终止.你的目标是让很多其它的服务瘫痪(没有计算机有该项服务). 思路:(见大白70页,我的方程与大白不同) 把n个集合P1.P2.Pn分成尽量多的组,使得每组中全部集合的并集等于全集,这里的集合Pi是计算机i及其相邻计算机的集合,用cover[i]表示若干Pi的集合S中全部集合的并集,dp[s]表示子集s最多能够分成多少组,则 假设co

UVA 11774 - Doom&amp;#39;s Day(规律)

UVA 11774 - Doom's Day 题目链接 题意:给定一个3^n*3^m的矩阵,要求每次按行优先取出,按列优先放回,问几次能回复原状 思路:没想到怎么推理,找规律答案是(n + m) / gcd(n, m),在topcoder上看到一个证明,例如以下: We can associate at each cell a base 3-number, the log3(R) most significant digits is the index of the row of the cel

UVA - 434 Matty&amp;#39;s Blocks

题意:给你正视和側视图,求最多多少个,最少多少个 思路:贪心的思想.求最少的时候:由于能够想象着移动,尽量让两个视图的重叠.所以我们统计每一个视图不同高度的个数.然后计算.至于的话.就是每次拿正视图的高度去匹配側视求最大 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int MAXN = 1000

UVA 10026 Shoemaker&amp;#39;s Problem

Shoemaker's Problem Shoemaker has N jobs (orders from customers) which he must make. Shoemaker can work on only one job in each day. For each ith job, it is known the integer Ti (1<=Ti<=1000), the time in days it takes the shoemaker to finish the jo

UVa 11790 - Murcia&amp;#39;s Skyline

称号:给你一个行长度的建设和高度,我们祈求最长的和下降的高度. 分析:dp,最大上升子. 说明:具有长度,不能直接优化队列单调. #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int h[2000],w[2000],u[2000],l[2000]; int main() { int T,n; while (~scanf("%d",&T