UtilityToolsLab

© 2026 UtilityToolsLab. Built and maintained by the UtilityToolsLab Team.

Free eBooks·About·Changelog·Privacy Policy·Terms of Service·Report a bug
HomeCompetitive ProgrammingExtended GCD

Related Tools

Code FormatterMatrix GeneratorComplexity CalcBit VisualizerBitmask PlannerPrime FactorsMEX CalcInterval MergerMatrix RotationPBDS GeneratorSegment TreeGraph VisualizerStress TesterModulo CalcConvex HullPath FinderOffline JudgeBig-O AnalyzerCombinatoricsDP Table BuilderSieve VisualizerBinary SearchSorting VisualizerSparse Table RMQUnion-Find DSU

Extended GCD & Bezout Calculator

Compute GCD, LCM, and Bezout coefficients x, y for ax+by=gcd with a full Euclidean step trace and modular inverse — BigInt-exact up to 10¹⁸.

You Might Also Like

All Competitive Programming

Code Formatter

Format and beautify C++, Java, and Python code with consistent indentation. 100% client-side — no code leaves your browser.

Matrix Generator

Generate grid/matrix inputs for competitive programming. Random, zeros, identity, or sequential fill. Outputs in multiple formats.

Complexity Calc

Estimate Big-O operations and runtime for any N. Interactive reference table for all complexity classes from O(1) to O(N!).

Bit Visualizer

Toggle 32/64-bit grids. Click bits to flip them live and see decimal recalculate. Shows popcountll, clzll, ctzll, and MSB instantly.

Paste two non-negative integers and the Extended GCD & Bezout Calculator runs the Extended Euclidean Algorithm, producing the GCD, the LCM, and the Bezout coefficients x and y that satisfy a·x + b·y = gcd(a, b). Every step of the Euclidean division chain is shown in a table so you can follow the back-substitution by hand, useful when a contest problem asks you to prove the identity, not just quote the answer.

Competitive programmers reach for this algorithm in two situations: verifying that a modular inverse exists before computing it (the inverse only exists when gcd = 1), and solving linear Diophantine equations ax + by = c, where a solution exists exactly when gcd(a, b) divides c. All arithmetic uses JavaScript BigInt, so inputs up to 10¹⁸ are exact with no floating-point rounding and no silent overflow.

Worked Example: Two Numbers, One Step Trace

Load the sample (252 and 198) and the step trace shows four division rows: 252 = 1·198 + 54, then 198 = 3·54 + 36, then 54 = 1·36 + 18, then 36 = 2·18 + 0. The remainder hits zero at 18, so GCD = 18. Back-substitution yields x = −3, y = 4, verified by 252·(−3) + 198·4 = −756 + 792 = 18. The identity check row confirms this automatically. LCM = 252 × 198 / 18 = 2,772. Because gcd = 18 is not 1, no modular inverse exists for this pair, and the tool reports "An inverse exists only when gcd(a, b) = 1" rather than a silent wrong answer.

What Each Output Field Means

  • Bezout x and y are one particular solution to ax + by = gcd. The full solution set is x + k·(b/gcd), y minus k·(a/gcd) for any integer k.
  • Modular inverse of a mod b equals x reduced into [0, b), shown only when gcd(a, b) = 1. Plugging it back in gives a·inv ≡ 1 (mod b) exactly.
  • LCM(a, b) is computed as (a / gcd) * b to keep intermediate values small. Multiplying a * b first before dividing overflows for inputs near 10¹⁸.
  • The Euclidean step count is O(log min(a, b)). For a = 10¹⁸ and b = 10¹⁸ minus 1, the worst case, the trace reaches zero in at most 87 steps.

Edge Cases and When the Tool Cannot Produce a Result

Try a = 6, b = 9. GCD = 3, so no modular inverse exists. The tool shows the exact guard message rather than computing a meaningless result; the bundled C++ snippet returns -1 in this case, which is the convention most competitive-programming templates follow. Entering 0 for both values triggers a separate guard: gcd(0, 0) is undefined, so the algorithm has no meaningful output and the tool says so immediately. The step table is not shown for this input because there are no division steps to trace.

GCD(a, b)
18
LCM(a, b)
2772
Bezout x → 252·x + 198·y = 18
4
Bezout y → 252·x + 198·y = 18
-5
Identity check (a·x + b·y)
18
No modular inverse — gcd(252, 198) = 18 ≠ 1. An inverse exists only when gcd(a, b) = 1.

Euclidean Step Trace

Stepabq = a÷br = a mod b
1252198154
219854336
35436118
4361820 ← gcd

C++ — Extended GCD Template

// Extended GCD — returns gcd(a,b); sets x,y so that a*x + b*y = gcd
long long extgcd(long long a, long long b, long long &x, long long &y) {
    if (b == 0) { x = 1; y = 0; return a; }
    long long x1, y1;
    long long g = extgcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - (a / b) * y1;
    return g;
}

// Usage
long long x, y;
long long g = extgcd(a, b, x, y);
// Now: a*x + b*y == g
// Modular inverse of a mod m (only if g == 1):
long long inv = (x % m + m) % m;