Skip to content

Commit

Permalink
add FastGCD
Browse files Browse the repository at this point in the history
  • Loading branch information
tatyam-prime committed May 31, 2024
1 parent 721cf07 commit e491b32
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
path: |
.verify-helper/cache/
~/.cache/online-judge-tools/library-checker-problems/
~/.local/share/online-judge-tools/
key: cache-judge-data

- name: Run tests
Expand Down
11 changes: 11 additions & 0 deletions src/math/FastGCD.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
u64 ctz(u64 x) { return countr_zero(x); }
u64 binary_gcd(u64 x, u64 y) {
if(!x || !y) return x | y;
u64 n = ctz(x), m = ctz(y);
x >>= n, y >>= m;
while(x != y) {
if(x > y) x = (x - y) >> ctz(x - y);
else y = (y - x) >> ctz(y - x);
}
return x << min(n, m);
}
13 changes: 13 additions & 0 deletions test/math/FastGCD.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include "test/template.hpp"
using u64 = uint64_t;
#include "src/math/FastGCD.hpp"

int main() {
mt19937_64 rnd;
rep(i, 0, (ll)1e7) {
u64 a = rnd(), b = rnd();
assert(gcd(a, b) == binary_gcd(a, b));
}
puts("Hello World");
}
2 changes: 1 addition & 1 deletion test/template.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ int main() {
assert(chmax(a, 1) == 1);
assert(chmax(a, 1) == 0);
assert(chmin(a, 0) == 1);
cout << "Hello World" << endl;
puts("Hello World");
}

0 comments on commit e491b32

Please sign in to comment.