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):
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split())
B1,B2 = map(int,input().split())

len1 = T1 * A1 + T2 * A2
len2 = T1 * B1 + T2 * B2
if len1 == len2:
  print("infinity")
  quit()

ans = 0
subs = 0  
sub1 = T1 * (A1 - B1)
sub2 = T2 * (A2 - B2)

if sub1 > 0:
  if len1 - len2 > 0:
    ans = 0
  else:
    S = (-1 * sub1) // (sub1 + sub2)
    T = (-1 * sub1) % (sub1 + sub2)
    if T == 0:
      ans = 2 * S
    else:
      ans = 2 * S + 1
else:
  if len1 - len2 < 0:
    ans = 0
  else:
    S = (-1 * sub1) // (sub1 + sub2)
    T = (-1 * sub1) % (sub1 + sub2)
    if T == 0:
      ans = 2 * S
    else:
      ans = 2 * S + 1
    
print(ans)

Solution (Python):
def main():
    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:
        return 'infinity'
    if a1<b1:
        a1,b1=b1,a1
        a2,b2=b2,a2
    if a2>b2:
        return 0
    d1=(a1-b1)*t1
    d2=(b2-a2)*t2
    if d1>d2:
        return 0
    d=d2-d1
    if d1%d==0:
        return d1//d*2
    return d1//d*2+1
    
if __name__=='__main__':
    print(main())

Solution (Python):
T1,T2=[int(i) for i in input().split()]
A1,A2=[int(i) for i in input().split()]
B1,B2=[int(i) for i in input().split()]
if((B1-A1)*((B1-A1)*T1+(B2-A2)*T2)>0):
  print(0);
  exit();
if((B1-A1)*T1+(B2-A2)*T2==0):
  print("infinity")
  exit();
ans=(abs((B1-A1)*T1)//abs((B1-A1)*T1+(B2-A2)*T2))*2+1
if(abs((B1-A1)*T1)%abs((B1-A1)*T1+(B2-A2)*T2)==0):
  ans-=1
print(ans)

Solution (Python):
s,t = map(int,input().split())
k,l = map(int,input().split())
m,n = map(int,input().split())
a = (k-m)*s
b = (l-n)*t

if a+b == 0:
    print("infinity")
elif a>0 and a+b>0:
    print(0)
elif a<0 and a+b<0:
    print(0)
else:
    div = abs(a) // abs(a+b)
    if div == abs(a) / abs(a+b):
        print(div*2)
    else:
        print(div*2+1)

Solution (Python):
from decimal import Decimal
T = tuple(map(Decimal, input().split()))

A = list(map(Decimal, input().split()))
B = list(map(Decimal, input().split()))


ans = 0
if A[0] < B[0]:
    tmp = (A[0], A[1])
    A[0], A[1] = B[0], B[1]
    B[0], B[1] = tmp[0], tmp[1]

a = abs(T[0]*A[0] - T[0]*B[0])

b = T[0]*(B[0]-A[0])+T[1]*(B[1]-A[1])


if b == 0:
    print("infinity")
elif b < 0:
    print(0)
    exit(0)
else:
    ans += 1
    if b > a:
        print(1)
    elif b == a:
        print("infinity")
    else:
        if a % b == 0:
            print((a//b)*2)
        else:
            print((a//b)*2 + 1)

Solution (Python):

import sys, math, itertools, heapq, collections, bisect, string, copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7
ans = count = 0; pro = 1

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
if a1*t1+a2*t2<b1*t1+b2*t2:
    print(0)
    exit()
elif a1*t1+a2*t2 == b1*t1+b2*t2:
    print("infinity")
    exit()
dis=t2*(a2-b2)+t1*(a1-b1)
ans=(t1*(b1-a1))//dis
anssub = (t1*(b1-a1))%dis
if anssub==0:
    sub=0
else:
    sub=1
print(ans*2+sub)

Solution (Python):
from math import floor

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

diff1 = (a1 - b1) * t1
diff2 = (a2 - b2) * t2
diff = diff1 + diff2

if diff1 * diff > 0:
    print("0")

elif diff == 0:
    print("infinity")

else:
    diff1 = abs(diff1)
    diff2 = abs(diff2)
    n = diff2 / (diff2 - diff1)

    if n == floor(n):
        n = floor(n)
        ans = 2 * n - 2
    else:
        n = floor(n)
        ans = 2 * n - 1

    print(ans)

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

from math import *

x = A1*T1 + A2*T2 - (B1*T1 + B2*T2)
y = A1*T1 - B1*T1
if(x ==0 or y==0):
    print('infinity')
elif(y * x > 0):
    print(0)
else:
    z = floor(abs(y) / abs(x))
    if abs(y) == z * abs(x):
        print(floor(abs(y)/abs(x))*2)
    else:
        print(floor(abs(y)/abs(x))*2+1)

Solution (Python):

from math import floor

def solve():
    T1, T2 = map(int, input().split())
    A1, A2 = map(int, input().split())
    B1, B2 = map(int, input().split())
    C1 = T1*(A1-B1)
    C = C1 + T2*(A2-B2)
    if C == 0:
        return 'infinity'
    elif C1/C > 0:
        return '0'
    elif C1/C > -1:
        return '1'
    else:
        res =  2*(floor(-C1/C)+1)-1-(C1/C).is_integer()
        return str(res)

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

Solution (Python):
def main():
    t = list(map(int, input().split()))
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    circle = a[0] * t[0] + a[1] * t[1] - b[0] * t[0] - b[1] * t[1]
    if circle == 0:
        print('infinity')
        return
    d = [0, 0]
    cd = 0
    ans = -1
    for i in range(2):
        d[i] = cd + a[i] * t[i] - b[i] * t[i]
        if cd * d[i] <= 0:
            ans += 1
        cd = d[i]
    if ans == 0:
        print(0)
        return
    if circle < 0:
        if d[0] > 0:
            ans += d[0] // -circle * 2
            if d[0] % (-circle) == 0:
                ans -= 1
        elif d[1] > 0:
            ans += d[1] // -circle * 2
            if d[1] % (-circle) == 0:
                ans -= 1
    else:
        if d[0] < 0:
            ans += -d[0] // circle * 2
            if -d[0] % (circle) == 0:
                ans -= 1
        elif d[1] < 0:
            ans += -d[1] // circle * 2
            if -d[1] % (circle) == 0:
                ans -= 1
    print(ans)
main()

Solution (Python):
from math import ceil
t1,t2,a1,a2,b1,b2=map(int,open(0).read().split())
w,x,y,z=t1*a1,t2*a2,t1*b1,t2*b2
if w+x==y+z:
	print("infinity")
	exit()
if (w-y)*(w+x-y-z)>0:
	print(0)
	exit()
a1=min(w,y)
b1=max(w,y)
c1=max(w+x,y+z)
c2=min(w+x,y+z)
print(2*int((b1-a1)//(c1-c2))+int((b1-a1)%(c1-c2)>0))

Solution (Python):
import math

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

  sumA = t1 * a1 + t2 * a2
  sumB = t1 * b1 + t2 * b2

  if sumA == sumB:
    return 'infinity'
  if sumA < sumB:
    sumA, sumB = sumB, sumA
    a1, b1 = b1, a1
    a2, b2 = b2, a2
  

  halfA = t1 * a1
  halfB = t1 * b1
  if halfA > halfB:
    return 0
  div, mod = divmod(halfB - halfA, sumA - sumB)
  if mod == 0:
    return div * 2
  return div * 2 + 1

print(main())

Solution (Python):
from math import floor

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

diff1 = (a1 - b1) * t1
diff2 = (a2 - b2) * t2
diff = diff1 + diff2

if diff1 * diff > 0:
    print("0")

elif diff == 0:
    print("infinity")

else:
    diff1 = abs(diff1)
    diff2 = abs(diff2)
    n = diff2 / (diff2 - diff1)

    if n == floor(n):
        n = floor(n)
        ans = 2 * n - 2
    else:
        n = floor(n)
        ans = 2 * n - 1

    print(ans)

Solution (Python):
import sys

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

p1 = (b1 - a1) * t1
p2 = (b2 - a2) * t2

if p1 + p2 == 0:
    print('infinity')
    sys.exit()

if p1 + p2 < 0:
    p1 *= -1
    p2 *= -1

if p1 > 0:
    print(0)
    sys.exit()

ans = int((-1 * p1) / (p1+p2)) * 2 + 1

if (-1 * p1) % (p1+p2) == 0:
    ans -= 1

print(ans)

Solution (Python):

t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
l1 = (a1-b1)*t1
l2 = (a2-b2)*t2

    
if (l1+l2)==0:
    print('infinity')
elif (l1)*(l1+l2)>0:
    print(0)
else:
    zure = abs(l1+l2)
    oitsuki = abs(l1)
    n = oitsuki//zure
    if n * zure == oitsuki:
        print(2*n)
    else:
        print(2*n+1)

Solution (Python):
t1,t2 = list(map(int, input().split()))
a1,a2 = list(map(int, input().split()))
b1,b2 = list(map(int, input().split()))
x=(a1-b1)*t1 + (a2-b2)*t2
if x == 0:
    print("infinity")
elif x*(a1-b1) > 0:
    print(0)
else:
    s,t = divmod(abs((a1-b1)*t1), abs(x))
    if t:
        print(s*2+1)
    else:
        print(s*2)

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

al1=t1*a1
al2=t2*a2
bl1=t1*b1
bl2=t2*b2

if al1>bl1:
    l1=al1
    l2=al2
    l3=bl1
    l4=bl2
else:
    l1=bl1
    l2=bl2
    l3=al1
    l4=al2

if al1+al2==bl1+bl2:
    print("infinity")
else:
    if (l1-l3)%(l1+l2-l3-l4)==0:
        ans=max(0,2*math.floor((l1-l3)/(l3+l4-l1-l2)))
        print(ans)
    else:
        ans=max(0,2*math.floor((l1-l3)/(l3+l4-l1-l2))+1)
        print(ans)

Solution (Python):
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split())
B1,B2 = map(int,input().split())
ans = 0
if T1*A1+T2*A2 == T1*B1+T2*B2:
    print("infinity")
    exit()
if A1>B1:
    jtA = True
else:
    jtA = False
if jtA:
    if T1*A1+T2*A2 > T1*B1+T2*B2:
        print(0)
        exit()
    else:
        jA = False
        x = abs(T1*B1+T2*B2 - T1*A1-T2*A2)
else:
    if T1*A1+T2*A2 < T1*B1+T2*B2:
        print(0)
        exit()
    else:
        jA = True
        x = abs(T1*A1+T2*A2 - T1*B1-T2*B2)

if (abs(A1-B1)*T1)%x == 0:
    ans += ((abs(A1-B1)*T1)//x)*2
else:
    ans += ((abs(A1-B1)*T1)//x)*2+1
print(ans)

Solution (Python):

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

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


def p(x, y):
    if x%y == 0:
        return abs(x//y)
    else:
        return abs(x//y) + 1

if y == 0:
    ans = 'infinity'
else:
    if x%y != 0:
        if x < 0 and y > 0 :
            ans = (abs(x//y) + 1) * 2 - 3
        elif x > 0 and y < 0 :
            ans = (abs(x//y) + 1) * 2 - 3
        else:
            ans = 0
    else:
        if x < 0 and y > 0 :
            ans = (abs(x//y) + 1) * 2 - 2
        elif x > 0 and y < 0 :
            ans = (abs(x//y) + 1) * 2 - 2
        else:
            ans = 0


print(ans)

Solution (Python):
t1,t2=map(int,input().split())

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

a=a1-b1
b=a2-b2


ans=0

if(a*b>0):
    ans=-1
else:
    go=a*t1+b*t2
    if(go*a>0):
        ans=0
    elif(go==0):
        ans=-10
    else:
        kai=a*t1
        if(go<0):
            go*=-1
        if(kai<0):
            kai*=-1
        
        if(kai%go==0):
            ans=(kai//go)*2
        else:
            ans=(kai//go)*2+1

if(ans==-1):
    print(0)
elif(ans==-10):
    print("infinity")
else:
    print(ans)

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

Adist1=T1*A1
Adist2=T2*A2
Bdist1=T1*B1
Bdist2=T2*B2


if Adist1==Bdist1:
  print("infinity")

elif Adist1>Bdist1 and Adist1+Adist2>Bdist1+Bdist2:
  print(0)

elif Adist1<Bdist1 and Adist1+Adist2<Bdist1+Bdist2:
  print(0)

elif Adist1+Adist2==Bdist1+Bdist2:
  print("infinity")

elif Adist1>Bdist1 and Adist1+Adist2<Bdist1+Bdist2:
  diff=(Bdist1+Bdist2)-(Adist1+Adist2)
  print(2*((Adist1-Bdist1)//diff)+((Adist1-Bdist1)%diff!=0))

elif Adist1<Bdist1 and Adist1+Adist2>Bdist1+Bdist2:
  diff=(Adist1+Adist2)-(Bdist1+Bdist2)
  print(2*((Bdist1-Adist1)//diff)+((Bdist1-Adist1)%diff!=0))

Solution (Python):
def f():
    t0, t1 = [int(s) for s in input().split()]
    a = [int(s) for s in input().split()]
    b = [int(s) for s in input().split()]
    sa = t0*a[0] + t1*a[1]
    sb = t0*b[0] + t1*b[1]
    if sa == sb:
        return 'infinity'
    if sa > sb:
        a, b = b,a
        sa, sb = sb, sa
    
    if b[0] > a[0]:
        return 0

    delta = sb - sa

    ans = 1
    d0 = (a[0]-b[0])*t0
    if d0%delta == 0:
        ans += 1
    count = (d0-1)//delta
    ans += 2 * count
    return ans


print(f())

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
from itertools import count
from functools import lru_cache
from collections import defaultdict
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok



def main():
    T1,T2 = mis()
    A1,A2 = mis()
    B1,B2 = mis()
    d1 = T1*A1 - T1*B1
    d2 = T2*A2 - T2*B2
    if d1 + d2 == 0:
        print('infinity')
    elif d1 < 0 > d2 or d1 > 0 < d2:
        print(0)
    else:
        if d1 < 0:
            d1 *= -1
            d2 *= -1
        if d1 + d2 > 0:
            print(0)
        elif d1 % (d1+d2) == 0:
            print(d1 // abs(d1+d2) * 2)
        else:
            print(d1 // abs(d1+d2) * 2 + 1)




main()

Solution (Python):
import math
T_1, T_2 = map(int, input().split())
A_1, A_2 = map(int, input().split())
B_1, B_2 = map(int, input().split())
ans = 0
if T_1 * A_1 + T_2 * A_2 == T_1 * B_1 + T_2 * B_2:
    print("infinity")
else:
    
    if T_1 * A_1 + T_2 * A_2 < T_1 * B_1 + T_2 * B_2:
        A_1, A_2, B_1, B_2 = B_1, B_2, A_1, A_2
    if A_1 < B_1:
        sa_12 = T_1 * A_1 + T_2 * A_2 - (T_1 * B_1 + T_2 * B_2)  
        sa_1 = -T_1 * A_1 + T_1 * B_1  
        if sa_1 % sa_12 == 0:
            ans = int((sa_1 / sa_12)*2)
        else:
            kari = math.ceil(sa_1 / sa_12)
            ans = (kari-1)*2+1
    print(ans)

Solution (Python):
t = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

c = [t[0]*a[0],t[1]*a[1]]
d = [t[0]*b[0],t[1]*b[1]]

if sum(c)==sum(d):
    print('infinity')
    exit()
elif c[0]==d[0]:
    print('infinity')
    exit()

if sum(d)>sum(c) and d[0]>c[0]:
    print(0)
    exit()
elif sum(c)>sum(d) and c[0]>d[0]:
    print(0)
    exit()
else:
    n = (c[0]-d[0])/(sum(d)-sum(c))
    if n==int(n):
        print(2*int(n))
    else:
        print(2*int(n)+1)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
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 main():
    t1,t2=MI()
    a1,a2=MI()
    b1,b2=MI()
    if a1<b1:a1,a2,b1,b2=b1,b2,a1,a2
    if a1*t1+a2*t2==b1*t1+b2*t2:
        print("infinity")
        exit()
    if a1*t1+a2*t2>b1*t1+b2*t2:
        print(0)
        exit()
    x=a1*t1-b1*t1
    y=b1*t1+b2*t2-(a1*t1+a2*t2)
    ans=(x//y)*2
    if x%y:
        ans+=1
    print(ans)


main()

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

if (a1<b1 and a2<b2) or (a1>b1 and a2>b2):
    print(0)
    exit()

if a1 > b1:
    a1,b1 = b1,a1
    a2,b2 = b2,a2

_b1 = b1 - a1
_a2 = a2 - b2

v1 = t1*_b1
v2 = t2*_a2

if v1 == v2:
    print('infinity')
    exit()

if v1 > v2:
    print(0)
    exit()

d = v2 - v1
k = v1 // d

ans = k*2 + 1
if v2*k == v1*(k+1):
    ans -= 1
print(ans)

Solution (Python):
T1,T2=map(int, input().split())
A1,A2=map(int, input().split())
B1,B2=map(int, input().split())
C1=A1*T1
D1=A2*T2
C2=B1*T1
D2=B2*T2
if (A1-B1)*(A2-B2)>0:
  print(0)
else:
  C=C1-C2
  D=D1-D2
  if C<0:
    C=-C
    D=-D
  E=C+D
  if E>0:
    print(0)
  elif E==0:
    print("infinity")
  else:
    k=C//int(-E)
    if k==C/int(-E):
      print(2*k)
    else:
      print(2*k+1)

Solution (Python):
def meet(faster, slower):
  if slower[0] - faster[0] < 0:
    print(0)
  else:
    count = (slower[0] - faster[0]) // (sum(faster) - sum(slower))
    if (slower[0] - faster[0]) % (sum(faster) - sum(slower)) == 0:
      print(count * 2)
    else:
      print(count * 2 + 1)

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

at1 = a1 * t1
at2 = a2 * t2
bt1 = b1 * t1
bt2 = b2 * t2

if at1 + at2 < bt1 + bt2:
  faster = [bt1, bt2]
  slower = [at1, at2]
  meet(faster, slower)
elif at1 + at2 > bt1 + bt2:
  faster = [at1, at2]
  slower = [bt1, bt2]
  meet(faster, slower)
else:
  print('infinity')

Solution (Python):

import sys
import re
import random
import numpy as np
from math import sqrt,log
from fractions import gcd  
from collections import Counter, defaultdict



MOD=1000000007

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


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


def calc_kmax(az,bz):
    assert az < bz

    lo = 0    
    hi = az 
    while lo+1 < hi:
        k = (lo + hi)//2
        if k * bz >= (k+1) * az:
            hi = k
        else:
            lo = k
    return hi


def solve(T1,T2,A1,A2,B1,B2):
    T = T1 + T2
    az = T1*A1 + T2*A2
    bz = T1*B1 + T2*B2
    if az == bz:
        print('infinity')
        return False
    if az > bz:
        A1,A2,B1,B2 = B1,B2,A1,A2
        az,bz = bz,az

    numer = T1*(A1-B1)
    denom = (bz-az)

    g = numer / denom
    gi = int(g)
    gr = gi - g

    if (g > 0):
        if gr == 0:
            ans = -1 + gi*2 + 1
        else:
            ans = -1 + (gi+1)*2
    else:
        ans = 0

    print(ans)


T1,T2 = read_ints()
A1,A2 = read_ints()
B1,B2 = read_ints()
solve(T1,T2, A1,A2, B1,B2)

Solution (Python):
import numpy as np

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

S1 = (A1 - B1) * T1
S2 = (A2 - B2) * T2

if (S1 + S2) == 0:
    print('infinity')
elif np.sign(S1) == np.sign(S2):
    print(0)
elif abs(S1) > abs(S2):
    print(0)
else:
    z = 1 + ((abs(S1)) // abs(S1 + S2)) * 2
    if (abs(S1)) % abs(S1 + S2) == 0:
        z = z -1
    print(z)

Solution (Python):
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
v1 = [a1,b1]
d = a1*t1 + a2*t2 - b1*t1 - b2*t2
if d == 0:
  print("infinity")
  exit()
if d > 0:
  f = 0
  s = 1
else:
  f = 1
  s = 0
  d = -d
l = t1*(v1[s]-v1[f])
if l < 0:
  print(0)
  exit()
tm = int(l//d)
if int(l%d) == 0:
  print(2*tm)
else:
  print(1+2*tm)

Solution (Python):
import math

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

sa = A1 * T1
sb = B1 * T1
ea = A2 * T2
eb = B2 * T2
fa = sa + ea
fb = sb + eb

if fa < fb:
  if sa < sb:
    print(0)
  elif sa == sb:
    print(1)
  elif sa > sb:
    d1 = sa - sb
    di = fb - fa
    c = int(d1 / di) + math.ceil(d1 / di)
    print(c)
elif fa == fb:
  print("infinity")
elif fa > fb:
  if sa < sb:
    d1 = sb - sa
    di = fa - fb
    c = int(d1 / di) + math.ceil(d1 / di)
    print(c)
  elif sa == sb:
    print(1)
  elif sa > sb:
    print(0)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from math import ceil

t1, t2 = li()
a1, a2 = li()
b1, b2 = li()


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


elif (b1 - a1) * t1 + (b2 - a2) * t2 < 0:
    
    if (b1 - a1) * t1 < 0 and (b2 - a2) * t2 < 0:
        print(0)
    
    elif (b1 - a1) * t1 < 0 and (b2 - a2) * t2 > 0:
        print(0)
    
    else:
        cross = ceil((b1-a1)*t1 / ((a1-b1)*t1 + (a2-b2)*t2)) - 1
        
        if ((a1-b1)*t1 + (a2-b2)*t2) * (cross+1) + (a1-b1)*t1 == 0:
            print(2* (cross+ 1))
        else:
            print(2 * cross + 1)


else:
    
    if (b1 - a1) * t1 > 0 and (b2 - a2) * t2 > 0:
        print(0)
    
    elif (b1 - a1) * t1 > 0 and (b2 - a2) * t2 < 0:
        print(0)
    
    else:
        cross = ceil((b1-a1)*t1 / ((a1-b1)*t1 + (a2-b2)*t2)) - 1
        
        if ((a1-b1)*t1 + (a2-b2)*t2) * (cross+1) + (a1-b1)*t1 == 0:
            print(2 * (cross + 1))
        else:
            print(2 * cross + 1)

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

c1 = a1 * t1
c2 = a2 * t2
d1 = b1 * t1
d2 = b2 * t2

if c1 + c2 == d1 + d2:
    print("infinity")
    exit(0)

if c1 + c2 < d1 + d2:
    c1, d1 = d1, c1
    c2, d2 = d2, c2



def check(mid):
    if mid % 2 == 0:
        a = (c1 + c2) * (mid // 2)
        b = (d1 + d2) * (mid // 2)
        f1 = a < b
        a += c1
        b += d1
        if a == b:
            return True
        f2 = a < b
        return f1 != f2
    else:
        a = (c1 + c2) * (mid // 2) + c1
        b = (d1 + d2) * (mid // 2) + d1
        f1 = a < b
        a += c2
        b += d2
        if a == b:
            return True
        f2 = a < b
        return f1 != f2

ok, ng = 0, 10 ** 20
while ng - ok > 1:
    mid = (ok + ng) // 2
    if check(mid):
        ok = mid
    else:
        ng = mid

print(ok)

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

delta1 = A1 - B1
delta2 = A2 - B2

d = delta1 * T1 + delta2 * T2
T = T1 + T2

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

if delta1 * delta2 > 0:
    print(0)
    exit()

if delta1 * d > 0:
    print(0)
    exit()

n = int(delta1 * T1 // d) * (-1)
ans = 2 * n - 1
if delta1 * T1 == n * d * (-1):
    ans += 1

print(max(ans, 0))

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

if (a1<b1 and a2<b2) or (a1>b1 and a2>b2):
    print(0)
    exit()

if a1 > b1:
    a1,b1 = b1,a1
    a2,b2 = b2,a2

_b1 = b1 - a1
_a2 = a2 - b2

v1 = t1*_b1
v2 = t2*_a2

if v1 == v2:
    print('infinity')
    exit()

if v1 > v2:
    print(0)
    exit()

d = v2 - v1
k = v1 // d

ans = k*2 + 1
if v2*k == v1*(k+1):
    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 and a2 > b2) or (a1 < b1 and a2 < b2):
  print(0)
else:
  if a1 * t1 + a2 * t2 == b1 * t1 + b2 * t2:
    print("infinity")
  else:
    d_all = (a1 * t1 + a2 * t2) - (b1 * t1 + b2 * t2)
    d_1 = a1 * t1 - b1 * t1
    if (d_all > 0 and d_1 > 0) or (d_all < 0 and d_1 < 0):
      print(0)
    else:
      if abs(d_all) > abs(d_1):
        print(1)
      else:
        ans = 1
        ans += abs(d_1) // abs(d_all) * 2 - (1 if d_1 % d_all == 0 else 0)
        print(ans)

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

if t1*a1 + t2*a2 == 0:
    print("infinity")
    exit()
if a1*a2 > 0:
    print(0)
    exit()
a1 *= t1
a2 *= t2
if (a1 > 0 and a1+a2>0) or (a1<0 and a1+a2<0):
    print(0)
    exit()

if a1 > 0:
    x = -a1/(a1+a2)
    n = ceil(x)
    if n == x:
        print(2*n)
    else:
        print(2*n-1)
if a2 > 0:
    x = a1 / (-(a1 + a2))
    n = ceil(x)
    if n == int(x):
        print(2 * n)
    else:
        print(2 * n - 1)

Solution (Python):
import math

t = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

c = [t[0]*a[0],t[1]*a[1]]
d = [t[0]*b[0],t[1]*b[1]]

if sum(c)==sum(d):
    print('infinity')
    exit()
elif c[0]==d[0]:
    print('infinity')
    exit()

if sum(d)>sum(c) and d[0]>c[0]:
    print(0)
    exit()
elif sum(c)>sum(d) and c[0]>d[0]:
    print(0)
    exit()



if sum(d)>sum(c):
    if d[0]>c[0]:
        print(0)
        exit()
    elif d[0]<c[0]:
        n = (c[0]-d[0])/(sum(d)-sum(c))
        if n==int(n):
            print(2*int(n))
        else:
            print(2*math.ceil(n)-1)
elif sum(c)>sum(d):
    if c[0]>d[0]:
        print(0)
        exit()
    elif c[0]<d[0]:
        n = (c[0]-d[0])/(sum(d)-sum(c))
        if n==int(n):
            print(2*int(n))
        else:
            print(2*math.ceil(n)-1)

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

a1 = T1 * A1
a2 = T2 * A2
a = a1 + a2
b1 = T1 * B1
b2 = T2 * B2
b = b1 + b2


def bis(p, ok, ng):
    mid = (ok + ng) // 2
    return (
        ok if abs(ok - ng) == 1 else
        bis(p, mid, ng) if p(mid) else
        bis(p, ok, mid)
    )


def p(k):
    x = k * a
    y = k * b
    
    
    if x < y:
        return x + a1 >= y + b1
    else:
        return y + b1 >= x + a1


def p2(k):
    x = k * a + a1
    y = k * b + b1
    if x < y:
        return x + a2 >= y + b2
    elif x > y:
        return y + b2 >= x + a2
    else:
        return False

if a == b:
    ans = "infinity"
else:
    
    
    c = 1 if (a1 < b1 and a > b) or (b1 < a1 and b > a) else 0
    
    
    ans = bis(p, 0, 10**16) + bis(p2, 0, 10**16) + c

print(ans)

Solution (Python):
t1, t2 = [ int(v) for v in input().split() ]
a1, a2 = [ int(v) for v in input().split() ]
b1, b2 = [ int(v) for v in input().split() ]
t = t1 + t2

if a1*t1 + a2*t2 == b1*t1 + b2*t2:
    ans = -1
else:
    if a1 >= b1 and  a2 >= b2:
        ans = 0
    elif a1 <= b1 and  a2 <= b2:
        ans = 0
    else:
        if a1*t1 + a2*t2 < b1*t1 + b2*t2:
            a1, b1 = b1, a1
            a2, b2 = b2, a2

        mind = a1*t1 - b1*t1
        if mind > 0:
            ans = 0
        else:
            lastd = a1*t1 + a2*t2 - b1*t1 - b2*t2
            mind = abs(mind)
            if mind % lastd == 0:
                ans = 2 * (mind // lastd) 
            else:
                ans = 2 * (mind // lastd) + 1
    
print(ans if ans != -1 else "infinity")

Solution (Python):


from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def solve():
    t1, t2 = LI()
    a1, a2 = LI()
    b1, b2 = LI()
    if t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
        print("infinity")
        return
    if t1 * a1 > t1 * b1:
        if t1 * a1 + t2 * a2 > t1 * b1 + t2 * b2:
            print(0)
            return
        else:
            x = t1 * (a1 - b1) / (-t1 * (a1 - b1) + t2 * (b2 - a2))
            if x.is_integer():
                print(2 * (t1 * (a1 - b1) // (-t1 * (a1 - b1) + t2 * (b2 - a2))))
            else:
                print(2 * (t1 * (a1 - b1) // (-t1 * (a1 - b1) + t2 * (b2 - a2))) + 1)
            return
    elif t1 * a1 < t1 * b1:
        if t1 * a1 + t2 * a2 < t1 * b1 + t2 * b2:
            print(0)
            return
        else:
            x = t1 * (b1 - a1) / (-t1 * (b1 - a1) + t2 * (a2 - b2))
            if x.is_integer():
                print(2 * (t1 * (b1 - a1) // (-t1 * (b1 - a1) + t2 * (a2 - b2))))
            else:
                print(2 * (t1 * (b1 - a1) // (-t1 * (b1 - a1) + t2 * (a2 - b2))) + 1)
            return
    return



if __name__ == '__main__':
    solve()

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

X1 = T1 * (A1 - B1)
X2 = T2 * (A2 - B2)

D = X1 + X2
if D == 0:
	print("infinity")
else:
	if X1 < 0:
		X1, X2 = [-X1, -X2]
	else:
		D = -D
	
	if D < 0:
		print("0")
	else:
		if X1 % D == 0:
			print((X1 // D) * 2)
		else:
			print((X1 // D) * 2 + 1)

Solution (Python):
def p_f():
    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

    dif1 = a1 * t1 - b1 * t1
    dif2 = (b1 * t1 + b2 * t2) - (a1 * t1 + a2 * t2)
    
    
    if dif2 < 0:
        print(0)
        exit()
    if dif2 == 0:
        print("infinity")
        exit()
    if dif1 % dif2 == 0:
        print(dif1 // dif2 * 2)
    else:
        print((dif1 + dif2) // dif2 * 2 - 1)
        
p_f()

Solution (Python):
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
v1 = [a1,b1]
d = a1*t1 + a2*t2 - b1*t1 - b2*t2
if d == 0:
  print("infinity")
  exit()
if d > 0:
  f = 0
  s = 1
else:
  f = 1
  s = 0
  d = -d
l = t1*(v1[s]-v1[f])
if l < 0:
  print(0)
  exit()
tm = int(l//d)
if int(l%d) == 0:
  print(2*tm)
else:
  print(1+2*tm)

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

    move = T1*(A1-B1) + T2*(A2-B2)
    mid = T1*(A1-B1)

    if move==0:
        return -1

    if move*mid > 0:
        return 0

    if move < 0:
        move *= -1
    else:
        mid *= -1

    if mid%move == 0:
        return 2*(mid//move)
    else:
        return 2*(mid//move)+1

if __name__ == "__main__":
    ans = main()
    if ans==-1:
        print("infinity")
    else:
        print(ans)

Solution (Python):
import math

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

if A1 > B1:
    F1, F2 = A1, A2
    S1, S2 = B1, B2
else:
    F1, F2 = B1, B2
    S1, S2 = A1, A2

oi = F1 * T1 - S1 * T1
nuki = S2 * T2 - F2 * T2 - oi

if nuki == 0:
    print("infinity")
elif nuki > 0:
    enc = oi // nuki * 2
    if oi % nuki != 0:
        enc = enc + 1
    print(enc)
else:
    print(0)

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

if A1*T1+A2*T2 == B1*T1+B2*T2:
    print('infinity')
    exit()

if (A1-B1)*T1 > (B2-A2)*T2:
    print(0)
    exit()

d = abs(A1*T1+A2*T2 - B1*T1-B2*T2)  
L = abs(A1-B1)*T1  


if L % d == 0:
    print(L//d*2)
else:
    ans = L//d*2+1
    print(ans)