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:
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
dA=T1*A1
dB=T1*B1
F=abs(DA-DB)
f=abs(dA-dB)
if F==0:
	print("infinity")
elif DA>DB:
	if dA>dB:
		print(0)
	else:
		if f%F==0:
			print((int(f//F))*2)
		else:
			print((int(f//F))*2+1)
else:
	if dA<dB:
		print(0)
	else:
		if f%F==0:
			print((int(f//F))*2)
		else:
			print((int(f//F))*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) * t1 == (a2 - b2) * t2:
    print("infinity")
    import sys
    sys.exit()

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

if p1 * p2 > 0:
    print(0)
    import sys
    sys.exit()

import math

if abs(p1)%abs(p2) == 0:
     print(2*math.ceil(abs(p1)/abs(p2)))
else:
    print(2*math.ceil(abs(p1)/abs(p2))-1)

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
c,d=map(int,input().split())
e,f=map(int,input().split())
if (c>e and d>f) or (c<e and d<f):
    print(0)
else:
    if c<e:
        c,d,e,f=e,f,c,d
    s=(c-e)*a
    t=(f-d)*b
    if s>t:
        print(0)
    elif s==t:
        print("infinity")
    else:
        print(int(s/(t-s))+int(t/(t-s))-(1 if s%(t-s)==0 else 0))

Submission status: Accepted

Solution code:
import sys
from itertools import combinations, permutations

def solve():
    input = sys.stdin.readline
    T1, T2 = map(int, input().split())
    A1, A2 = map(int, input().split())
    B1, B2 = map(int, input().split())
    Atotal = A1 * T1 + A2 * T2
    Btotal = B1 * T1 + B2 * T2

    if Atotal == Btotal: print("infinity")
    else:
        if A1 >= B1 and A2 >= B2: print(0)
        elif A1 <= B1 and A2 <= B2: print(0)
        else:
            if Atotal > Btotal:
                if A1 > B1: print(0) #Aが先行逃げ切りでBは追い付けない
                else: #Bが先行するがAが抜き返す
                    max_roop = (B1 - A1) * T1 // (Atotal - Btotal) 
                    cross = (1 if (B1 - A1) * T1 % (Atotal - Btotal) == 0 else 2)
                    count = 1
                    if max_roop > 0: 
                        count += 2 * (max_roop - 1) + cross
                    print(count)
            else:
                if B1 > A1: print(0) #Bの先行逃げ切りでAは追い付けない
                else: #Aが先行するがBが抜き返す
                    max_roop = (A1 - B1) * T1 // (Btotal - Atotal)
                    cross = (1 if (A1 - B1) * T1 % (Btotal - Atotal) == 0 else 2)
                    count = 1
                    if max_roop > 0:
                        count += 2 * (max_roop - 1) + cross
                    print(count)

    return 0

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())
a1=A1*T1
a2=A2*T2
b1=B1*T1
b2=B2*T2
if a1+a2==b1+b2:
    print("infinity")
else:
    if a1>b1 and a2<b2:
        if a1+a2<b1+b2:
            if (a1-b1)%(-a1-a2+b1+b2)==0:
                print((a1-b1)//(-a1-a2+b1+b2)*2)
            else:
                print((a1-b1)//(-a1-a2+b1+b2)*2+1)
        else:
            print(0)
    elif a1<b1 and a2>b2:
        if a1+a2>b1+b2:
            if (b1-a1)%(a1+a2-b1-b2)==0:
                print((b1-a1)//(a1+a2-b1-b2)*2)
            else:
                print((b1-a1)//(a1+a2-b1-b2)*2+1)
        else:
            print(0)
    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,c2 = b1-a1,b2-a2
d = t1*c1 + t2*c2
if d == 0:
  print('infinity')
elif c1 * c2 > 0:
  print(0)
else:
  if c1 < 0:
    c1 *= -1 
    c2 *= -1
    d  *= -1
  top = c1 * t1
  d = -d
  #print(top,d)
  if d < 0:
    print(0)
  elif top % d != 0:
    print(top//d*2+1)
  else:
    print(top//d*2)

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')
    exit()

if a1 < b1:
    a1, a2, b1, b2 = b1, b2, a1, a2
d1 = (a1-b1)*t1
d2 = (a2-b2)*t2
if d1 + d2 > 0:
    print(0)
    exit()
if d1 == d2:
    print('infinity')
    exit()

dd = abs(d1 + d2)
if d1 % dd == 0:
    print(d1 // dd*2)
else:
    print((d1+dd-1)//dd*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())

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


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


if A1 + A2 < B1 + B2:
    A1, A2, B1, B2 = B1, B2, A1, A2
D = (A1 + A2) - (B1 + B2)
Remain = B1 - A1

print(max(0, Remain // D * 2 + (Remain % D != 0)))

Submission status: Accepted

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

A1T1 = A1 * T1
A2T2 = A2 * T2
B1T1 = B1 * T1
B2T2 = B2 * T2
X = A1T1 - B1T1
Y = A2T2 - B2T2
Z = X + Y

if Z == 0:
    print("infinity")
elif X * Z > 0:
    print(0)
else:
    W = abs(X) // abs(Z)
    if (abs(X) / abs(Z)).is_integer():
        print(W * 2)
    else:
        print(W * 2 + 1)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
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')
else:
    x=(b1-a1)*t1
    y=(b2-a2)*t2
    if x*y>0:
        print(0)
    else:
        if x<0:
            x=-x
            y=-y
        if x+y>0:
            print(0)
        else:
            z=-(x+y)
            if x%z==0:
                print(2*(x//z))
            else:
                print(1+2*(x//z))

Submission status: Accepted

Solution code:
T1,T2=map(int,input().split())
A1,A2=map(int,input().split())
B1,B2=map(int,input().split())
a,b,c,d=T1*A1,T1*B1,T2*A2,T2*B2
if a+c==b+d:
    print('infinity')
else:
    if (a-b)*(a+c-b-d)>0:
        print(0)
    else:
        x,y=abs(a-b),abs(a+c-b-d)
        if x%y==0:
            print(x//y*2)
        else:
            print(x//y*2+1)

Submission status: Accepted

Solution code:
ts = list(map(int, input().split()))
vs = list(map(int, input().split()))
ws = list(map(int, input().split()))


def place(i):
    x = (ts[0] * vs[0] + ts[1] * vs[1]) * (i // 2)
    if i % 2 == 1:
        x += ts[0] * vs[0]

    y = (ts[0] * ws[0] + ts[1] * ws[1]) * (i // 2)
    if i % 2 == 1:
        y += ts[0] * ws[0]

    return x, y


x0, y0 = place(2)
if x0 == y0:
    print("infinity")
    exit()


def changed(i):
    x1, y1 = place(i)
    x2, y2 = place(i + 1)
    return x1 != y1 and (x2 == y2 or (x1 < y1) != (x2 < y2))


ok = 0
ng = 10 ** 20

while ng - ok > 1:
    mid = (ok + ng) // 2
    if changed(mid):
        ok = mid
    else:
        ng = mid

print(ok)

Submission status: Accepted

Solution code:
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split())
B1,B2 = map(int,input().split())
A1 -= B1
A2 -= B2
if A1 < 0:
    A1 *= -1
    A2 *= -1

if A1*T1 > -A2*T2:
    print(0)
elif A1*T1 == -A2*T2:
    print("infinity")
else:
    dx = -A2*T2 -A1*T1
    ans = (A1*T1)//dx *2 +1
    if (A1*T1)%dx == 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 - b1 >= 0) == (a2 - b2 >= 0):
    print(0)
    exit()

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

if d1 > d2:
    print(0)
    exit()
if d1 == d2:
    print('infinity')
    exit()

cnt = 0
D = d2 - d1
cnt += (d1 // D) * 2
if d1 % D != 0:
    cnt += 1

print(cnt)

Submission status: Accepted

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

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

Dtot = (A1*T1+A2*T2) - (B1*T1+B2*T2)
Dmin = (B1*T1) - (A1*T1)

ans = 2*((Dmin-1)//Dtot) + 1
if Dmin%Dtot==0: ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
t_1,t_2=map(int,input().split())
a_1,a_2=map(int,input().split())
b_1,b_2=map(int,input().split())
if a_1==b_1:
    print("infinity")
    sys.exit()
    
x=a_1*t_1
y=a_2*t_2
w=b_1*t_1
z=b_2*t_2
if x+y==w+z:
    print("infinity")
    sys.exit()

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

Submission status: Accepted

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

v1 = A1-B1
v2 = A2-B2

if v1==0:
    print("infinity")
elif v2==0:
    print(0)
elif v1>0 and v2>0:
    print(0)
elif v1<0 and v2<0:
    print(0)
else:
    cycle = v1*T1+v2*T2
    if cycle==0:
        print("infinity")
    elif v1 > 0 and cycle > 0:
        print(0)
    elif v1 < 0 and cycle < 0:
        print(0)
    else:
        n = -(v1*T1) // cycle
        if abs(v1*T1)%abs(cycle)!=0:
            print(2*n+1)
        else:
            print(2*n)

Submission status: Accepted

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
sA = T1*A1+T2*A2
sB = T1*B1+T2*B2
if sA == sB:
  print("infinity")
  exit()
hA = T1*A1
hB = T1*B1
if sA > sB:
  hA, sA, hB, sB = hB, sB, hA, sA
#print(hA, sA, hB, sB)
if hA < hB:
  print(0)
  exit()
if (hA-hB)%(sB-sA) == 0:
  r = 2*((hA-hB)//(sB-sA))
else:
  r = 2*((hA-hB)//(sB-sA))+1
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())
x1 = (a1 - b1) * t1
x2 = (a2 - b2) * t2
if (x1 > 0 and x2 > 0) or (x1 < 0 and x2 < 0) or abs(x1) > abs(x2):
  print(0)
elif x1 + x2 == 0:
  print('infinity')
else:
  diff = x1 + x2
  ans = 1 + abs(x1) // abs(diff) * 2
  if abs(x1) % abs(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())



Taka_end1 = t1*a1
Taka_end2 = t2*a2
Taka_end_all = Taka_end1+Taka_end2

Taka = [t1*a1,t2*a2,Taka_end_all]

Aok_end1 = t1*b1
Aok_end2 = t2*b2
Aok_end_all = Aok_end1+Aok_end2

Aok = [t1*b1,t2*b2,Aok_end_all]



Taka_fast = False
if Taka_end1 > Aok_end1:
    Taka_fast = True

if Taka_fast:
    d1 = Taka_end1-Aok_end1
    d2 = Aok_end_all-Taka_end_all

    # print("Taka")
    # print(d1,d2)

    if d2 < 0:
        print(0)
    elif d2 == 0:
        print("infinity")
    else:
        if d1 % d2 == 0:
            print((d1//d2)*2)
        else:
            print((d1 // d2) * 2 + 1)
else:
    d1 = -(Taka_end1-Aok_end1)
    d2 = -(Aok_end_all-Taka_end_all)
    #
    # print("Aok")
    # print(d1,d2)

    if d2 < 0:
        print(0)
    elif d2 == 0:
        print("infinity")
    else:
        if d1 % d2 == 0:
            print((d1 // d2) * 2)
        else:
            print((d1 // d2)*2 + 1)

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())
    D1 = (B1 - A1) * T1
    D2 = (B2 - A2) * T2
    if D1 + D2 == 0:
        print('infinity')
    elif (D1 >= 0 and D2 >= 0) or (D1 <= 0 and D2 <= 0):
        print(0)
    elif (D1 >= 0 and D1 + D2 >= 0) or (D1 <= 0 and D1 + D2 <= 0):
        print(0)
    else:
        diff = abs(D1 + D2)
        top = abs(D1)
        if top % diff == 0:
            n = top // diff
            ans = 1 + 2 * n - 1
        else:
            n = top // diff
            ans = 1 + 2 * n
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
T = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
if A[0]*T[0]+A[1]*T[1] == B[0]*T[0]+B[1]*T[1]:
  print('infinity')
else:
  if A[0]*T[0]+A[1]*T[1] < B[0]*T[0]+B[1]*T[1]:
    C = A
    A = B
    B = C
  if A[0] > B[0]:
    print('0')
  else:
    bcover = (B[0]-A[0])*T[0]
    tcover = A[0]*T[0]+A[1]*T[1] - B[0]*T[0]-B[1]*T[1]
    if bcover%tcover == 0:
      print(int(bcover/tcover)*2)
    else:
      print(int(bcover/tcover)*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 t1*a1+t2*a2 == t1*b1+t2*b2 or a1 == b1:
    print('infinity')
    exit()

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



if a1*t1 + a2*t2 > b1*t1+b2*t2:
    print(0)
    exit()

res = 1
a = a1*t1
b = b1*t1
#Aの方が上
judge = True

c = a1*t1-b1*t1
d = -c - a2*t2 + b2*t2

if d > c:
    print(1)
    exit()

if c%d == 0:
    res -= 1

res += (c//d)*2

print(res)

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")
elif (T1*A1-T1*B1)*((T1*A1+T2*A2)-(T1*B1+T2*B2)) > 0:
    print("0")
else:
    if A1>B1:
        hoge = ((A1-B1)*T1)//(B1*T1+B2*T2-A1*T1-A2*T2) * 2 + 1
        if ((A1-B1)*T1)%(B1*T1+B2*T2-A1*T1-A2*T2) == 0:
            hoge -= 1
    else:
        hoge = ((B1-A1)*T1)//(A1*T1+A2*T2-B1*T1-B2*T2) * 2 + 1
        if ((B1-A1)*T1)%(A1*T1+A2*T2-B1*T1-B2*T2) == 0:
            hoge -= 1
    print(hoge)

Submission status: Accepted

Solution code:
from math import floor, ceil

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

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

if diff == 0:
    print("infinity")
elif diff > 0:
    print(0)
else:
    c = T1 * (B1 - A1) / (- diff)
    ans = floor(c) + ceil(c)
    print(ans)

Submission status: Accepted

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	T1, T2 = getlist()
	A1, A2 = getlist()
	B1, B2 = getlist()
	if A1 * T1 + A2 * T2 == B1 * T1 + B2 * T2:
		print("infinity")
		return

	if A1 - B1 > 0 and A1 * T1 + A2 * T2 > B1 * T1 + B2 * T2:
		print(0)
		return

	if B1 - A1 > 0 and A1 * T1 + A2 * T2 < B1 * T1 + B2 * T2:
		print(0)
		return

	x = A1 - B1
	y = A2 - B2
	if x > 0:
		ans = 0
		dif = -y * T2 - x * T1
		if (x * T1) % dif == 0:
			ans += 2 * int((x * T1) // dif)
		else:
			ans += 2 * int((x * T1) // dif) + 1
		print(ans)
		return

	if x < 0:
		ans = 0
		dif = y * T2 + x * T1
		if (-x * T1) % dif == 0:
			ans += 2 * int((-x * T1) // dif)
		else:
			ans += 2 * int((-x * T1) // dif) + 1
		print(ans)
		return


if __name__ == '__main__':
	main()

Submission status: Accepted

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

def calc(t1, t2, a1, a2, b1, b2):
    x1 = a1*t1 + a2*t2
    x2 = b1*t1 + b2*t2
    if x1 == x2:
        return 'infinity'
    if x1 > x2:
        a1, a2, b1, b2, x1, x2 = b1, b2, a1, a2, x2, x1
    y1 = a1*t1
    z1 = b1*t1
    
    if y1 < z1:
        return 0
    res = 1
    ok = 0
    ng = 10**20
    while abs(ng - ok) > 1:
        med = (ng + ok)//2
        if med*x1 + y1 >= med*x2 + z1:
            if med*x1 + y1 == med*x2 + z1:
                res = 0
            ok = med
        else:
            ng = med
    return res + 2*ok
        
        
T1, T2 = map(int, readline().split())
A1, A2 = map(int, readline().split())
B1, B2 = map(int, readline().split())

print(calc(T1, T2, A1, A2, B1, B2))

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())

a = A1-B1
b = A2-B2

d = T1*a + T2*b

if d == 0:
    print('infinity')
    exit()
if a*b > 0 or a*d > 0:
    print(0)
    exit()

x = abs(T1*a)
d = abs(d)

cnt = (x+d-1)//d
if cnt*d == x:
    cnt = 2*cnt
else:
    cnt = 2*cnt-1

print(cnt)

Submission status: Accepted

Solution code:
import sys; readline = sys.stdin.readline
from collections import deque

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

T = t1 + t2

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

if avgA < avgB:
    a1, a2, b1, b2 = b1, b2, a1, a2
    avgA, avgB = avgB, avgA

d1 = a1 - b1
d2 = a2 - b2

if d1 > 0:
    print(0)
    exit(0)

y0 =  -d1 * t1
dy = (avgA - avgB)

n = y0 // dy

if y0 % dy == 0:
    print((n + 1) * 2 - 2)
else:
    print((n + 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 = t1 * (a1-b1)
n = t2 * (a2-b2)

ans = 0
one = p + n

if one == 0:
    ans = "infinity"


elif one < 0:
    if p > 0:
        ans += (p//(-one)) * 2 + 1
        if p % one == 0:
            ans -= 1

else:
    if p < 0:
        ans += ((-p) // one) * 2 + 1
        if p % one == 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())

def ispos(x):
    return x==abs(x)

first = (a1-b1)*t1
second = (a2-b2)*t2
net = first+second
if net == 0:
    print("infinity")
elif ispos(first) == ispos(net):
    print(0)
else:
    res = 1
    first = -abs(first)
    second = abs(second)
    net = first+second
    res += 2*int(abs(first)/net)
    if first%net == 0:
        res -= 1
    print(res)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    t1,t2=map(int,input().split())
    a1,a2=map(int,input().split())
    b1,b2=map(int,input().split())
    v1=a1-b1
    v2=a2-b2

    # v1とv2が同符号なら0回
    if(v1*v2>0):
        print(0)
        return
    # v1t1+v2t2 が0ならば無限
    if(v1*t1+v2*t2==0):
        print("infinity")
        return
    # abs(v1t1)>abs(v2t2)ならば0回
    if(abs(v1*t1)>abs(v2*t2)):
        print(0)
        return

    # 以下、v1を負の方の大きさ、v2を正の方の大きさとする
    v1=abs(v1); v2=abs(v2)
    d=v2*t2-v1*t1

    ans=-1
    ans-=(v1*t1%d==0)
    k=(v1*t1-1)//d+1+(v1*t1%d==0)
    ans+=2*k
    print(ans)
resolve()

Submission status: Accepted

Solution code:
list1 = list(map(int, input().split()))
list2 = list(map(int, input().split()))
list3 = list(map(int, input().split()))
x=list1[0]
y=list1[1]
a=list2[0]
b=list2[1]
c=list3[0]
d=list3[1]
if a * x + b * y == c * x + d * y:
  print("infinity")
else:
  if a * x + b * y < c * x + d * y:
    a,c=c,a
    b,d=d,b
  t = a * x + b * y - (c * x + d * y)
  if a>c:
    print(0)
  else:
    k=x*(c-a)
    if k%t==0:
      print((k//t)*2)
    else:
      print((k//t)*2+1)

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())
    a = A1 * T1 + A2 * T2
    b = B1 * T1 + B2 * T2
    if a == b:
        print("infinity")
        return
    if a < b:
        a, b = b, a
        A1, B1 = B1, A1
        A2, B2 = B2, A2
    if A1 > B1:
        print(0)
        return
    ans = (B1 - A1) * T1 // (a - b) * 2
    if (B1 - A1) * T1 % (a-b) != 0:
        ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdout
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

t1,t2 = inm()
a1,a2 = inm()
b1,b2 = inm()
c = (a1-b1)*t1
d = (a2-b2)*t2
if c>0 and d>0 or c<0 and d<0 or abs(c)>abs(d):
    print(0)
    exit()
ac = abs(c)
e = abs(d)-ac
if e==0:
    print('infinity')
else:
    print((ac//e)*2 + (0 if ac%e == 0 else 1))

Submission status: Accepted

Solution code:
def solve(t1, t2, a1, a2, b1, b2):
    sa = a1 * t1 + a2 * t2
    sb = b1 * t1 + b2 * t2
    if sa == sb:
        return "infinity"
    if a1 > b1:
        if sa >= sb:
            return 0
        else:
            d0 = a1 * t1 - b1 * t1
            d1 = sb - sa
            res = (d0 // d1 + 1) * 2 - 1
            if d0 % d1 == 0:
                res -= 1
    else:
        if sa <= sb:
            return 0
        else:
            d0 = b1 * t1 - a1 * t1
            d1 = sa - sb
            res = (d0 // d1 + 1) * 2 - 1
            if d0 % d1 == 0:
                res -= 1
    return res


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


def test():
    assert solve(1, 2, 10, 10, 12, 4) == 1
    assert solve(100, 1, 101, 101, 102, 1) == "infinity"
    assert solve(12000, 15700, 3390000000, 3810000000, 5550000000, 2130000000) == 113


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

Submission status: Accepted

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

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

s = t1*c1
t = s + t2*c2
# print(c1,c2,s,t)
ans = -1
if s * t > 0:
    ans = 0
elif t == 0:
    ans=-10
else:
    if s > t:
        tt = abs(t)
        ans = -((-s / tt)//1)*2 - 1
        if s / tt - s//tt < 10**-25:
            ans += 1
    else:
        ss = abs(s)
        ans = -((-ss / t)//1)*2 - 1
        if ss / t -ss//t < 10**-25:
            ans += 1
if ans == -10:
    print("infinity")
else:
    print(int(ans))

Submission status: Accepted

Solution code:
def main():
    from math import ceil
    t1,t2=map(int,input().split())
    a1,a2=map(int,input().split())
    b1,b2=map(int,input().split())
    run1=a1*t1-b1*t1
    run2=a2*t2-b2*t2
    #run1,run2=-4,6
    sum_=run1+run2
    #print(run1,run2,sum_)
    if run1>0 and run2>0:
        print(0)
    elif run1<0 and run2<0:
        print(0)
    else:
        if abs(run1)>abs(run2):
            print(0)
        elif abs(run1)==abs(run2):
            print("infinity")
        else:
            if abs(run1)%abs(sum_)==0:
                print(2*ceil(abs(run1)/abs(sum_)))
            else:
                print(2*ceil(abs(run1)/abs(sum_))-1)
main()

Submission status: Accepted

Solution code:
import sys
# input = sys.stdin.buffer.readline
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getlist():
    return list(map(int, input().split()))
import heapq
import bisect
import copy
# import math
def warshall_floyd(d, N):
    for k in range(N):
        for i in range(N):
            for j in range(N):
                d[i * N +j] = min(d[i * N +j], d[i * N + k] + d[k * N + j])
    return d

MOD = 10**9 + 7

def main():
    t1, t2 = getlist()
    a1, a2 = getlist()
    b1, b2 = getlist()
    if (a1 > b1 and a2 > b2) or (a1 < b1 and a2 < b2):
        print(0)
        return

    if t1 * a1 + t2 * a2 == b1 * t1 + b2 * t2:
        print("infinity")
        return

    if t1 * a1 + t2 * a2 < b1 * t1 + b2 * t2:
        # 多く走る方がa
        a1, a2, b1, b2 = b1, b2, a1, a2


    if a1 < b1:
        gap = (b1 - a1) * t1
        totalgap = (t1 * a1 + t2 * a2) - (b1 * t1 + b2 * t2)
        # print(gap, totalgap)
        ans = 1 + (gap // totalgap) * 2 - (gap % totalgap == 0)
        print(ans)

    else:
        print(0)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import random
from operator import itemgetter
import re
import sys
from itertools import accumulate
from collections import defaultdict
from collections import deque
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
input = sys.stdin.readline
T =[int(i) for i in input().split()]
A =[int(i) for i in input().split()]
B =[int(i) for i in input().split()]
if (T[0] *A[0] +T[1] * A[1]) == (T[0] *B[0] +T[1] * B[1]):
    print("infinity")
elif A[0] > B[0] and (T[0] *A[0] +T[1] * A[1]) > (T[0] *B[0] +T[1] * B[1]):
    print(0)
elif A[0] < B[0] and (T[0] *A[0] +T[1] * A[1]) < (T[0] *B[0] +T[1] * B[1]):
    print(0)
else:
    adv = abs(T[0] *A[0]-T[0] *B[0])
    diff = abs((T[0] *A[0] +T[1] * A[1]) - (T[0] *B[0] +T[1] * B[1]))
    if adv%diff == 0:
        ans = (adv//diff) *2
    else:
        ans = (adv//diff) *2+1
    print(ans)

Submission status: Accepted

Solution code:
from sys import stdin


def input():
    return stdin.readline()[:-1]


def intput():
    return int(input())


def sinput():
    return input().split()


def intsput():
    return map(int, sinput())


# Code

t1, t2 = intsput()

a1, a2 = intsput()

b1, b2 = intsput()

leap1 = a1 * t1 + a2 * t2
leap2 = b1 * t1 + b2 * t2

if leap1 == leap2:
    print('infinity')
    exit()


if leap2 < leap1:
    a1, a2, b1, b2 = b1, b2, a1, a2
    leap1, leap2 = leap2, leap1

lag = leap2 - leap1

# 1 is always slower
if a1 < b1 and a2 < b2:
    print(0)
    exit()

if a1 > b1:
    gap = (a1 - b1) * t1
    q = gap // lag
    ans = q * 2 + 1
    if lag * q == gap:
        ans -= 1
    print(ans)
    exit()

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())

if t1*a1+t2*a2 == t1*b1+t2*b2:
	print("infinity")
elif (a1-b1) * (t1*(a1-b1) + t2*(a2-b2)) > 0:
	print(0)
else:
	if a1 > b1:
		x = (a1-b1)*t1
		y = (b2-a2)*t2 - x
	else:
		x = (b1-a1)*t1
		y = (a2-b2)*t2 - x

	ans = 1 + (x//y)*2
	if x%y == 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())
if A1*T1+A2*T2 == B1*T1+B2*T2:
    print('infinity')  
    sys.exit()
d1 = A1*T1-B1*T1
d2 = A2*T2-B2*T2
d = d1+d2
if d1<0:
    d = -d
    d1 = -d1
    d2 = -d2
if d>0:
    print(0)
    sys.exit()
d = -d
if d1%d == 0:
    print(d1//d*2)
else:
    ans = d1//d*2+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())

def cross_foo(turn):
  posA1 = (T1 * A1 + T2 * A2) * (turn - 1)
  posB1 = (T1 * B1 + T2 * B2) * (turn - 1)
  posA2 = posA1 + T1 * A1
  posB2 = posB1 + T1 * B1
  if (posA1 - posB1 == 0): return False
  return ((posA1 - posB1) * (posA2 - posB2) <= 0)

def cross_bar(turn):
  posA1 = (T1 * A1 + T2 * A2) * (turn - 1) + T1 * A1;
  posB1 = (T1 * B1 + T2 * B2) * (turn - 1) + T1 * B1;
  posA2 = posA1 + T2 * A2;
  posB2 = posB1 + T2 * B2;
  if (posA1 - posB1 == 0): return False
  return ((posA1 - posB1) * (posA2 - posB2) <= 0)

def main():
  if (T1 * A1 + T2 * A2 == T1 * B1 + T2 * B2):
    print("infinity")
    return

  if (not cross_bar(1)):
    print(0)
    return

  if (not cross_foo(2)):
    print(1)
    return

  ans = 0

  foo_ok = 2
  foo_ng = 1
  for i in range(110):
    foo_ng *= 10
  while (foo_ok < foo_ng - 1):
    mid = int((foo_ok + foo_ng) / 2)
    if (cross_foo(mid)): foo_ok = mid
    else: foo_ng = mid
  ans += foo_ok - 1

  bar_ok = 1
  bar_ng = 1
  for i in range(110):
    bar_ng *= 10
  while (bar_ok < bar_ng - 1):
    mid = int((bar_ok + bar_ng) / 2)
    if (cross_bar(mid)): bar_ok = mid
    else: bar_ng = mid;
  ans += bar_ok

  print(ans)

main()

Submission status: Accepted

Solution code:
t1,t2 = map(int, input().split())
a1,a2 = map(int, input().split())
b1,b2 = map(int, input().split())
diff = (t1 * a1 + t2 * a2) - (t1 * b1 + t2 * b2)
diff1 = t1 * (a1-b1)
diff2 = t1 * (a2-b2)
if diff == 0:
  print("infinity")
else:
  if (diff < 0 and diff1 < 0) or (diff > 0 and diff1 > 0):
    print(0)
  else:
    ans = 1
    if diff1 > 0:
      cnt = diff1 // -diff
      if not diff1 % -diff:
        ans += cnt * 2 - 1
      else:
        ans += cnt * 2
    else:
      cnt = -diff1 // diff
      if not -diff1 % diff:
        ans += cnt * 2 - 1
      else:
        ans += cnt * 2 
    print(ans)

Submission status: Accepted

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

def binsearch(l, r, pred):  # [l, r)
    assert l < r
    l -= 1
    while r - l > 1:
        m = (l + r) // 2
        if pred(m):
            r = m
        else:
            l = m
    return r

def solve(t1, t2, a1, a2, b1, b2):
    assert a1 != b1
    assert a2 != b2
    if a1 > b1:
        a1, b1 = b1, a1
        a2, b2 = b2, a2
    assert a1 < b1
    if a1 * t1 + a2 * t2 < b1 * t1 + b2 * t2:
        return 0
    elif a1 * t1 + a2 * t2 == b1 * t1 + b2 * t2:
        return 'infinity'
    else:

        def f(k):
            x0 = (a1 * t1 + a2 * t2) * k
            y0 = (b1 * t1 + b2 * t2) * k
            x1 = x0 + a1 * t1
            y1 = y0 + b1 * t1
            x2 = x1 + a2 * t2
            y2 = y1 + b2 * t2
            if y0 <= x0 and x1 < y1 and y2 < x2:
                return 2
            elif y0 <= x0 and x1 < y1 and y2 <= x2:
                return 1
            elif y0 <= x0 and x1 == y1:
                return 1
            else:
                return 0
        def pred(k):
            return f(k) == 0
        k = binsearch(0, 10 ** 18, pred)
        return 2 * (k - 1) + f(k - 1) - 1

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

if __name__ == '__main__':
    main()