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.



Sample input:
1 2
10 10
12 4


Sample output:
1

Submission status: Accepted

Solution code:
import sys
import math
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

T1, T2 = IL()
a1, a2 = IL()
b1, b2 = IL()
if a1*T1 + a2*T2 > b1*T1 + b2*T2:
  pass
else:
  a1,a2,b1,b2 = b1,b2,a1,a2

if a1*T1 + a2*T2 == b1*T1 + b2*T2:
  print("infinity")
elif a1*T1 >= b1*T1:
  print(0)
else:
  half = b1*T1 - a1*T1
  x = (a1*T1 + a2*T2 - (b1*T1 + b2*T2))
  # half/x >= n
  loop = half//x + 1
  if (loop - 1)*x == half:
    print(loop*2 -2)
  else:
    print(loop*2 -1)

Submission status: Accepted

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

if B1>A1:
  A1,A2,B1,B2=B1,B2,A1,A2
  
if A1*T1+A2*T2>B1*T1+B2*T2:
  print(0)
  exit()
if A1*T1+A2*T2==B1*T1+B2*T2:
  print("infinity")
  exit()

x=(T1*A1-T1*B1)//((B1-A1)*T1+(B2-A2)*T2)+1
ans=2*x-1
if (T1*A1-T1*B1)%((B1-A1)*T1+(B2-A2)*T2)==0:
  ans-=1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#mitsui f
import sys
import math
import collections
#sys.setrecursionlimit(100000)

#n=int(input())

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

if(a1*t1+a2*t2==b1*t1+b2*t2):
	print("infinity")
	sys.exit()
diff=(a1*t1+a2*t2)-(b1*t1+b2*t2)


if(diff>0):
	c1=a1-b1
	c2=a2-b2
else:
	c1=b1-a1
	c2=b2-a2

if(c1>0):
	print("0")
	sys.exit()

diff=c1*t1+c2*t2
pitch=c1*t1*-1

ans=pitch//diff*2+1
if(pitch%diff==0):
	ans-=1
print(ans)

Submission status: Accepted

Solution code:
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:
    a1,a2,b1,b2 = b1,b2,a1,a2
d = b1*t1+b2*t2 - (a1*t1+a2*t2)
if d == 0:
    print('infinity')

elif a1*t1<b1*t1:
    print(0)
else:
    if (a1*t1 - b1*t1)*2%d ==0:
        print(((a1*t1 - b1*t1)*2+d-1)//d)
    else:
        print((a1*t1 - b1*t1)//d*2+1)

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

Solution code:
def solve():
    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)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
sa1=(a1-b1)*t1
sa2=(a2-b2)*t2
if sa1>=0:
    sa1*=-1
    sa2*=-1
if sa1+sa2==0:
    print("infinity")
if sa1+sa2<0:
    print(0)
if sa1+sa2>0:
    ans=((-sa1)//(sa2+sa1))*2+1
    if sa1%(sa1+sa2)==0:
        ans-=1
    print(ans)

Submission status: Accepted

Solution code:
import sys

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

a = a1 * t1 + a2 * t2
b = b1 * t1 + b2 * t2
ha = a1 * t1
hb = b1 * t1
if a == b or ha == hb:
  print ("infinity")
  sys.exit(0)

if a > b:
  a, b = b, a
  ha, hb = hb, ha

gap = b - a
hgap = ha - hb

if hgap < 0:
  print (0)
  sys.exit(0)

ans = 2 * (hgap // gap) + (1 if hgap % gap > 0 else 0)
print (ans)

Submission status: Accepted

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


da, db = 0, 0
for i in range(2):
    da += T[i] * A[i]
    db += T[i] * B[i]

if da == db:
    print("infinity")
    exit()

x = (B[0] - A[0]) * T[0]
y = da - db

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

Submission status: Accepted

Solution code:
s,t,a,b,x,y=map(int,open(0).read().split())
if a*s+b*t==x*s+y*t:
  print('infinity')
  exit()
if a<x:a,b,x,y=x,y,a,b
if a*s+b*t>x*s+y*t:
  print(0)
  exit()
n=(a*s-x*s)/(x*s+y*t-(a*s+b*t))
m=int(n)
print(m*2+(n>m))

Submission status: Accepted

Solution code:
T1,T2=map(int,input().split())
A1,A2=map(int,input().split())
B1,B2=map(int,input().split())
X=(B1-A1)*T1
Y=(B2-A2)*T2
if X>0:
    X=-X
    Y=-Y
if X+Y<0:
    print(0)
elif X+Y==0:
    print("infinity")
else:
    A=(-X)//(X+Y)
    B=(-X)%(X+Y)
    if B!=0:
        print(2*A+1)
    else:
        print(2*A)

Submission status: Accepted

Solution code:
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() ]

if a1*t1 + a2*t2 == b1*t1 + b2*t2:
    ans = -1
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")

Submission status: Accepted

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

a = T[0]*A[0] + T[1]*A[1]
b = T[0]*B[0] + T[1]*B[1]

if(a==b):
    print("infinity")
    exit()

if(a<b):
    A[0], B[0] = B[0], A[0]
    A[1], B[1] = B[1], A[1]


a = T[0]*A[0] # tak
b = T[0]*B[0] # aok

if a>b:
    print(0)
    exit()

left = 1
right = 10**20

while left+1!=right:
    center = (left+right)//2
    tak = a + (center-1) * (A[0]*T[0]+A[1]*T[1])
    aok = b + (center-1) * (B[0]*T[0]+B[1]*T[1])

    if(tak==aok):
        ans = (center-1)*2
        print(ans)
        exit()
    elif aok<tak:
        right = center
    else:
        left = center

ans = (left-1)*2 + 1
print(ans)

Submission status: Accepted

Solution code:
def f():
	from math import ceil
	T1,T2 = map(int,input().split())
	A1,A2 = map(int,input().split())
	B1,B2 = map(int,input().split())
	if B1 > A1:
		A1,A2,B1,B2 = B1,B2,A1,A2
	diff1 = (A1 - B1) * T1
	fac = (B2 - A2) * T2 - diff1
	if fac == 0:
		print("infinity")
	elif diff1 / fac < 0:
		print(0)
	elif diff1 / fac <= 1:
		print(1)
	else:
		print(2 * ceil(diff1 / fac) - int(diff1 % fac != 0))
f()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
T1,T2 = map(int, input().split())
A1,A2 = map(int, input().split())
B1,B2 = map(int, input().split())
d1,d2 = (A1-B1)*T1,(A2-B2)*T2
if d1 < 0: d1,d2 = -d1,-d2
if d1+d2 == 0: print("infinity")
elif d1+d2>0: print(0)
elif d1 % (-(d1+d2)) == 0: print(d1//(-(d1+d2))*2)
else: print(d1//(-(d1+d2))*2+1)

Submission status: Accepted

Solution code:
import math
t1,t2 = map(int, input().split())
c1,c2 = map(int, input().split())
d1,d2 = map(int, input().split())

x = [(c1*t1, c2*t2),(d1*t1, d2*t2)]

x.sort(reverse=True)

if x[0][0]+x[0][1] == x[1][0]+x[1][1]:
    print("infinity")
elif x[0][0]+x[0][1] > x[1][0]+x[1][1]:
    print(0)
else:
    n = (x[0][0]-x[1][0]+0.0)/(x[1][0]+x[1][1]-x[0][0]-x[0][1])
    m = math.ceil(n)
    if math.floor(n) == m:
        print(2*m)
    else:
        print(2*m-1)

Submission status: Accepted

Solution code:
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
a1 *= t1
a2 *= t2
if a1 + a2 == 0:
    print("infinity")
    exit()
if a1*(a1+a2) > 0:
    print(0)
    exit()

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

Submission status: Accepted

Solution code:
t,T,a,A,b,B=map(int,open(0).read().split())
x,y=(b-a)*t,(A-B)*T
if y-x==0:
 r="infinity"
else:
 s,t=divmod(x,y-x)
 r=max(0,s*2+(t!=0))
print(r)

Submission status: Accepted

Solution code:
t,T,a,A,b,B=map(int,open(0).read().split())
x,y=(b-a)*t,(A-B)*T
if y-x==0:
 r="infinity"
else:
 s,t=divmod(x,y-x)
 r=max(0,s*2+bool(t))
print(r)

Submission status: Accepted

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


def solve(T: "List[int]", A: "List[int]", B: "List[int]"):
    dif1 = T[0]*(A[0] - B[0])
    dif2 = T[1]*(A[1] - B[1])

    if dif1+dif2 == 0:
        print('infinity')
        return
    
    if dif1*dif2>0 or abs(dif1)>abs(dif2):
        print(0)
        return
    
    ## tが抜かれる方
    t = abs(dif1)
    a = abs(dif2)

    if t%abs(t-a)==0:
        print(2*(t//abs(t-a)))
    else:
        print(2*(t//abs(t-a))+1)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    T = [int(next(tokens)) for _ in range(2)]  # type: "List[int]"
    A = [int(next(tokens)) for _ in range(2)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(2)]  # type: "List[int]"
    solve(T, A, B)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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

def solve() :    
    A = T1 * A1 + T2 * A2
    B = T1 * B1 + T2 * B2
    
    if A == B :
        return 'infinity'
    
    if (A1 > B1 and A > B) or (A1 < B1 and A < B) :
        return 0
    
    D0 = abs(A1 - B1) * T1
    D = abs(A - B)
    
    if D0 % D == 0 :
        return D0 // D * 2
    else :
        return D0 // D * 2 + 1
        
print(solve())

Submission status: Accepted

Solution code:
t,T,a,A,b,B=map(int, open(0).read().split())
x,y=(b-a)*t,(A-B)*T
if y-x==0:
 r="infinity"
else:
 s,t=divmod(x,y-x)
 r=max(0,s*2+(1 if t else 0))
print(r)

Submission status: Accepted

Solution code:
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:
    if T1*A1 + T2*A2 > T1*B1 + T2*B2:
        A1, B1 = B1, A1
        A2, B2 = B2, A2
    if T1*A1 < T1*B1:
        print("0")
    else:
        forward = T1*A1 - T1*B1
        shrink = (T1*B1 + T2*B2) - (T1*A1 + T2*A2)
        #print(str(forward))
        #print(str(shrink))
        if forward % shrink == 0:
            print(str(2*forward//shrink))
        else:
            print(str((forward//shrink + 1)*2 - 1))

Submission status: Accepted

Solution code:
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, T = divmod(-P, P+Q)
    if T != 0:
        print(S * 2 + 1)
    else:
        print(S * 2)

Submission status: Accepted

Solution code:
from sys import exit

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 A2 < B2:
  print(0)
  exit()

if A1 * T1 + A2 * T2 < B1 * T1 + B2 * T2:
  print(0)
  exit()

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

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

if a % b == 0:
  print(t * 2)
else:
  print(t * 2 + 1)

Submission status: Accepted

Solution code:
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split())
B1,B2 = map(int,input().split())
T = T1 + T2
C = A1*T1+A2*T2
D = B1*T1+B2*T2
if C == D:
    print("infinity")
    exit()
if C > D:
    A1,B1=B1,A1
    A2,B2=B2,A2
    C,D  = D,C
if A1 < B1:
    print(0)
    exit()
ok = 0
ng = pow(10,30)
while ng - ok > 1:
    md = (ok + ng)//2
    if A1*T1+C*md>=B1*T1+D*md:
        ok = md
    else:
        ng = md
ans = 2*ok + 1
if A1*T1+C*ok == B1*T1+D*ok:
    ans -= 1
print(ans)

Submission status: Accepted

Solution code:
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
f = (a1-b1)*t1
l = (b2-a2)*t2
if f > l:
  print(0)
elif f == l:
  print("infinity")
else:
   print(2 * (f//(l-f)) + int(f%(f-l)!=0))

Submission status: Accepted

Solution code:
t,T,a,A,b,B=map(int, open(0).read().split())
x,y=(a-b)*t,(A-B)*T
if x+y==0:
 r="infinity"
else:
 s,t=divmod(-x, x+y)
 r=0 if s<0 else s*2+(1 if t else 0)
print(r)

Submission status: Accepted

Solution code:
import math
T1,T2=map(int,input().split())
A1,A2=map(int,input().split())
B1,B2=map(int,input().split())
C1,C2=A1-B1,A2-B2
D1,D2=T1*C1,T2*C2

if D1==-D2:
    print("infinity")
    exit(0)

diff=0
x=-1
ans=0

if  D1*(D1+D2)<0:
    ans+=1
    diff+=D1+D2
else:
    print(ans)
    exit(0)

if diff*(diff+D1)<=0:
    ans+=2*abs((diff+D1)//(D1+D2))

if diff+(D1+D2)*abs((diff+D1)//(D1+D2))+D1==0:
    ans+=1
if diff+(D1+D2)*abs((diff+D1)//(D1+D2))+D1+D2==0:
    ans+=1

print(ans)

Submission status: Accepted

Solution code:
import numpy

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

v1=b1-a1
v2=b2-a2

x1=v1*t1
x2=v2*t2
di = abs(abs(x2)-abs(x1))
if numpy.sign(x1)==numpy.sign(x2):
    print(0)
elif abs(x2)<abs(x1):
    print(0)
elif abs(x1)==abs(x2):
    print("infinity")
else:
    if abs(x1)%di==0:
        print(abs(int(2*(x1/di))))
        #di = abs(x2)-abs(x1)
    else:
        print(abs(int(2*(x1//di)+1)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import sys
    import math
    s,t=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
    x=(a[0]-b[0])*s
    y=(a[1]-b[1])*t

    if x+y>0:
        print(0)
        sys.exit()
    elif x+y==0:
        print('infinity')
        sys.exit()

    y=abs(y)
    if x%(y-x)==0: ans=(x//(y-x))*2
    else: ans=math.floor(x/(y-x))*2+1
    print(ans)


 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    t = list(map(int, input().split()))
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    diff_0 = a[0]-b[0]
    diff_1 = a[1]-b[1]

    if diff_0*diff_1 > 0:
        return 0
    
    d_one_iter = diff_0*t[0]+diff_1*t[1]

    if diff_0*d_one_iter > 0:
        return 0
    if diff_0*d_one_iter == 0:
        return 'infinity'
    
    if abs(diff_0*t[0])%abs(d_one_iter) == 0:
        return 2*abs(diff_0*t[0])//abs(d_one_iter)
    else:
        return 2*(abs(diff_0*t[0])//abs(d_one_iter))+1

if __name__ == "__main__":
    print(main())

Submission status: Accepted

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

D1 = T1 * A1 - T1 * B1
D2 = T1 * A1 + T2 * A2 - T1 * B1 - T2 * B2

if D1 * D2 > 0:
    print(0)
elif D2 == 0:
    print("infinity")
else:
    D1 = abs(D2 - D1)
    D2 = abs(D2)
    c = (D1 // D2) * 2 - 1
    if D1 % D2 == 0:
        c -= 1
    print(c)

Submission status: Accepted

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

A1 *= T1
B1 *= T1
A2 *= T2
B2 *= T2

if A1+A2 > B1+B2: # Aを遅く
    A1,B1 ,A2,B2 = B1,A1 ,B2,A2

if A1+A2 == B1+B2:
    print("infinity")
    exit()

if A1 < B1:
    print(0)
    exit()

def nibu(l,r):
    if l >= r:
        return l
    c = (l+r+1)//2
    if (A1+A2)*(c-1)+A1 >= (B1+B2)*(c-1)+B1:
        return nibu(c,r)
    else:
        return(nibu(l,c-1))

ans = nibu(0,10**100)
if (A1+A2)*(ans-1)+A1 == (B1+B2)*(ans-1)+B1:
    print(ans*2-2)
else:
    print(ans*2-1)

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
'''
def GI(V,E,DAG=False,index=0):
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[1:])
            bb=(inp[1:])
        g[a].append(b)
        if g[b].append(a)
        h.append(b)
'''

def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()
 
show_flg=False
show_flg=True

ans=0

s,t=LI()
a,c=LI()
b,d=LI()
if a*s+c*t<b*s+d*t:
    a,b,c,d=b,a,d,c
if a*s+c*t==b*s+d*t:
    print('infinity')
    exit()
p,q=(a-b)*s,(c-d)*t
if p>0 and p+q>0:
    print(0)
    exit()

#(p+q)*n+p<0
n=-p//(p+q)
ans=2*n+1
if -p%(p+q)==0:
    ans-=1

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
d1 = (b1 - a1) * t1
d2 = (b2 - a2) * t2
if d1 > 0:
    d1 = -d1
    d2 = -d2
if d1 + d2 < 0:
    print(0)
    exit()
if d1 + d2 == 0:
    print('infinity')
    exit()

ok, ng = 0, 10**20
while ng - ok > 1:
    m = (ok + ng) // 2
    if m * d2 + (m + 1) * d1 <= 0:
        ok = m
    else:
        ng = m
if ok * d2 + (ok + 1) * d1 == 0:
    print(2 * ok)
else:
    print(2 * ok + 1)

Submission status: Accepted

Solution code:

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

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

import sys
if d1+d2 == 0:
    print('infinity')
    sys.exit()
elif d1*d2 > 0 or (abs(d1)-abs(d2)>0 and d1*d2<0):
    print(0)
    sys.exit()
    
else:
    start = abs(d1)
    delta = abs(d2+d1)
    ans = start//delta * 2
    if (start%delta)!=0:
        ans += 1
    print(ans)

Submission status: Accepted

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

v1, v2 = b1 - a1, b2 - a2
x1, x2 = t1 * v1, t2 * v2
if x1 + x2 == 0:
  print('infinity')
else:
  if x1 < 0:
    x1, x2 = [x1 * -1, x2 * -1]

  if x1 + x2 > 0:
    print(0)
  else:
    dx = abs(x1 + x2)
    c = (x1 // dx) * 2 + 1
    if x1 % dx == 0:
      c -= 1
    
    print(c)

Submission status: Accepted

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

    if A1 < B1 and A2 < B2:
        return 0
    elif A1 > B1 and A2 > B2:
        return 0

    C1, C2 = B1-A1, B2-A2

    if C1 < 0:
        C1, C2 = -C1, -C2

    dist = C1*T1 + C2*T2
    if dist == 0:
        return 'infinity'
    elif dist > 0:
        return 0

    diff = C1*T1
    num = diff // abs(dist) * 2

    if diff % abs(dist):
        num += 1

    return num


print(solve())

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    t1, t2 = list(map(int, readline().split()))
    a1, a2 = list(map(int, readline().split()))
    b1, b2 = list(map(int, readline().split()))
    a1 -= b1
    a2 -= b2
    if a1 * t1 + a2 * t2 == 0:
        print("infinity")
        exit()
    elif a1 < 0:
        if a1 * t1 + a2 * t2 < 0:
            print(0)
            exit()
    elif a1 > 0:
        if a1 * t1 + a2 * t2 > 0:
            print(0)
            exit()
    s = abs(a1 * t1) // abs(a1 * t1 + a2 * t2)
    m = abs(a1 * t1) % abs(a1 * t1 + a2 * t2)
    print(s * 2 if m == 0 else s * 2 + 1)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

v1 = a1-b1
v2 = a2-b2

m1 = v1*t1
m2 = v2*t2

if m1>0:
    m1 *= -1
    m2 *= -1

if m1 == -m2:
    print('infinity')
elif m1+m2>0:
    ans = floor(-m1/(m2+m1)) * 2
    if m1%(m2+m1) != 0:
        ans += 1
    print(ans)
else:
    print(0)

Submission status: Accepted

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

C1 = (A1-B1)*T1
C2 = (A2-B2)*T2

if C1+C2==0:
    print('infinity')
elif C1*(C1+C2)>0:
    print(0)
else:
    if abs(C1)%abs(C1+C2)==0:
        print(abs(C1)//abs(C1+C2)*2)
    else:
        print(abs(C1)//abs(C1+C2)*2+1)

Submission status: Accepted

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
d1 = (A1 - B1) * T1
d2 = d1 + (A2 - B2) * T2
if (d1 > 0 and d2 > 0) or (d1 < 0 and d2 < 0):
    print(0)
    exit()
if d2 == 0:
    print('infinity')
    exit()
d1 = abs(d1)
d2 = abs(d2)
if d1 % d2 == 0:
    print(d1 // d2 * 2)
else:
    print(1 + d1 // d2 * 2)

Submission status: Accepted

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
flag = False
if ((T1*A1 > T1*B1) and (T1*A1 + T2*A2 <= T1*B1 + T2*B2)) or ((T1*A1 < T1*B1) and (T1*A1 + T2*A2 >= T1*B1 + T2*B2)):
  flag = True

if flag:
  a1 = abs(T1*A1 - T1*B1)
  a2 = abs(T1*A1+T2*A2-T1*B1-T2*B2)
  if a2 == 0:
    print("infinity")
  else:
    b1 = a1//a2
    b2 = a1 % a2
    if b2 == 0:
      print(a1//a2*2)
    else:
  	  print(1 + a1//a2*2)
else:
  print(0)

Submission status: Accepted

Solution code:
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 T1*A1+T2*A2==T1*B1+T2*B2:
    print("infinity")
elif ((A1>B1) & (T1*A1+T2*A2-(T1*B1+T2*B2)>0)) or ((A1<B1) & (T1*A1+T2*A2-(T1*B1+T2*B2)<0)):
    print(0)
else:
    d=abs(T1*A1+T2*A2-(T1*B1+T2*B2))#T1+T2だけ走った後離れている距離
    ss=0
    if abs(T1*(A1-B1))%d==0:
        ss=1
        
    print(abs(T1*(A1-B1))//d*2-ss+1)

Submission status: Accepted

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

LA = T1*A1+T2*A2
LB = T1*B1+T2*B2
if LA == LB:
    print("infinity")
    exit()
elif LA < LB:
    LA, LB = LB, LA 
    A1, B1 = B1, A1
    A2, B2 = B2, A2

l = LA - LB
r = T1*B1-T1*A1 
if r < 0:
    print(0)
    exit()
else:
    # print("===")
    # print(l,r)
    if r%l==0:
        print( (r//l)*2)
    else:
        print( (r//l)*2 +1 )

Submission status: Accepted

Solution code:
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

if diff1 > 0:
    diff1, diff2 = -1 * diff1, -1 * diff2

if diff1 + diff2 < 0:
    print(0)
elif diff1 + diff2 == 0:
    print('infinity')
else:
    q, r = divmod(-diff1, diff1 + diff2)
    print(2*q + (1 if r != 0 else 0))

Submission status: Accepted

Solution code:
import sys
import string
input = sys.stdin.readline

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

if A1 < B1 and A2 < B2:
    print(0)
    exit()
if A1 > B1 and A2 > B2:
    print(0)
    exit()
delta1 = T1 * A1 - T1 * B1
delta2 = T2 * A2 - T2 * B2
if (delta1 + delta2) / delta1 > 0:
    print(0)
    exit()
if delta1 + delta2 == 0:
    print('infinity')
    exit()
if delta1 % (delta1 + delta2) == 0:
    print(2 * abs(delta1 // (delta1 + delta2)))
else:
    print(2 * abs(delta1 // (delta1 + delta2)) - 1)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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())

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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())