POJ 1808 + Ural 1132 平方剩余

链接:http://poj.org/problem?id=1808

http://acm.timus.ru/problem.aspx?space=1&num=1132

题意:两道题都是模板题,第一个是判断是否有平方剩余,第二个是计算平方剩余。

思路:平方剩余就是给定a,n(n为质数) 问 x^2 ≡ a (mod n) 是否有解,可以用a^((n - 1)/2) ≡ ±1(mod n) 当为1是二次剩余,为-1是非二次剩余。

资料:http://blog.csdn.net/acdreamers/article/details/10182281

参考资料中基本正确,注意区分好同余和相等的关系。

代码:

LL w;
LL pow_mod(LL aa,LL ii,LL nn)
{
    if(ii==0)
        return 1%nn;
    LL temp=pow_mod(aa,ii>>1,nn);
    temp=temp*temp%nn;
    if(ii&1)
        temp=temp*aa%nn;
    return temp;
}
struct comp
{
    LL r,i;
};
comp multi(comp a, comp b, LL m)
    {
        comp ans;
        ans.r = (a.r * b.r % m + a.i * b.i % m * w % m) % m;
        ans.i = (a.r * b.i % m + a.i * b.r% m) % m;
        return ans;
    }
    comp pow_mod(comp a, LL b,LL m)
    {
        comp ans;
        ans.r = 1;
        ans.i = 0;
        while(b)
        {
            if(b & 1)
            {
                ans = multi(ans, a, m);
                b--;
            }
            b >>= 1;
            a = multi(a, a, m);
        }
        return ans;
    }
LL Legendre(LL a, LL p)
{
    return pow_mod(a, (p-1)>>1, p);
}
LL Quadratic_residue(LL n,LL p)
{
    if(p==2)
        return 1;
    if (Legendre(n, p) + 1 == p)
        return -1;
    LL a = -1, t;
    while(1)
    {
        a = rand() % p;
        t = a * a - n;
        w=(t%p+p)%p;
        if(Legendre(w, p) + 1 == p) break;
    }
    comp temp;
    temp.r=a;
    temp.i=1;
    comp ans;
    ans=pow_mod(temp,(p+1)>>1,p);
    return ans.r;
}

POJ 1808 + Ural 1132 平方剩余

时间: 2024-08-24 11:46:24

POJ 1808 + Ural 1132 平方剩余的相关文章

poj 1808 Quadratic Residues 【平方剩余】【数论】

题目链接:http://poj.org/problem?id=1808 题目大意:给你T组数据,每组数据一个a一个n,判断 x^2  ≡  a ( mod  n ) 能否成立.成立则输出1否则输出-1. 一个简单的平方剩余,只用判断能否有解即可. #include<stdio.h> #define LL long long LL pow_mod(LL a, LL n, LL mod) { LL res = 1; while(n) { if (n & 1) res = res * a %

平方剩余 (poj 1808)

题意: 判断平方剩余,即判断(x^2)%p=a是否有解. 限制: |a| <= 1e9 && a % p !=0; 2 < p < 1e9 && p为奇素数. 思路: 用欧拉准则计算勒让德符号(用来判断平方剩余) 代码,内含求平方剩余的解函数

poj 2352 &amp; Ural 1028 数星星 题解

一道水题,由于x坐标递增y坐标也递增于是前缀和统计即可,用树状数组实现. #include<bits/stdc++.h> using namespace std; const int maxn=15010; const int maxx=32010; inline long long read(){ long long x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar();

As FancyCoder

路漫漫其修远兮,吾将上下而求索.搞OI的时候,我用这句话勉励自己. 漫漫六年OI路,无数个对着电脑屏幕的日日夜夜,无数次清脆急促的键盘敲击声,对OI的那份热爱给了我从未有过的执着. 有发自内心的高兴,有真真切切的泪水. 这个是我自己做的一份OI笔记吧.算是一份算法清单吧,外加一些题目的总结. 不过由于我水平有限,很多东西也不甚懂,所以可能不会特别全面. 不过觉得可能对后人有用,就放上来了.博君一笑. 还有部分自己的代码以及一份在学校里曾经用过的讲稿(自己感觉还是蛮好的).也顺便放上来了. 下面是

POJ 2774 Long Long Message &amp;&amp; URAL 1517. Freedom of Choice(求最长重复子序列)

两个题目意思差不多,都是让求最长公共子串,只不过poj那个让输出长度,而URAL那个让输出一个任意的最长的子串. 解体思路: Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 22313   Accepted: 9145 Case Time Limit: 1000MS Description The little cat is majoring in physics in the cap

后缀数组 POJ 3974 Palindrome &amp;&amp; URAL 1297 Palindrome

题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串中心位置,RMQ询问LCP = min (height[rank[l]+1] to height[rank[r]]),注意的是RMQ传入参数最好是后缀的位置,因为它们在树上的顺序未知,且左边还要+1. #include <cstdio> #include <algorithm> #in

平方剩余

平方剩余 POJ:1808 链接:http://poj.org/problem?id=1808 题意:给定a,n(n为质数) 问 x^2 ≡ a (mod n) 是否有解 可以用a^((n - 1)/2) ≡ ±1(mod n) 当为1是二次剩余,为-1是非二次剩余 但上述方法仅仅是判断是否有解,下面的方法能够求最小整数解 Ural(Timus) 1132 链接: http://acm.timus.ru/problem.aspx?space=1&num=1132 题意:给定a,n(n为质数) 问

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348