Timus 1132 Square Root(二次剩余 解法2)

不理解,背板子

#include<cstdio>

using namespace std;

int Pow(int a,int b,int p)
{
    int res=1;
    for(;b;a=1LL*a*a%p,b>>=1)
        if(b&1) res=1LL*a*res%p;
    return res;
}

bool Legendre(int a,int p)
{
    return Pow(a,p-1>>1,p)==1;
}

void modsqr(int a,int p)
{
    int x;
    int i,k,b;
    if(p==2) x=a%p;
    else if(p%4==3) x=Pow(a,p+1>>2,p);
    else
    {
        for(b=1;Legendre(b,p);++b);
        i=p-1>>1;
        k=0;
        do
        {
            i>>=1;
            k>>=1;
            if(!((1LL*Pow(a,i,p)*Pow(b,k,p)+1)%p)) k+=p-1>>1;
        }while(!(i&1));
        x=1LL*Pow(a,i+1>>1,p)*Pow(b,k>>1,p)%p;
    }
    if(p-x<x) x=p-x;
    if(x==p-x) printf("%d\n",x);
    else printf("%d %d\n",x,p-x);
}        

int main()
{
    freopen("data.txt","r",stdin);
    freopen("aa.txt","w",stdout);
    int T;
    scanf("%d",&T);
    int a,n;
    while(T--)
    {
        scanf("%d%d",&a,&n);
        a%=n;
        if(!Legendre(a,n))
        {
            puts("No root");
            continue;
        }
        modsqr(a,n);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8969735.html

时间: 2024-11-06 10:56:21

Timus 1132 Square Root(二次剩余 解法2)的相关文章

Project Euler 80:Square root digital expansion 平方根数字展开

Square root digital expansion It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all. The square root of two i

UVA 10023 - Square root(手算平方根)

题目:UVA 10023 - Square root(手算平方根) 题目链接 题目大意:求给定的一个数的平方根. 解题思路:用二分但是这个数太大了,就超时了.看题接后发现需要用一种手算平方根的算法. 算法: 先判断这个数是不是偶数位,是的话就第一次取前面的两位数,不是的话第一次就只取前面的一位数来作为被除数.接下来就是两位两位为一节来计算. 用前一次的计算结果乘上20+一个个位数a再乘上这个a,找到最大的a使得这个式子的结果不大于被除数. 被除数减去这个结果然后再在尾巴接上那个大数的接下来两位作

Square Root

Square RootWhen the square root functional configuration is selected, a simplified CORDIC algorithm isused to calculate the positive square root of the input. The input, X_IN, and the output,X_OUT, are always positive and are both expressed as either

Square Root of Permutation - CF612E

Description A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i

欧拉工程第57题:Square root convergents

题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; class level57{ void solve0(){ /*** a a+2b --- ------- b a+b ***/ int count = 0; BigInteger a=BigInteger.va

Project Euler #80: Square root digital expansion

1 from decimal import getcontext, Decimal 2 3 4 def main(): 5 n = int(raw_input()) 6 p = int(raw_input()) 7 8 getcontext().prec = p+10 # 扩大精度,保证接过 9 sum = 0 10 11 for i in range(1,n+1): 12 nTemp = Decimal(i).sqrt() 13 if nTemp._isinteger() : # 自生函数的判

使用牛顿-拉弗森法定义平方根函数(Newton-Raphson method Square Root Python)

牛顿法(Newton's method)又称为牛顿-拉弗森法(Newton-Raphson method),是一种近似求解实数方程式的方法.(注:Joseph Raphson在1690年出版的<一般方程分析>中提出了后来被称为"牛顿-拉弗森法"的数学方法,牛顿于1671年写成的著作<流数法>中亦包括了这个方法,但该书在1736年才出版.) 之前的一篇博客中提到的二分法可以求解方根,而使用牛顿迭代法可以更快地解出方根.现在,人们使用的计算器里面大多数都是运用的牛顿

UVA - 10023 - Square root (模拟手算开方)

题目传送:UVA - 10023 思路:模拟手算开方,不想用c/c++,感觉太麻烦了,就直接用的java里的BigInteger类来写的,写了好久......Java还是得看看书呀,手算开方参考的这里 AC代码: import java.util.Scanner; import java.math.BigInteger; public class Main { static void fun(BigInteger x) { String str; str = x.toString(); str

Timus Online Judge 1001. Reverse Root

Input The input stream contains a set of integer numbers Ai (0 ≤ Ai ≤ 1018). The numbers are separated by any number of spaces and line breaks. A size of the input stream does not exceed 256 KB. Output For each number Ai from the last one till the fi