The problem is from codeforces: http://codeforces.com/problemset/problem/197/B
It has been a while since last time I submitted Python solutions to codeforces. This time, I pick a math problem that can remind me of the ‘limit’ which was taught in high school.
The inputs are two known polynomials P and Q and their degrees are known. The output should be the limit of P / Q. It seems difficult at first, but when you think for a second, you will realise how easy the solution is. The most important task is to check the coefficients P(0) and Q(0) of the highest degree. If degree of P is larger than Q, the answer is either ‘Infinity’ or ‘-Infinity’ depending on the signs. If degree of P is smaller than Q, the answer is ‘0/1’, otherwise, you will need to compute the GCD (Greatest Common Divisor) of P(0) and Q(0), and the answer will be P(0) / G divided by Q(0) / G.
#!/usr/bin/env python from sys import stdin fun = lambda: map(int, stdin.readline().split(" ")) n, m = fun() a = fun() b = fun() def gcd(a, b): while b: a, b = b, a % b return a if n > m: if a[0] * b[0] > 0: print "Infinity" else: print "-Infinity" elif n == m: t = gcd(a[0], b[0]) print "%d/%d" % (a[0] / t, b[0] / t) else: print "0/1"
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: Customised HTML pages for Your HTTP Server
Next Post: Advanced DNS: Terms Explained