Gcd dynamic programming