Problem:


Score : 300 points

Problem Statement

AtCoDeer the deer is seeing a quick report of election results on TV.
Two candidates are standing for the election: Takahashi and Aoki.
The report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes.
AtCoDeer has checked the report N times, and when he checked it for the i-th (1≦i≦N) time, the ratio was T_i:A_i.
It is known that each candidate had at least one vote when he checked the report for the first time.

Find the minimum possible total number of votes obtained by the two candidates when he checked the report for the N-th time.
It can be assumed that the number of votes obtained by each candidate never decreases.

Constraints

1≦N≦1000

1≦T_i,A_i≦1000 (1≦i≦N)

T_i and A_i (1≦i≦N) are coprime.

It is guaranteed that the correct answer is at most 10^{18}.

Input

The input is given from Standard Input in the following format:

N
T_1 A_1
T_2 A_2
:
T_N A_N

Output

Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the N-th time.

Sample Input 1

3
2 3
1 1
3 2

Sample Output 1

10

When the numbers of votes obtained by the two candidates change as 2,3 → 3,3 → 6,4, the total number of votes at the end is 10, which is the minimum possible number.

Sample Input 2

4
1 1
1 1
1 5
1 100

Sample Output 2

101

It is possible that neither candidate obtained a vote between the moment when he checked the report, and the moment when he checked it for the next time.

Sample Input 3

5
3 10
48 17
31 199
231 23
3 2

Sample Output 3

6930



Sample input:
3
2 3
1 1
3 2


Sample output:
10

Submission status: Runtime Error

Solution code:
import math
from decimal import *

N = int(input())
_input = []
for i in range(N):
    _input.append(list(map(int,input().split())))

total_T = 1
total_A = 1

for t,a in _input:
    #n = max(math.ceil(total_T/t),math.ceil(total_A/a))
    n = max(-(-t//Ti), -(-a//Ai))
    total_T = int(t*n)
    total_A = int(a*n)

print(total_A+total_T)

Submission status: Runtime Error

Solution code:
n = int(input())
ts, as = map(int, input().split())
for _ in range(n-1):
  t, a = map(int, input().split())
  m = max((ts-1)//t, (as-1)//a)+1
  ts = m*t
  as = m*a
print(ts+as)

Submission status: Runtime Error

Solution code:
from math import gcd
n=int(input())
a,b=map(int,input().split())
d=a+b
for i in range(n-1):
    e,f=map(int,input().split())
    d=d*(e+f)//gcd(d,e+f)
print(d)

Submission status: Runtime Error

Solution code:
from itertools import gcd
n=int(input())
a,b=map(int,input().split())
d=a+b
for i in range(n-1):
    e,f=map(int,input().split())
    d=d*(e+f)//gcd(d,e+f)
print(d)

Submission status: Runtime Error

Solution code:
n = int(input())
t,a = [],[]
for i in range(n):
    tmp = [int(i) for i in input().split()]
    t.append(tmp[0])
    a.append(tmp[1])

p,q = 0,0
for zip(x,y) in t,a:
    tmp = max(p//x,q//y)
    p = x*tmp 
    q = y*tmp
print(p+q)

Submission status: Runtime Error

Solution code:
n = int(input())
t,a = [],[]
for i in range(n):
    tmp = [int(i) for i in input().split()]
    t.append(tmp[0])
    a.append(tmp[1])

p,q = 0,0
for x,y in t,a:
    tmp = max(p//x,q//y)
    p = x*tmp 
    q = y*tmp
print(p+q)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
ans = 2
tp, ap = 0, 0
for i in range(N):#ans以上での比からの最小値でans更新,
    #A+Tのn倍、ただしn>=ans/(t+a)
    t, a = map(int, input().split())
    k = max(math.ceil(tp/t), math.ceil(ap/a))
    t, a = t*k, a*k
    c = math.ceil(ans/(t+a))
    while True:#ansが前回のans以上になるようにする
        if (t+a)*c >= ans:
            ans = (t+a)*c
            break
        c += 1
    tp, ap = t*c, a*c
print(ans)

Submission status: Runtime Error

Solution code:
from math import ceil
from decimal import *
n=int(input())
prea=1.0
pret=1.0
for i in range(n):
    
    t,a=map(Decimal,input().split())
    i=max(ceil(prea/a),ceil(pret/t))
    pret,prea=t*i,a*i

print(pret+prea)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import itertools

n = input()
input_lines = list(input())

pairs = [input_line.split(' ') for input_line in input_lines]
t_i = [pair[0] for pair in pairs]
a_i = [pair[1] for pair in pairs]

def satisfy_conditions(total_votes):

    votes_t_i = total_votes * a_i[-i]
    votes_a_i = total_votes * t_i[-i]

    for i in range(1, n+1):

        constraint = (votes_t_i, votes_a_i)
        possible_combination = is_possible(constraint, a_i[-i], t_i[-i])
        if possible_combination is None:
            return False
        else:
            votes_t_i = possible_combination[0]
            votes_a_i = possible_combination[1]

    return True

def is_possible(constraint, t_i, a_i):
    votes_t_i_plus_1 = constraint[0]
    votes_a_i_plus_1 = constraint[1]

    combinations = list(itertools.product(list(range(1, votes_t_i_plus_1 + 1)),
                                          list(range(1, votes_a_i_plus_1 + 1))))
    
    combinations.reverse()

    for combination in combinations:
        if combination[0] / combination[1] == t_i / a_i:
            return (combination[0], combination[1])

    return None


max_iter = 1000000
answer = None
unit = t_i[-1] + a_i[-1]
for k in range(1, max_iter):
    if satisfy_conditions(unit * k):
        answer =  unit * k
        break


print(answer)

Submission status: Runtime Error

Solution code:
#    X2 
# X1 X0 X4 X5 
#    X3
#
# C0    C1
#    X0 
# C3    C2
#
# C1    C0
#    5X 
# C2    C3
#
N = int(raw_input())
# N = 6
# N = 8

C_list = []
for n in range(N):
    C_list.append(map(int, raw_input().split()))


"""
C_list = [
    [0, 1, 2, 3], 
    [0, 4, 6, 1], 
    [1, 6, 7, 2], 
    [2, 7, 5, 3], 
    [6, 4, 5, 7], 
    [4, 0, 3, 5]
]

C_list = [
    [0, 0, 0, 0], 
    [0, 0, 1, 1], 
    [0, 1, 0, 1], 
    [0, 1, 1, 0], 
    [1, 0, 0, 1], 
    [1, 0, 1, 0], 
    [1, 1, 0, 0], 
    [1, 1, 1, 1]
]

C_list = [
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0]
]
"""
res = 0

def rotate(A, n):
    if n == 0:
        return A
    elif n == 1:
        return [A[1], A[2], A[3], A[0]]
    elif n == 2:
        return [A[2], A[3], A[0], A[1]]
    elif n == 3:
        return [A[3], A[0], A[1], A[2]]
    

C_list_1 = [rotate(C, 1) for C in C_list]
C_list_2 = [rotate(C, 2) for C in C_list]
C_list_3 = [rotate(C, 3) for C in C_list]


for i in range(N-1):
    X0_ = C_list[i]
    for j in range(i+1, N):
        X5 = [C_list[j][1], C_list[j][0], C_list[j][3], C_list[j][2]]
        for k in range(4):
            X0 = rotate(X0_, k)
            X1 = [X5[0], X5[1], X0[1], X0[0]]
            X2 = [X5[1], X5[2], X0[2], X0[1]]
            X3 = [X5[2], X5[3], X0[3], X0[2]]
            X4 = [X5[3], X5[0], X0[0], X0[3]]

            # drop duplicate
            c1 = 0
            c2 = 0
            c3 = 0
            c4 = 0
            
            if X2 in [rotate(X1, l) for l in range(4)]:
                c2 += 1
            if X3 in [rotate(X1, l) for l in range(4)]:
                c3 += 1
            if X3 in [rotate(X2, l) for l in range(4)]:
                c3 += 1
            if X4 in [rotate(X1, l) for l in range(4)]:
                c4 += 1
            if X4 in [rotate(X2, l) for l in range(4)]:
                c4 += 1
            if X4 in [rotate(X3, l) for l in range(4)]:
                c4 += 1
            
            # count how many fits
            r1 = 0
            r1 += max(sum([C_list[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r1 += max(sum([C_list_1[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r1 += max(sum([C_list_2[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r1 += max(sum([C_list_3[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r2 = 0
            r2 += max(sum([C_list[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r2 += max(sum([C_list_1[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r2 += max(sum([C_list_2[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r2 += max(sum([C_list_3[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r3 = 0
            r3 += max(sum([C_list[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r3 += max(sum([C_list_1[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r3 += max(sum([C_list_2[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r3 += max(sum([C_list_3[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r4 = 0
            r4 += max(sum([C_list[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            r4 += max(sum([C_list_1[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            r4 += max(sum([C_list_2[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            r4 += max(sum([C_list_3[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            res += r1*r2*r3*r4
            
print res