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: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
TA = [LIST() for _ in range(N)]

T = TA[0][0]
A = TA[0][1]

for t, a in TA[1:]:
	p = max((T+t-1)//t, (A+a-1)//a)
	T = t*p
	A = a*p
	
print(T+A)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
TA = [LIST() for _ in range(N)]
vote_T = 1
vote_A = 1
for x, y in TA:
	n = max((vote_T+x-1)//x, (vote_A+y-1)//y)
	vote_T = x * n
	vote_A = y * n
print(vote_A + vote_T)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
TA = [LIST() for _ in range(N)]
vote_T = 1
vote_A = 1
for x, y in TA:
	n = max(-(-vote_T // x), -(-vote_A // y))
	vote_T = x * n
	vote_A = y * n
print(vote_A + vote_T)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
TA = [LIST() for _ in range(N)]

T = TA[0][0]
A = TA[0][1]

for t, a in TA[1:]:
	p = max(T//t + (T%t!=0), A//a + (A%a!=0))
	T = t*p
	A = a*p
	
print(T+A)

Submission status: Accepted

Solution code:
n = int(input())
a,b = map(int,input().split())

for i in range(n-1):
    c,d = map(int,input().split())
    x = -min((-a)//c,(-b)//d)
    a,b = c*x,d*x
print(a + b)

Submission status: Accepted

Solution code:
N = int(input())
T = 1
A = 1
for i in range(N):
    Tk = [0,0]
    Ak = [0,0]
    Tn,An = map(int,input().rstrip().split(" "))
    if T % Tn == 0:
        Tk[0] = T
    else:
        Tk[0] = (T // Tn + 1) * Tn
    Ak[0] = (Tk[0] // Tn) * An
    if Ak[0] < A:
        Ak[0] = 10 ** 18 + 1

    if A % An == 0:
        Ak[1] = A
    else:
        Ak[1] = (A // An + 1) * An
    Tk[1] = (Ak[1] // An) * Tn
    if Tk[1] < T:
        Tk[1] = 10 ** 18 + 1
    
    if Tk[0] + Ak[0] <= Tk[1] + Ak[1]:
        T = Tk[0]
        A = Ak[0]
    else:
        T = Tk[1]
        A = Ak[1]
print(A + T)

Submission status: Accepted

Solution code:
N=int(input())
X,Y=map(int,input().split())
T,A=0,0
b,c=0,0
for i in range(N-1):
  T,A=map(int,input().split())
  b=((X+T-1)//T)*T
  c=(b//T)*A
  if c>=Y:
    X,Y=b,c
  else:
    Y=((Y+A-1)//A)*A
    X=(Y//A)*T
print(X+Y)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def main():
    # i回目 Ti : Ai
    N = int(input())
    T, A = [], []
    for i in range(N):
        t, a = map(int, input().split())
        T.append(t)
        A.append(a)

    num = [0, 0]
    for i, (t, a) in enumerate(zip(T, A)):
        # print(i, t, a, num)
        while True:
            if num[0] <= t and num[1] <= a:
                break
            if num[0] <= t:
                # print("B", t, a, num)
                c = -(-num[1]//a)
                a *= c
                t *= c
                continue
            if num[1] <= a:
                # print("C", t, a, num)
                c = -(-num[0]//t)
                199, 17
                a *= c
                t *= c
                continue
            c = max(-(-num[0]//t), -(-num[1]//a))
            # print("D", t, a, num, c)
            a *= c
            t *= c
            continue
        num[0] = t
        num[1] = a
        # print(a, t)
    print(num[0]+num[1])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
TA = [list(map(int, input().split())) for _ in range(N)]

T, A = 0, 0
for t, a in TA:
    if T == 0 and A == 0:
        T = t
        A = a
        continue
    if t == a:
        M = max(T, A)
        T = M
        A = M
    elif t < a:
        n = max((T // t) * t, (A // a) * t)
        while n < T or a * (n // t) < A:
            n += t
        T = n
        A = a * (T // t)
    else:
        n = max((A // a) * a, (T // t) * a)
        while n < A or t * (n // a) < T:
            n += a
        A = n
        T = t * (A // a)
print(A + T)

Submission status: Accepted

Solution code:
N = int(input())
takahashi = 1
aoki = 1
for i in range(N):
  T, A = map(int,input().split())
  n = max((takahashi + T - 1) // T, (aoki + A - 1) // A)
  takahashi = n*T
  aoki = n*A
print(takahashi+aoki)

Submission status: Accepted

Solution code:
N=int(input())
takahashi=1
aoki=1
for i in range(N):
  T,A=map(int,input().split())
  n=max((takahashi+T-1)//T,(aoki+A-1)//A)
  takahashi=n*T
  aoki=n*A
print(takahashi+aoki)

Submission status: Accepted

Solution code:
import math


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N = read_int()
    T, A = 1, 1
    for _ in range(N):
        Ti, Ai = read_ints()
        # T  A   T    A
        # Ti Ai  Ti*x Ai*x
        #
        # Ti*x >= T, Ai*x >= A, min x?
        # x >= T/Ti
        # x >= A/Ai
        if T%Ti == 0:
            x0 = T//Ti
        else:
            x0 = T//Ti+1
        if A%Ai == 0:
            x1 = A//Ai
        else:
            x1 = A//Ai+1
        x = max(x0, x1)
        T, A = Ti*x, Ai*x
    return T+A


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
import sys
import numpy as np
import random
import math
from decimal import Decimal
import itertools
import re
import bisect
from collections import deque, Counter
from functools import lru_cache

sys.setrecursionlimit(10**9)
INF = 10**13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
def SERIES(n): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ')
def GRID(h,w): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ').reshape(h,-1)[:,:w]
def GRIDfromString(h,w): return np.frombuffer(sys.stdin.buffer.read(), 'S1').reshape(h,-1)[:,:w]
MOD = 1000000007

def main():
	n = I()
	x, y = 1, 1
	for _ in range(n):
		t, a = LI()
		m = max((x+t-1)//t,(y+a-1)//a)
		x, y = m*t, m*a
	print(x+y)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
TA = [list(map(int, input().split())) for _ in range(n)]

# from math import ceil

res = [1, 1]
for i in range(n):
    t, a = TA[i]
    # mx = max(ceil(res[0] / t), ceil(res[1] / a))
    mx = max(-(-res[0] // t), -(-res[1] // a))
    res = [t * mx, a * mx]

print(sum(res))

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    for i in range(n):
        if i==0:
            t,a=map(int,readline().split())
        else:
            t_i,a_i=map(int,readline().split())
            tmp1=(t+t_i-1)//t_i
            tmp2=(a+a_i-1)//a_i
            x=max(tmp1,tmp2)
            t=t_i*x
            a=a_i*x
    
    print(a+t)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())

ra,rb = 1,1
for i in range(n):
  a,b = map(int,input().split())
  n = max((ra+a-1)//a,(rb+b-1)//b)
  ra = a*n
  rb = b*n
    
print(ra+rb)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    TA = [list(map(int, input().split())) for _ in range(n)]

    t_pev, a_prev = TA[0]
    for i in range(1, n):
        t_now, a_now = TA[i]
        if t_pev <= t_now and a_prev <= a_now:
            t_pev, a_prev = t_now, a_now
        else:
            d = max((t_pev + (t_now - 1)) // t_now, (a_prev + (a_now - 1)) // a_now)
            t_pev, a_prev = t_now * d, a_now * d
    print(t_pev + a_prev)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
# coding: utf-8
# hello worldと表示する
import sys
#import numpy
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
from copy import deepcopy
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
pairs=[LI() for i in range(n)]
def pair_revise(x,y,a,b):
    global p
    global q
    #print(x,y,a,b)
    for i in range(max(x//a,y//b),max(x//a,y//b)+2):
        p,q=a*i,b*i
        if p>=x and q>=y:
            break
    return [p,q]
    #return [x*max(ceil(x/a),ceil(y/b)),y*max(ceil(x/a),ceil(y/b))]
    #for i in range(max(ceil(x/a),ceil(y/b)),):
u,v=pairs[0]
for i in range(n-1):
    u,v=pair_revise(u,v,pairs[i+1][0],pairs[i+1][1])
print(u+v)
    
    
        









'''def pair_revise(x,y,a,b):
    global p
    global q
    i=max(ceil(x/a),ceil(y/b))
    p,q=a*i,b*i
    return p,q
u,v=1,1
for i in range(n):
    u,v=pair_revise(u,v,pairs[i][0],pairs[i][1])
    print(u,v)
print(u+v)'''

Submission status: Accepted

Solution code:
# coding: utf-8
# hello worldと表示する
import sys
#import numpy
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor,pi,factorial
from operator import itemgetter
from copy import deepcopy
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

def ceil(x,y):
    return (-1)*((-x)//y)
    
n=I()
pairs=[LI() for i in range(n)]
x,y=1,1
for i in range(n):
    a=pairs[i][0]
    b=pairs[i][1]
    k=max(ceil(x,a),ceil(y,b))
    x,y=a*k,b*k
print(x+y)





'''def pair_revise(x,y,a,b):
    global p
    global q
    i=max(ceil(x/a),ceil(y/b))
    p,q=a*i,b*i
    return p,q
u,v=1,1
for i in range(n):
    u,v=pair_revise(u,v,pairs[i][0],pairs[i][1])
    print(u,v)
print(u+v)'''

Submission status: Accepted

Solution code:
n = int(input())
vt, va = 1, 1
for i in range(n):
  t, a = map(int, input().split())
  d = max((t + vt - 1) // t, (a + va - 1) // a)
  vt = d * t
  va = d * a
print(vt + va)

Submission status: Accepted

Solution code:
n=int(input())
ta=[list(map(int,input().split())) for _ in range(n)]
tv,av=1,1
#print('-----')
for t,a in ta:
  i=(t+tv-1)//t
  j=(a+av-1)//a
  k=max(i,j)
  tv=t*k
  av=a*k
print(tv+av)

Submission status: Accepted

Solution code:
t = 1
a = 1

for i in range(int(input())):
  T,A = map(int,input().split())
  n = max(-(-t//T),-(-a//A))
  t = n*T
  a = n*A

print(t+a)

Submission status: Accepted

Solution code:
n = int(input())
ta = []
for i in range(n):
    t,a = map(int,input().split())
    ta.append([t,a])
count = ta[0][0] + ta[0][1]
t = ta[0][0]
a = ta[0][1]
for i in range(1,n):
    if ta[i][0] == ta[i][1]:
        count += max(t,a) - min(t,a)
        t = max(t,a)
        a = max(t,a)
    elif ta[i][0] > ta[i][1]:
        if a % ta[i][1] != 0:
            count += ta[i][1] - (a % ta[i][1])
            a += ta[i][1] - (a % ta[i][1])
        if (a//ta[i][1]) * ta[i][0] >= t:
            count += (a//ta[i][1]) * ta[i][0] - t
            t = (a//ta[i][1]) * ta[i][0]
        else:
            if t % ta[i][0] != 0:
                count += ta[i][0] - (t % ta[i][0])
                t += ta[i][0] - (t % ta[i][0])
            count += (t//ta[i][0]) * ta[i][1] - a
            a = (t//ta[i][0]) * ta[i][1]
    else:
        if t % ta[i][0] != 0:
            count += ta[i][0] - (t % ta[i][0])
            t += ta[i][0] - (t % ta[i][0])
        if (t//ta[i][0]) * ta[i][1] >= a:
            count += (t//ta[i][0]) * ta[i][1] - a
            a = (t//ta[i][0]) * ta[i][1]
        else:
            if a % ta[i][1] != 0:
                count += ta[i][1] - (a % ta[i][1])
                a += ta[i][1] - (a % ta[i][1])
            count += (a//ta[i][1]) * ta[i][0] - t
            t = (a//ta[i][1]) * ta[i][0]
print(count)

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############
"""
N = rand_N(2, 6)
query = rand_query(2, 10, N)
print(query)
ans_taka, ans_aoki = query[0]
"""

N = getN()
query =  [getList() for i in range(N)]
# 一回目の選挙速報の結果を受けて
ans_taka, ans_aoki = query[0]

# 現在の高橋くん、青木くんの票数、選挙速報の結果の比を基に
# 次の高橋くん、青木くんの票数の最小値が出てくる
def vote(taka, aoki, ratio):
    ratio_taka, ratio_aoki = ratio
    taka_multi = (taka + ratio_taka - 1) // ratio_taka
    aoki_multi = (aoki + ratio_aoki - 1) // ratio_aoki
    multi = max(taka_multi, aoki_multi)

    return [ratio_taka * multi, ratio_aoki * multi]

# 選挙速報２回目〜
for i in range(1, N):
    ans_taka, ans_aoki = vote(ans_taka, ans_aoki, query[i])
    # print(ans_taka, ans_aoki)
print(ans_taka + ans_aoki)

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############
"""
N = rand_N(2, 6)
query = rand_query(2, 10, N)
print(query)
ans_taka, ans_aoki = query[0]
"""

N = getN()
query =  [getList() for i in range(N)]
ans_taka, ans_aoki = query[0]

# 次の高橋くん、青木くんの票数が出てくる
def vote(taka, aoki, ratio):
    ratio_taka, ratio_aoki = ratio
    taka_multi = (taka + ratio_taka - 1) // ratio_taka
    aoki_multi = (aoki + ratio_aoki - 1) // ratio_aoki
    multi = max(taka_multi, aoki_multi)

    return [ratio_taka * multi, ratio_aoki * multi]

for i in range(1, N):
    ans_taka, ans_aoki = vote(ans_taka, ans_aoki, query[i])
    # print(ans_taka, ans_aoki)
print(ans_taka + ans_aoki)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def gcd(a,b):
    while b:a,b=b,a%b
    return a

def lcm(a,b):return a*b//gcd(a,b)

def main():
    n=II()
    ta=LLI(n)
    x=y=-1
    for t,a in ta:
        if x==-1:
            x,y=t,a
            continue
        c=max((t+x-1)//t,(a+y-1)//a)
        x=c*t
        y=c*a
    print(x+y)

main()

Submission status: Accepted

Solution code:
import math
from fractions import Fraction

N = int(input())
a, b = map(int, input().split())
ret_a = a
ret_b = b
for _ in range(1, N):
    a, b = map(int, input().split())
    x = math.ceil(max(Fraction(ret_a, a), Fraction(ret_b, b)))
    ret_a = a * x
    ret_b = b * x
print(ret_a + ret_b)

Submission status: Accepted

Solution code:
import sys
def v():
    N=int(sys.stdin.readline())
    P=[]
    pt,pa=tuple(map(int,sys.stdin.readline().split()))
    res=pt+pa
    for _ in [0]*(N-1):
        xt,xa=tuple(map(int,sys.stdin.readline().split()))
        xs=xt+xa
        kt=pt//xt if pt%xt ==0 else pt//xt+1
        ka=pa//xa if pa%xa ==0 else pa//xa+1
        k=max([kt,ka])
        pt,pa=k*xt,k*xa
        res=pt+pa
    print(res)
if __name__=='__main__':v()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    TA = [list(map(int, input().split(' '))) for _ in range(N)]
    if N == 1:
        print(sum(TA[0]))
        return
    cur_t, cur_a = TA[0]
    for t, a in TA[1:]:
        ratio = max([(cur_t + t - 1) // t, (cur_a + a - 1) // a])
        cur_t, cur_a = ratio * t, ratio * a
    print(cur_t + cur_a)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import ceil 
N = int(input())
votes = [1,1]
rate = 1
for i in range(N):
    t,a = map(int,input().split())
    rate = max((votes[0] - 1) // t, (votes[1] - 1) // a) + 1
    # if t * rate >= votes[0] and a * rate >= votes[1]:
    votes = [t * rate, a * rate]

print(sum(votes))

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A,B = [1,1]
for _ in range(N):
  a,b = IL()
  if A*b > B*a: #A/B > a/b
    A = -(-A//a)*a
    B = (A//a)*b
  else:
    B = -(-B//b)*b
    A = (B//b)*a
print(A+B)

Submission status: Accepted

Solution code:
n=int(input())
x,y=map(int,input().split())
z=x+y
for i in range(n-1):
  a,b=map(int,input().split())
  k=max((x-1)//a,(y-1)//b)+1
  x=k*a
  y=k*b
print(x+y)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import math

def main():
    N = int(input())
    T, A = map(int, input().split())
    for _ in range(N-1):
        t, a = map(int, input().split())
        # 桁が大きいとfloatの誤差が生じて正しく判定できない
        # 負の数にして切り捨てにすればceilと同じ挙動になる
        x = max(-(-T//t), -(-A//a))
        T = t*x
        A = a*x
    print(T+A)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
import sys
def sinput(): return sys.stdin.readline()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(): return list(imap())
def farr(): return list(fmap())
def sarr(): return sinput().split()

n = int(input())
tv = av = 1
t = [0]*n; a = [0]*n
for i in range(n):
    t[i], a[i] = imap()
for i in range(n):
    tmp = max(tv//t[i]+(tv%t[i]!=0), av//a[i]+(av%a[i]!=0))
    tv = t[i]*tmp; av=a[i]*tmp
print(tv+av)

Submission status: Accepted

Solution code:
N = int(input())

T, A = map(int, input().split())

for i in range(N-1):
  t, a = map(int, input().split())
  da = (a - A%a)%a
  dt = (A + da)*t//a - T
  if dt >= 0:
    A += da
    T += dt
  else:
    i = -(dt // t)
    da += i*a
    dt += i*t
    A += da
    T += dt

print(T + A)

Submission status: Accepted

Solution code:
N=int(input())
t=a=1
for i in range(N):
    T,A=map(int,input().split())
    num=max(-(-t//T),-(-a//A))
    t,a=num*T,num*A

print(t + a)

Submission status: Accepted

Solution code:
import math

n = int(input())
ta = [list(map(int, input().split())) for _ in range(n)]

x, y = ta[0]
for i in range(1, n):
    t, a = ta[i]
    k = max(-(-x // t), -(-y // a))
    x, y = k * t, k * a

print(x + y)

Submission status: Accepted

Solution code:
import math
from fractions import Fraction
n = int(input())
# t0, a0 = map(int, input().split())
anst = 1
ansa = 1
for _ in range(n):
    ti, ai = map(int, input().split())
    anst = ti * math.ceil(max(Fraction(anst, ti), Fraction(ansa, ai)))
    ansa = ai * math.ceil(max(Fraction(anst, ti), Fraction(ansa, ai)))
    # p
    # if math.ceil(anst/ti) < math.ceil(ansa/ai):
    #     # if math.ceil(anst/ti)*ai >= ansa:
    #     #     anst = ti * math.ceil(anst/ti)
    #     #     ansa = ai * math.ceil(anst/ti)
    #     # else:
    #     ansa = ai * math.ceil(ansa/ai)
    #     anst = ti * math.ceil(ansa/ai)
    # else:
    #     # if math.ceil(ansa/ai)*ti >= anst:
    #     #     ansa = ai * math.ceil(ansa/ai)
    #     #     anst = ti * math.ceil(ansa/ai)
    #     # else:
    #     ansa = ai * math.ceil(anst/ti)
    #     anst = ti * math.ceil(anst/ti)
    # # print("anst", anst,"ansa = ", ansa)
        
print(int(anst + ansa))

Submission status: Accepted

Solution code:
import bisect
import collections
import copy
import functools
import heapq
import math
import sys
from collections import deque
from collections import defaultdict
input = sys.stdin.readline
MOD = 10**9+7

N = int(input())
T = [0]*N
A = [0]*N
for i in range(N):
    T[i],A[i] = map(int,(input().split()))

t,a = T[0],A[0]

for i in range(1,N):
    s = T[i] + A[i]
    now = 1
    l = 1
    r = 10**18//s + 1
    mae = -1
    while now != mae:
        mae = now
        if T[i]*now < t or A[i]*now < a:
            l = now
        else:
            r = now
        now = (l+r+1)//2
    t,a = T[i]*now,A[i]*now
print(t+a)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():

    N = int(input())
    A, B = [0] * N, [0] * N

    for i in range(N):
        A[i], B[i] = map(int, input().split())

    x = 1

    for i in range(N-1):
        x = max(-(-A[i]*x // A[i+1]), -(-B[i]*x // B[i+1]))

    print(x*(A[N-1]+B[N-1]))


resolve()

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_CEILING

N = int(input())
pairs = (map(Decimal, input().split()) for _ in range(N))

t = Decimal('1')
a = Decimal('1')
ceil = lambda d: d.to_integral_exact(rounding=ROUND_CEILING)

for ti, ai in pairs:
    mul = max(ceil(t / ti), ceil(a / ai))
    t = ti * mul
    a = ai * mul

print(int(t + a))

Submission status: Accepted

Solution code:
N = int(input())
T, A = 1, 1
for _ in range(N):
    t, a = map(int,input().split())
    k = max((T - 1) // t + 1, (A - 1) // a + 1)
    T = t * k
    A = a * k
print(A + T)