Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1019
Solution: lcm (a, B) = a * B/gcd (a, B)
Reflection: at the beginning of the submission, WA thought it was overflow, so it was changed to long or WA, so we didn't understand it, so we went to view discuss and found that we should write it like this.
Lcm (a, B) = a * gcd (a, B) * B; it is used to prevent the overflow of a multiplied by B. Note !!!! So we need to divide and multiply.
# Include <stdio. h> int gcd (int a, int B) {int t, r; if (a <B) {t = a; a = B; B = t ;} r = a % B; while (r! = 0) {a = B; B = r; r = a % B;} return B;} int main () {int ncase; int n; int; scanf ("% d", & ncase); while (ncase --) {long s = 1; scanf ("% d", & n); while (n --) {scanf ("% d", & a); s = s/gcd (s, a) * a;} printf ("% ld \ n", s );}}
Hangdian 1019 Least Common Multiple [minimum public Multiple]