33ms Runtime in C. Modular Arithmetic. GCD and Factor Removal
ApproachModular Arithmetic:We use two modulus values (2 and 5)GCD and Factor Removal:We use the greatest common divisor (GCD) to simplify numbers where necessar



# Approach
<!-- Describe your approach to solving the problem. -->
**Modular Arithmetic:**

We use two modulus values (2 and 5)


**GCD and Factor Removal:**

We use the greatest common divisor (GCD) to simplify numbers where necessary, and we remove factors of 2 and 5 from the numbers to reduce them modulo the target value.

**Key Insight:**

We break the problem into two independent tests: one for modulus 2 and another for modulus 5.

# Complexity
- Time complexity: **O(n)**
- Space complexity: **O(1)**
# Code
```c [] 
// Computes the greatest common divisor of a and b.
static inline int32_t gcd(int32_t a, int32_t b) {
    while (b) {
        a ^= b ^= a ^= b; // XOR trick to swap a and b
        b %= a;
    }
    return a;
}

/* Removes factors of mod from a.
Returns in *r the value a reduced modulo */
static inline void cal(int32_t a, int32_t mod, int32_t *r, int32_t *cnt) {
    int32_t count = 0;
    while (a > 0 && a % mod == 0) {
        count++;
        a /= mod;
    }
    *r = a % mod;
    *cnt = count;
}

// Computes the modular inverse of a modulo mod.
static inline int32_t modinv(int32_t a, int32_t mod) {
    for (int32_t i = 1; i < mod; i++) {
        if ((a * i) % mod == 1)
            return i;
    }
    return 0;
}

/* Performs the test for a given modulus(2 or 5) 
based on the difference between weighted sums. */
static bool test(int32_t mod, const char *s) {
    int32_t n = strlen(s);
    int32_t res = 0, r = 1, c = 0;
    for (int32_t i = 0; i < n - 1; i++) {
        if (c == 0) {
            res = (res + r * ((s[i] - '0') - (s[i + 1] - '0'))) % mod;
            if (res < 0) res += mod;
        }
        int32_t rr, cc;
        cal(n - 2 - i, mod, &rr, &cc);
        r = (r * rr) % mod;
        c += cc;
        cal(i + 1, mod, &rr, &cc);
        r = (r * modinv(rr, mod)) % mod;
        c -= cc;
    }
    return (res % mod) == 0;
}

/* Checks if the final two digits are the same.
 Returns true if they are the same, false otherwise. */
static bool hasSameDigits(const char *s) {
    return test(2, s) && test(5, s);
}

```
['C']