Discrete Square Roots UVALive

a≡b(mod n)的含义是“a和b除以n的余数相同”,其充要条件是“a-b是n的整数倍”;

求所有满足条件r^2=x(mod m)的r

题目已经给定了一个初始的r,x,m

 

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10010, INF = 0x7fffffff;
LL N, mx, r;
set<LL> s;
LL gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if(!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        gcd(b, a%b, d, y, x);
        y -= x*(a/b);
    }
}

void solve(LL a, LL b)
{
    LL x, y, d;
    gcd(a, b, d, x, y);
    if((2*r) % d) return;
    x *= 2*r/d;
    x = (x % (b/d) + (b/d)) % (b/d);;
    LL r1 = x*a - r;
    while(r1 < N)
    {
        if(r1 >= 0 && (r1*r1) % N == mx)
            s.insert(r1);
        r1 += a*(b/d);
    }
}

int main()
{
    int kase = 0;
    while(cin>> mx >> N >> r && mx+N+r)
    {
        s.clear();
        for(LL i=1; i<=sqrt(N + 0.5); i++)
        {

            if(N % i) continue;
            LL a = i, b = N/i;
            solve(a, b);
            solve(b, a);
        }
        printf("Case %d:",++kase);
        for(set<LL>::iterator it=s.begin(); it!=s.end(); it++)
        {

            printf(" %lld",*it);
        }
        cout<<endl;

    }


    return 0;
}
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
原文地址:https://www.cnblogs.com/WTSRUVF/p/9327584.html