Skip to content

Commit c3f2f2c

Browse files
committed
Added probable sqrt
1 parent 5b62322 commit c3f2f2c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

math/bigint.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
// - SPOJ MUL, VFMUL: Multiplication.
2424
// - SPOJ FDIV, VFDIV: Division.
2525

26+
#include <bits/stdc++.h>
27+
28+
using namespace std;
29+
30+
using ll = long long;
31+
2632
const int BASE_DIGITS = 9;
2733
const int BASE = 1000000000;
2834

@@ -594,3 +600,27 @@ struct BigInt {
594600
return res / norm;
595601
}
596602
};
603+
604+
bool is_sqrt(BigInt n) {
605+
static constexpr int primes[] = {3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149};
606+
607+
auto fexp = [] (ll a, ll b, ll c) {
608+
ll ans= 1;
609+
while (b) {
610+
if (b&1) ans = a*ans%c;
611+
a = a*a%c;
612+
b /= 2;
613+
}
614+
return ans;
615+
};
616+
617+
for (int p : primes) {
618+
ll x = n % p;
619+
620+
if (x == 0) continue;
621+
622+
if (fexp(x, (p-1)/2, p) != 1) return false;
623+
}
624+
625+
return true;
626+
}

0 commit comments

Comments
 (0)