Problem:
 Score: 600 points    Problem Statement  Takahashi and Aoki are training for long-distance races in an infinitely long straight course running from west to east.  They start simultaneously at the same point and moves as follows towards the east :   Takahashi runs A_1 meters per minute for the first T_1 minutes,  then runs at A_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.  Aoki runs B_1 meters per minute for the first T_1 minutes,  then runs at B_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.   How many times will Takahashi and Aoki meet each other, that is, come to the same point? We do not count the start of the run. If they meet infinitely many times, report that fact.      Constraints   1 \leq T_i \leq 100000  1 \leq A_i \leq 10^{10}  1 \leq B_i \leq 10^{10}  A_1 \neq B_1  A_2 \neq B_2  All values in input are integers.         Input  Input is given from Standard Input in the following format:  T_1  T_2  A_1  A_2  B_1  B_2       Output  Print the number of times Takahashi and Aoki will meet each other. If they meet infinitely many times, print infinity instead.        Sample Input 1 1 2
10 10
12 4      Sample Output 1 1  They will meet just once, \frac{4}{3} minutes after they start, at \frac{40}{3} meters from where they start.       Sample Input 2 100 1
101 101
102 1      Sample Output 2 infinity  They will meet 101, 202, 303, 404, 505, 606, ... minutes after they start, that is, they will meet infinitely many times.       Sample Input 3 12000 15700
3390000000 3810000000
5550000000 2130000000      Sample Output 3 113  The values in input may not fit into a 32 -bit integer type.

Solution (Python):

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

T1,T2 = inpl()
A1,A2 = inpl()
B1,B2 = inpl()


d1 = (B1-A1)*T1
d2 = (B2-A2)*T2

if d1+d2 == 0:
    print("infinity")
    sys.exit()
else:
    if d1 > 0 and d2 > 0:
        print(0)
        sys.exit()
    elif d1 < 0 and d2 < 0:
        print(0)
        sys.exit()

    if abs(d1) < abs(d2):
        S = d1 
        d1,d2 = d2,d1
    else:
        S = 0

    if d1 < 0:
        d1 *= -1
        d2 *= -1
        S  *= -1

    

    d = d1 + d2
    OK = 0
    NG = 10**100
    while NG-OK > 1:
        mid = (NG+OK)//2
        if mid*d + S <= 0:
            OK = mid
        else:
            NG = mid

    if d*OK + S == 0:
        print(OK*2)
    else:
        print(OK*2+1)

Solution (Python):
T1, T2 = list(map(int, input().split()))
A1, A2 = list(map(int, input().split()))
B1, B2 = list(map(int, input().split()))

a = (A1-B1)*T1
b = (A2-B2)*T2

if a<0:
  a,b = -a,-b
  

  
if a+b>0:
  print(0)

elif a+b ==0:
  print('infinity')

elif a+b<0:
  
  print(2 *(a//(-b-a)) + bool(a%(-b-a)))

Solution (Python):
def main():
    t1,t2 = map(int,input().split())
    a1,a2 = map(int,input().split())
    b1,b2 = map(int,input().split())
    A1 = a1*t1
    A2 = a2*t2
    B1 = b1*t1
    B2 = b2*t2
    A3 = A1+A2
    B3 = B1+B2
    if A3 == B3:
        print("infinity")
    elif A1 < B1 and A3 < B3:
        print(0)
    elif A1 > B1 and A3 > B3:
        print(0)
    elif A3 < B3:
        gap = B3-A3
        x = A1-B1
        num = x//gap
        ans = num*2+1
        if x%gap == 0:
            ans -= 1
        print(ans)
    elif A3 > B3:
        gap = A3-B3
        x = B1-A1
        num = x//gap
        ans = num*2+1
        if x%gap == 0:
            ans -= 1
        print(ans)
        
if __name__ == '__main__':
    main()

Solution (Python):
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())

if a1 * t1 + a2 * t2 == b1 * t1 + b2 * t2:
    print('infinity')
    exit()

if a1 * t1 + a2 * t2 < b1 * t1 + b2 * t2:
    a1, a2, b1, b2 = b1, b2, a1, a2

if a1 > b1:
    print(0)
    exit()

x = (b1 - a1) * t1
y = a1 * t1 + a2 * t2 - b1 * t1 - b2 * t2

if x % y == 0:
    print(x // y * 2)
else:
    print(x // y * 2 + 1)

Solution (Python):
t,T = map(int,input().split())
a,A = map(int,input().split())
b, B = map(int,input().split())
p = a*t+A*T
q = b*t+B*T
if p == q:
    print('infinity')
elif p > q:
    if a > b:
        print(0)
    else:
        ans = 1
        r = p - q
        s = b*t - a*t
        m = s//r
        ans += 2*m
        if s%r == 0:
            ans -= 1
        print(ans)
else:
    if b > a:
        print(0)
    else:
        ans = 1
        r = q - p
        s = a*t - b*t
        m = s//r
        ans += 2*m
        if s%r == 0:
            ans -= 1
        print(ans)

Solution (Python):



import sys

def rl(proc=None):
    if proc is not None:
        return proc(sys.stdin.readline())
    else:
        return sys.stdin.readline().rstrip()

def srl(proc=None):
    if proc is not None:
        return list(map(proc, rl().split()))
    else:
        return rl().split()

def main():
    t1, t2 = srl(int)
    a1, a2 = srl(int)
    b1, b2 = srl(int)
    if a1 < b1:
        a1, b1 = b1, a1
        a2, b2 = b2, a2
    if a1 * t1 + a2 * t2 == b1 * t1 + b2 * t2:
        print('infinity')
        return
    elif a1 * t1 + a2 * t2 > b1 * t1 + b2 * t2:
        print('0')
        return
    s1 = a1*t1 - b1*t1
    s2 = a1 * t1 + a2 * t2 - b1 * t1 - b2 * t2
    s2 = -s2
    b = int(s1 % s2 == 0)
    k = s1 // s2 + 1
    print(2*k-1-b)

if __name__ == '__main__':
    main()

Solution (Python):
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
if t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
    print("infinity")
else:
    dis1 = t1 * (a1 - b1)
    dis2 = t1 * (a1 - b1) + t2 * (a2 - b2)
    if dis1 * dis2 > 0:
        print(0)
    else:
        if dis1 % dis2 == 0:
            print((-dis1 // dis2) * 2)
        else:
            print((-dis1 // dis2 + 1) * 2 - 1)

Solution (Python):
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())

sa = a1 * t1 + a2 * t2
sb = b1 * t1 + b2 * t2

if sa == sb:
	print("infinity")
	exit()


if sa < sb:
	a1, a2, b1, b2 = b1, b2, a1, a2
	sa, sb = sb, sa
delta = sa - sb


if a1 > b1:
	print(0)
	exit()


cnt = (b1 - a1) * t1 // delta


cnt += (b1 - a1) * t1 // delta + 1


if (b1 - a1) * t1 % delta == 0:
	cnt -= 1

print(cnt)

Solution (Python):
t = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = [a[0] - b[0], a[1] - b[1]]
w = t[0] * d[0] + t[1] * d[1]
if w < 0:
    d[0] *= -1
    d[1] *= - 1
    w *= -1
if t[0] * d[0] + t[1] * d[1] == 0:
    print("infinity")
else:
    min, max = 0, 1000000000000000
    while max - min > 1:
        mid = (min + max) // 2
        dest = w * mid
        ok = False
        for i in range(2):
            dest += d[i] * t[i]
            if dest <= 0:
                ok = True
        if ok:
            min = mid
        else:
            max = mid
    now = w * (min - 10)
    ans = 2 * (min - 10) - 1
    if ans < 0:
        ans = 0
        now = 0
    i, last = 0, 0
    while True:
        new = now + d[i] * t[i]
        last += 1
        if (now < 0 and new > 0) or (now > 0 and new < 0) or new == 0:
            ans += 1
            last = 0
        now = new
        i = 1 - i
        if last > 2:
            break
    print(ans)

Solution (Python):
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
a1 -= b1
a2 -= b2

if a1*t1+a2*t2 == 0:
	print("infinity")
	quit()

if a1 < 0:
	a1 = -a1
	a2 = -a2



if a2 > 0:
	print(0)
	quit()



d = a1*t1+a2*t2

if d > 0:
	print(0)
	quit()

ans = ((a1*t1-1)//(-d)+1)*2-1
if a1*t1%(-d) == 0:
	ans += 1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

T1,T2=map(int,input().split())
A1,A2=map(int,input().split())
B1,B2=map(int,input().split())

DA=(T1*A1,T2*A2)
DB=(T1*B1,T2*B2)

if sum(DA)==sum(DB):
    print("infinity")
    sys.exit()

if DA[0]>DB[0]:
    DA,DB=DB,DA

if sum(DA)<sum(DB):
    print(0)
    sys.exit()

rep=(DB[0]-DA[0])//(sum(DA)-sum(DB))
minus=0
if (DB[0]-DA[0])%(sum(DA)-sum(DB))==0:
    minus=1
print(rep*2+1-minus)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    t = LI()
    a = LI()
    b = LI()
    a[0] -= b[0]
    a[1] -= b[1]
    if t[0]*a[0] == -t[1]*a[1]:
        print("infinity")
        return
    if a[0] < 0:
        a[0] *= -1
        a[1] *= -1
    if t[0]*a[0]+t[1]*a[1] > 0:
        print(0)
        return
    l = 0
    r = 10**100
    A = t[0]*a[0]+t[1]*a[1]
    while r-l > 1:
        m = (l+r) >> 1
        x = m*A
        y = x+t[0]*a[0]
        if x*y <= 0:
            l = m
        else:
            r = m
    ans = r*2-1
    if l*A+t[0]*a[0] == 0:
        ans -= 1
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
T = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

if A[0] > B[0]:
  A, B = B, A

if A[1] < B[1]:
  print(0)
  exit()

d = (B[0] - A[0]) * T[0]
k = (A[1] - B[1]) * T[1] - d

if k < 0:
  print(0)
  exit()

if k == 0:
  print("infinity")
  exit()

ans = 1 + (d // k) * 2
if d // k == d / k:
  ans -= 1

print(ans)

Solution (Python):
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
if A1 > B1:
    A1, B1 = B1, A1
    A2, B2 = B2, A2

A, B = A1 * T1 + A2 * T2, B1 * T1 + B2 * T2
if A < B:
    print(0)
elif A == B:
    print("infinity")
else:
    c = 0
    l = 0
    r = 1<<100
    while r - l > 1:
        m = (l+r) // 2
        if B * (m-1) + B1 * T1 > A * (m-1) + A1 * T1:
            l = m
            c = 1
        elif B * (m-1) + B1 * T1 == A * (m-1) + A1 * T1:
            l = m
            c = 0
        else:
            r = m
    print((l-1) * 2 + c)

Solution (Python):
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())

ng = "infinity"

first = t1 * a1 - t1 * b1
second = t1 * a1 + t2 * a2 - t1 * b1 - t2 * b2

if second == 0:
    print(ng)
    exit()

if first * second > 0:
    print(0)
    exit()

if first < 0:
    first *= -1
    second *= -1
second *= -1
ans = 2 * (first // second) + 1
if first % second == 0:ans -=1
print(ans)

Solution (Python):
import math

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



t1,t2 = list(map(int, input().split()))
a1,a2 = list(map(int, input().split()))
b1,b2 = list(map(int, input().split()))

d1 = a1*t1 - b1*t1
d2 = (a2-b2)*t2


if(d1*d2>0 or abs(d1)>abs(d2)):
    print(0)
elif(abs(d1)==abs(d2)):
    print('infinity')
else:
    if(d1 % (abs(d2)-abs(d1)) == 0):
        print(2*(abs(d1) // (abs(d2)-abs(d1))))
    else:
        print(1 + 2*(abs(d1) // (abs(d2)-abs(d1))  ))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

T1,T2,A1,A2,B1,B2 = map(int,read().split())



def F(T1,T2,A1,A2):
    if A1 == 0:
        return 'infinity'
    if A1 < 0:
        A1 = -A1
        A2 = -A2
    if A2 >= 0:
        return 0
    R = T1 * A1
    L = T2 * (-A2)
    dx = R - L
    if dx == 0:
        return 'infinity'
    if dx > 0:
        return 0
    dx = -dx
    ret = 2 * (R//dx) + 1
    if R % dx == 0:
        ret -= 1
    return ret

answer = F(T1,T2,A1-B1,A2-B2)
print(answer)

Solution (Python):
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())

P = (A1 - B1) * T1
Q = (A2 - B2) * T2
if P > 0:
	P *= -1
	Q *= -1

if P + Q < 0:
	print(0)
elif P + Q == 0:
	print("infinity")
else:
	S = (-P) // (P + Q)
	T = (-P) % (P + Q)
	if T != 0:
		print(S * 2 + 1)
	else:
		print(S * 2)