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: Wrong Answer

Solution code:
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)
    # T2分間は追いつかれる
    elif (b1 - a1) * t1 < 0 and (b2 - a2) * t2 > 0:
        print(0)
    # T1分間は追いつかれる
    else:
        cross = ceil((b1-a1)*t1 / ((a1-b1)*t1 + (a2-b2)*t2)) - 1
        print(2 * cross + 1)

# 高橋君が遅れる
else:
    # ずっと高橋君のほうが遅い
    if (b1 - a1) * t1 > 0 and (b2 - a2) * t2 > 0:
        print(0)
    # T2分間は追い上げる
    elif (b1 - a1) * t1 > 0 and (b2 - a2) * t2 < 0:
        print(0)
    # T1分間は追い上げる
    else:
        cross = ceil((b1-a1)*t1 / ((a1-b1)*t1 + (a2-b2)*t2)) - 1
        print(2 * cross + 1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ptr1 = (b1 - a1) * t1
ptr2 = ptr1 + (b2 - a2) * t2
ptr3 = ptr2 + (b1 - a1) * t1
ptr4 = ptr3 + (b2 - a2) * t2

if ptr2 == 0:
  ans = 'infinity'
elif ptr1 * ptr2 > 0:
  ans = 0
else:
  ans = 1 + 2 * (abs(ptr1) // abs(ptr2))

print(ans)

Submission status: Wrong Answer

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:
	delo = t1 * b1 - t1 * a1
	delw = t1 * a1 + t2 * a2 - t1 * b1 - t2 * b2
	l = 0
	r = 10**36
	while l < r - 1:
		m = (l + r) // 2
		val1 = m // 2 * a2 * t2 + (m + 1) // 2 * a1 * t1
		val2 = (m + 1) // 2 * b1* t1 + m // 2 * b2 * t2
		val3 = (m + 1) // 2 * a2 * t2 + (m + 2) // 2 * a1 * t1
		val4 = (m + 2) // 2 * b1 * t1 + (m + 1) // 2 * b2 * t2
		if val1 <= val2 and val3 >= val4 or val1 >= val2 and val3 <= val4:
			l = m
		else:
			r = m
	print(l)

Submission status: Wrong Answer

Solution code:
T = [int(t) for t in input().split()]
A = [int(t) for t in input().split()]
B = [int(t) for t in input().split()]

Taka = T[0]*A[0]
Aoki = T[0]*B[0]
if Taka > Aoki:
    prev = "Taka"
else:
    prev = "Aoki"
    
idx = 1
ans = 0
m = 10**10
while True:
    Taka += T[idx]*A[idx]
    Aoki += T[idx]*B[idx]
    idx += 1
    idx %= 2
    if prev == "Taka" and Taka < Aoki:
        prev = "Aoki"
        ans += 1
    elif prev == "Aoki" and Taka > Aoki:
        prev = "Taka"
        ans += 1
    elif Taka == Aoki:
        ans = "infinity"
        break
    else:
        break
    
    if Taka > m and Aoki > m:
        q = min(Taka//m, Aoki//m)
        Taka -= q*m
        Aoki -= q*m
        
print(ans)

Submission status: Wrong Answer

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()
a1,a2=t1*a1,t2*a2
b1,b2=t1*b1,t2*b2
if a1+a2>b1+b2:a1,a2,b1,b2=b1,b2,a1,a2
d=b1+b2-a1-a2
print(int(0--(a1-b1+0.5)//d)*2-1)

Submission status: Wrong Answer

Solution code:
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 v1 % d == 0:
    ans += 1
print(ans)

Submission status: Wrong Answer

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

if y == 0:
    ans = 'infinity'
else:
    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 - 2
    else:
        ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
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)
    # T2分間は追いつかれる
    elif (b1 - a1) * t1 < 0 and (b2 - a2) * t2 > 0:
        print(0)
    # T1分間は追いつかれる
    else:
        cross = ceil((b1-a1)*t1 / ((a1-b1)*t1 + (a2-b2)*t2)) - 1
        print(2 * cross + 1)

# 高橋君が遅れる
else:
    # ずっと高橋君のほうが遅い
    if (b1 - a1) * t1 > 0 and (b2 - a2) * t2 > 0:
        print(0)
    # T2分間は追い上げる
    elif (b1 - a1) * t1 > 0 and (b2 - a2) * t2 < 0:
        print(0)
    # T1分間は追い上げる
    else:
        cross = ceil((b1-a1)*t1 / ((a1-b1)*t1 + (a2-b2)*t2))
        print(2 * cross + 1)

Submission status: Wrong Answer

Solution code:
import math

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

if t[0]*a[0]+t[1]*a[1]==t[0]*b[0]+t[1]*b[1]:
    print('infinity')
    exit()
elif t[0]*a[0]==t[0]*b[0]:
    print('infinity')
    exit()

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

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*n+1)
        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*n+1)
        else:
            print(2*math.ceil(n)-1)

Submission status: Wrong Answer

Solution code:
import math


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


def read_list():
    return list(read_values())


def main():
    T1, T2 = read_values()
    A1, A2 = read_values()
    B1, B2 = read_values()

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

    D1 = (A1 - B1) * T1
    D2 = (A2 - B2) * T2

    if D1 * D2 > 0:
        print(0)
        return

    D1 = abs(D1)
    D2 = abs(D2)

    if D1 > D2:
        print(0)
        return

    i = math.ceil(D1 / (D2 - D1))
    print(1 + 2 * (i - 1))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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]
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
l = t1*(v1[s]-v1[f])
if l < 0:
  print(0)
  exit()
tm = int(l//d)
if l%d == 0:
  print(2*tm)
else:
  print(1+2*tm)

Submission status: Wrong Answer

Solution code:
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
import math
f = (a1-b1)*t1
s = (a2-b2)*t2
if f == -s:
  print("infinity")
elif (f>0 and s>0) or (f<0 and s<0):
  print(0)
elif f//(-(f+s)) == math.ceil(f/(-(f+s))):
  print(f//(-(f+s))*2)
else:
  print(f//(-(f+s))*2+1)

Submission status: Wrong Answer

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() ]
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
        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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import itertools
import sys

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

taka = t1*a1
aoki = t1*b1
ans = 0
if(t1*a1+t2*a2==t1*b1+t2*b2):
    print("infinity")
    sys.exit()

for i in range(2,100000):
    prev_taka = taka
    prev_aoki = aoki
    if(i%2==1):
        taka += t1*a1
        aoki += t1*b1
    else:
        taka += t2*a2
        aoki += t2*b2
    if((prev_taka-prev_aoki)*(taka-aoki)<0):
        ans += 1
    else:
        pass
#    print("taka",taka)
#    print("aoki",aoki)

print(ans)

Submission status: Wrong Answer

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:
	delo = t1 * b1 - t1 * a1
	delw = t1 * a1 + t2 * a2 - t1 * b1 - t2 * b2
	l = 0
	r = 10**18
	while l < r - 1:
		m = (l + r) // 2
		val1 = m // 2 * a2 * t2 + (m + 1) // 2 * a1 * t1
		val2 = (m + 1) // 2 * b1* t1 + m // 2 * b2 * t2
		val3 = (m + 1) // 2 * a2 * t2 + (m + 2) // 2 * a1 * t1
		val4 = (m + 2) // 2 * b1 * t1 + (m + 1) // 2 * b2 * t2
		if val1 <= val2 and val3 >= val4 or val1 >= val2 and val3 <= val4:
			l = m
		else:
			r = m
	print(l)

Submission status: Wrong Answer

Solution code:
T = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
A1 = A[0]*T[0]
A2 = A[1]*T[1]
B1 = B[0]*T[0]
B2 = B[1]*T[1]
D1 = A1-B1
D2 = A2-B2
if D1*D2>0:
    output = "infinity"
elif (D1==0)and(D2==0):
    output = "infinity"
elif D1+D2==0:
    output = "infinity"
elif abs(D1)>abs(D2):
    output = 0
else:
    Z = abs(D1+D2)
    M1 = abs(D1)
    M2 = abs(D2)
    kanzen = M1//Z
    if Z*kanzen==M1:
        output = kanzen*2
    else:
        output = kanzen*2+1
print(output)

Submission status: Wrong Answer

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

delta1 = (a1 - b1) * t1
delta2 = (a2 - b2) * t2

if delta1 + delta2 == 0:
    print('infinity')
    exit()

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

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

steps = delta1 + delta2

ans = 2 * (min(abs(delta1), abs(delta2)) // steps) + 1
print(ans)

Submission status: Wrong Answer

Solution code:
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
    # print(f"k: {k}, x: {x}, y: {y}")
    #print(f"k: {k}, x+: {x + a1}, y+: {y + b1}")
    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
    else:
        return y + b2 >= x + a2

if a == b:
    ans = "infinity"
else:
    # print(bis(p, -1, 10**16))
    # print(bis(p2, -1, 10**16))
    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)

# print('-------')

# x = 0
# y = 0
# for i in range(114):
#     x += a1
#     y += b1
#     #print(x, y)
#     print(f"{i} {x < y}")
#     x += a2
#     y += b2
#     print(f"{i} {x < y}")
#     #print(x, y)

Submission status: Wrong Answer

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 and c2>0):
	print("0")
	sys.exit()


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


ans=(pitch//diff+1)*2-1

if(pitch%diff==0):
	ans-=1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    t1, t2 = LI()
    a1, a2 = LI()
    b1, b2 = LI()

    if a1 < b1:
        tmp = b1
        b1 = a1
        a1 = tmp

        tmp = b2
        b2 = a2
        a2 = tmp

    l1 = (a1 - b1) * t1
    l2 = (a2 - b2) * t2
    l3 = l1 + l2

    if l3 == 0:
        print("infinity")
        return
    if l3 > 0:
        print(0)
        return
    ans = 1
    print(ans + 2 * (l1 // (-l3)))






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    t1, t2 = LI()
    a1, a2 = LI()
    b1, b2 = LI()

    if a1 < b1:
        tmp = b1
        b1 = a1
        a1 = tmp

        tmp = b2
        b2 = a2
        a2 = tmp

    l1 = (a1 - b1) * t1
    l2 = (a2 - b2) * t2
    l3 = l1 + l2
    print(l1, l2)
    if l3 == 0:
        print("infinity")
        return
    if l3 > 0:
        print(0)
        return
    ans = 1
    print(ans + 2 * (l1 // (-l3)))






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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() :
    ret = -float('inf')
    
    if T1 * A1 + T2 * A2 == T1 * B1 + T2 * B2 :
        return 'infinity'
        
    else :
        a = T1 * A1
        b = T1 * B1
        d1 = a - b
        
        a += T2 * A2
        b += T2 * B2
        d2 = b - a
        if d2 < 0 :
            return 0
            
        a += T1 * A1
        b += T1 * B1
        d3 = a - b
        if d3 < 0 :
            return 1
            
        a += T2 * A2
        b += T2 * B2
        d4 = b - a
        if d4 < 0 :
            return 2
        
        if d3 < d1 :
            d31 = d1 - d3
            x = d1 // d31
            return x * 2 + 1
        else :
            d42 = d4 - d2
            x = d2 // d42
            return x * 2 + 2
        
print(solve())

Submission status: Wrong Answer

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

ta1 = T1 * A1
ta2 = T2 * A2
ao1 = T1 * B1
ao2 = T2 * B2
diff = 0
cnt = 0
sw = 0
if ta1 + ta2 == ao1 + ao2:
    print('infinity')
else:
    while abs(diff) < max(abs(ta1 - ao1), abs(ta2 - ao2)):
        if sw == 0:
            tmp = ta1 - ao1
            difff = diff
            diff = diff + tmp
            if difff * diff < 0:
                cnt += 1
        else:
            tmp = ta2 - ao2
            difff = diff
            diff = diff + tmp
            if difff * diff < 0:
                cnt += 1
        sw = 1 - sw

    print(cnt)

Submission status: Wrong Answer

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


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

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

# print (bunshi)
# print (bunbo)

n = 1 + bunshi / bunbo

print (int(n) * 2 - 1)

Submission status: Wrong Answer

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

if y == 0:
    ans = 'infinity'
else:
    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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


def main():
	t1,t2=map(int,input().split())
	a1,a2=map(int,input().split())
	b1,b2=map(int,input().split())
	
	a=a1*t1
	aa=a2*t2
	b=b1*t1
	bb=b2*t2
	
	if a+aa==b+bb:
		print("infinity")
		exit(0)
	elif a<b and aa<bb:
		print(0)
		exit(0)
	elif a > b and aa > bb:
		print(0)
		exit(0)
	
	else:
		x=abs(a-b)
		y=abs((a+aa)-(b+bb))
		print((x//y)*2+1-(x%y==0))
		
	
	
	
	
	
	
	

	
if __name__ == "__main__":
		main()

Submission status: Wrong Answer

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

L1 = t1*a1+t2*a2
L2 = t1*b1+t2*b2
if L1 == L2:
    print('infinity')
    exit()
flg = 1 if L1 < L2 else 0
ok = 0
ng = 10**15+1
mid = (ok + ng)//2
while ng - ok > 1:
    #print(mid * L1 - t2*a2, mid * L2 - t2*b2)
    if mid * L1 - t2*a2 >= mid * L2 - t2*b2:
        if flg:
            ok = mid
        else:
            ng = mid
    else:
        if flg:
            ng = mid
        else:
            ok = mid
    mid = (ok + ng)//2

print(ok * 2 - 1)

Submission status: Wrong Answer

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

s1=a1*t1+a2*t2
s2=b1*t1+b2*t2
ts=t1+t2
if s1==s2:
    print("infinity")
    exit()
faster=1 if s1>s2 else 2

if (a1>b1 and faster==1) or (a1<b1 and faster==2):
    print(0)
    exit()

if faster==2:
    a1,b1=b1,a1
    a2,b2=b2,a2

d=abs(s1-s2)
cnt=(b1*t1-a1*t1)//d
r=(a1*t1-b1*t1)%d
print(cnt*2+1)

Submission status: Wrong Answer

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

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

def getEnd(mid, f):
    c = mid % 2
    if f == 1:
        return (a1 * t1) * ((mid / 2) + c) + (a2 * t2) * (mid / 2)
    else:
        return (b1 * t1) * ((mid / 2) + c) + (b2 * t2) * (mid / 2)

def get(mid):
    c = mid % 2
    X1 = getEnd(mid, 1)
    Y1 = getEnd(mid - 1, 1)

    X2 = getEnd(mid, 2)
    Y2 = getEnd(mid - 1, 2)

    if X1 <= X2:
        if Y1 >= Y2:
            return True
    else:
        if Y1 <= Y2:
            return True
    return False


def check(mid):
    if mid == 1:
        return True
    if get(mid):
        return True
    return False

s = 1
e = 10**18
it = 100
while it:
    mid = (s + e) / 2
    it -= 1
    if check(mid):
        s = mid
    else:
        e = mid
res = -1
for i in range(mid - 5, mid + 5):
    if i >= 1:
        if check(i):
            res = i
print res - 1

Submission status: Wrong Answer

Solution code:
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
c1=a1*t1+a2*t2
c2=b1*t1+b2*t2
if c1==c2:
    print("infinity")
    exit()
if c1>c2:
    flag=1
else:
    flag=2
if flag==1:
    if a1>b1:
        print(0)
    else:
        m=(b1-a1)*t1
        print(((m-1)//(c1-c2))*2+1)
else:
    if a1<b1:
        print(0)
    else:
        m=(a1-b1)*t1
        print(((m-1)//(c2-c1))*2+1)

Submission status: Wrong Answer

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:
  	print(1 + a1//a2*2)
else:
  print(0)

Submission status: Wrong Answer

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 > B1:
    A1, A2, B1, B2 = B1, B2, A1, A2

if A1 + A2 == B1 + B2:
    print('infinity')
elif A1 + A2 < B1 + B2 or A1 == A2:
    print(0)
else:
    def check(c):
        return c * (A1 + A2) + A1 > c * (B1 + B2) + B1
    
    l = 0
    h = 1000* B1 + B2
    while l < h:
        mid = l + (h - l) // 2
        if check(mid):
            h = mid
        else:
            l = mid + 1
            
    print(2*l-1)

Submission status: Wrong Answer

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() ]
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
        lastd = a1*t1 + a2*t2 - b1*t1 - b2*t2
        mind = abs(mind)
        if mind % lastd == 0:
            ans = 2 * (mind // lastd) + 2
        else:
            ans = 2 * (mind // lastd) + 1
        print(mind, lastd)
    
print(ans if ans != -1 else "infinity")

Submission status: Wrong Answer

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

d1=t1*a1+t2*a2
d2=t1*b1+t2*b2

import sys
if d1>d2:
    dif=d1-d2
elif d1<d2:
    dif=d2-d1
else:
    print('infinity')
    sys.exit()
if b1>a1:
    gap=(b1-a1)*t1
else:
    gap=(a1-b1)*t1

if (b1-a1)*(b2-a2)>0:
    print(0)
    sys.exit()
if gap%dif!=0:
    print(gap//dif*2+1)
else:
    print(gap//dif*2)

Submission status: Wrong Answer

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 and c2>0):
	print("0")
	sys.exit()


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

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

Submission status: Wrong Answer

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

q = a1*t1-b1*t1
w = (b1*t1+b2*t2) - (a1*t1+a2*t2)
k = -(-q//w)
ans = 2*k-1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import itertools
import sys

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

taka = t1*a1
aoki = t1*b1
ans = 0
for i in range(2,100000):
    prev_taka = taka
    prev_aoki = aoki
    if(i%2==1):
        taka += t1*a1
        aoki += t1*b1
    else:
        taka += t2*a2
        aoki += t2*b2
    if((prev_taka-prev_aoki)*(taka-aoki)<0):
        ans += 1
    elif((prev_taka-prev_aoki)*(taka-aoki)==0):
        print("infinity")
        sys.exit()
    else:
        pass

print(ans)

Submission status: Wrong Answer

Solution code:
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
s1=t1*a1+t2*a2
s2=t1*b1+t2*b2
if s1>s2:
	s2,s1=s1,s2
if s1==s2:
	print("infinity")
	exit()
import math
cnt=math.ceil(s1/(s2-s1))
aa=t1*a1
bb=t1*b1
aaa=t1*a1+t2*a2
bbb=t1*b1+t2*b2
if (aa>bb and aaa>bbb) or (aa<bb and aaa<bbb):
	print(0)
	exit()
q=abs(aa-bb)
print(q//(s2-s1)*2+1)

Submission status: Wrong Answer

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

    # aの方がt1のときに先行している
    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)
    # print(dif1)
    # print(dif2)
    if dif2 == 0:
        print("infinity")
        exit()
    if dif1 % dif2 == 0:
        print(dif1 // dif2 * 2)
    else:
        print((dif1 + dif2) // dif2 * 2 - 1)


if __name__ == '__main__':
    p_f()

Submission status: Wrong Answer

Solution code:
import numpy as np
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
y1=a1*t1-b1*t1
y2=a2*t2-b2*t2
if y1 == -y2:
    print('infinity')
else:
    if y1<0:
        y1*=-1
        y2*=-1
    n=int(np.trunc(y2/(y1+y2)))
    if(y2/(y1+y2)==n):
        print((n-1)*2)
    else:
        print((n-1)*2+1)

Submission status: Wrong Answer

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

d1=t1*a1+t2*a2
d2=t1*b1+t2*b2

import sys
if d1>d2:
    dif=d1-d2
elif d1<d2:
    dif=d2-d1
else:
    print('infinity')
    sys.exit()
if b1>a1:
    gap=b1-a1
else:
    gap=a1-b1

if (b1-a1)*(b2-a2)>0:
    print(0)
    sys.exit()
if gap%dif!=0:
    print(gap//dif*2+1)
else:
    print(gap//dif*2)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def fi(): return float(input())
def mfi(): return map(float, input().rstrip().split())
def lmfi(): return list(map(float, input().rstrip().split()))
def li(): return list(input().rstrip())
def debug(*args, sep=" ", end="\n"): print("debug:", *args, file=sys.stderr, sep=sep, end=end) if not __debug__ else None
def exit(*arg): print(*arg); sys.exit()
# template


def main():
    t1, t2 = mi()
    a1, a2 = mi()
    b1, b2 = mi()
    aoki = 0
    taka = 0
    meetF = False
    meetS = False
    # a の方が速くなるようにスワップ
    if t1 * a1 + t2 * a2 < t1 * b1 + t2 * b2:
        x1, x2 = a1, a2
        a1, a2 = b1, b2
        b1, b2 = x1, x2
    # print(a1, a2, b1, b2)
    if t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
        exit("infinity")
    if a1 > b1:
        meetF = False
    else:
        meetF = True
    # 1区間目も2区間目もaの法が速いなら合うことはない
    if not meetF:
        exit("0")
    sa = t1 * a1 + t2 * a2 - t1 * b1 - t2 * b2  # 1区切りごとにＡが速く，付いてしまう差
    # print(sa)
    # 何区切りすると追いつけない差になってしまうか
    ng = 10**18
    ok = 0
    while(ng - ok > 1):
        mid = (ng + ok) // 2
        # シミュレーション（mid区切りのあと追いつけるか）
        a = 0
        b = -mid * sa
        if(a + t1 * a1 <= b + t1 * b1):
            ok = mid
        else:
            ng = mid
    # シミュレーション(ok*2回は確実に会う)
    exit(2 * ok + 1)
    # cnt = 2 * ok
    # a = 0
    # b = -ok * sa
    # for i in range(ok, ok + 10):
    #     a += t1 * a1
    #     b += t1 * b1
    #     if a <= b:
    #         cnt += 1
    #     a += t2 * a2
    #     b += t2 * b2
    # print(cnt)
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

    div = (a1 - b1) * t1 + (a2 - b2) * t2
    ans = (b1 - a1) * t1

    if div == 0:
        print('infinity')
        return
    elif div > 0:
        # now a1 >= b1
        # if a2 > b2:
        print('0')
        return

    else:
        print(2 * (ans // div) + 1)
        return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

mugen = "infinity"
a1 = A1*T1
a2 = A2*T2
b1 = B1*T1
b2 = B2*T2

if a1+a2 > b1+b2:
    if a1 > b1:
        print(0)
    elif a1 < b1:
        n = (a2-b2)//((a1+a2)-(b1+b2))
        if (a2-b2)%((a1+a2)-(b1+b2)) == 0:
            print(2*n)
        else:
            print(2*n-1)
    else:
        print(1)
            
elif a1 + a2 < b1 + b2:
    if a1 < b1:
        print(0)
    elif a1 > b1:
        n = -(a2-b2)//((a1+a2)-(b1+b2))
        if -(a2-b2)%((a1+a2)-(b1+b2)) == 0:
            print(2*n)
        else:
            print(2*n-1)
    else:
        print(1)
            
else:
    print(mugen)