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

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

elif (A1 > B1 and A2 > B2) or (A1 < B1 and A2 < B2):
    print (0)
    sys.exit()

else:

    pl = abs ( A1 * T1 - B1 * T1 )
    mi = abs ( A2 * T2 - B2 * T2 )

    #print (pl,mi)

    if pl < mi:

        if pl % (mi - pl) != 0:
            print ( pl // (mi - pl) * 2 + 1)
        else:
            print ( pl // (mi - pl) * 2)

    elif pl > mi:

        if mi % (pl - mi) != 0:
            print ( max( 0 , mi // (pl - mi) * 2 ))
        else:
            print ( max( 0 , mi // (pl - mi) * 2 - 1) )

    else:
        print ("infinity")

Submission status: Wrong Answer

Solution code:
T1,T2=map(int,input().split())
a,b=map(int,input().split())
c,d=map(int,input().split())
e=T1*a
f=T1*c
if e>f:
  g=e-f
  e+=T2*b
  f+=T2*d
  if e==f:
    print("infinity")
  elif e>f:
    print(0)
  else:
    print(1+(g//(f-e))*2)
elif e<f:
  g=f-e
  e+=T2*b
  f+=T2*d
  if e==f:
    print("infinity")
  elif e<f:
    print(0)
  else:
    print(1+(g//(e-f))*2)

Submission status: Wrong Answer

Solution code:
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
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)
else:
  print(f//(-(f+s))*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())

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
        print(2*n+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())

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


if __name__ == '__main__':
    p_f()

Submission status: Wrong Answer

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]
    print(A,B,bcover,tcover, bcover/tcover)
    if bcover%tcover == 0:
      print(int(bcover/tcover)*2)
    else:
      print(int(bcover/tcover)*2+1)

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*T1 > B1*T1 and A2*T2 > B2*T2) or (A1*T1 < B1*T1 and A2*T2 < B2*T2):
    print(0)
else:
    if a1+a2 > b1+b2:
        if a1 > b1:
            print(0)
        else:
            n = (a2-b2)//((a1+a2)-(b1+b2))
            print(2*(n-1)+1)
            
    elif a1 + a2 < b1 + b2:
        if a1 < b1:
            print(0)
        else:
            n = -(a2-b2)//((a1+a2)-(b1+b2))
            print(2*(n-1)+1)
            
    else:
        print(mugen)

Submission status: Wrong Answer

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:
        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:
        print((d1 // d2)*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())
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()
n = 2*((hA-hB)//(sB-sA))+1
print(n)

Submission status: Wrong Answer

Solution code:
def meet(faster, slower):
  if slower[0] - faster[0] < 0:
    print(0)
  else:
    print((slower[0] - faster[0]) // (faster[1] - slower[1]) + 1)

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

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

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

Submission status: Wrong Answer

Solution code:
import sys
T1, T2, A1, A2, B1, B2 = map(int, sys.stdin.read().split())
a1, b1 = T1*A1, T1*B1
a2, b2 = T2*A2, T2*B2
sa, sb = a1+a2, b1+b2
if sa==sb:
    print("infinity")
    exit()
if sa < sb:
    a1, a2, b1, b2, sa, ab = b1, b2, a1, a2, sb, sa

# 2*n-1 回以上出会うか
ok = 0
ng = 10**1800
a = 0
while ok+1 < ng:
    c = (ok+ng)//2
    if (c-1)*sa+a1 <= (c-1)*sb+b1:
        if (c - 1) * sa + a1 == (c - 1) * sb + b1:
            a = 1
        ok = c
    else:
        ng = c
if ok > 10**1799:
    print("infinity")
else:
    print(max(ok*2-1+a, 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())

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

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

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

Submission status: Wrong Answer

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(d,k)
    print(ans)
resolve()

Submission status: Wrong Answer

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

tmp = 0
ans = 0
f1 = True
f2 = True

for i in range(10**6):
	tmp += (a1-b1) * t1
	if tmp > 0 and f2:
		ans += 1
		f1, f2 = True, False
	elif tmp < 0 and f1:
		ans += 1
		f1, f2 = False, True
	elif tmp == 0:
		ans += 1
		f1, f2 = True, True
	else:
		break

	tmp += (a2-b2) * t2
	if tmp > 0 and f2:
		ans += 1
		f1, f2 = True, False
	elif tmp < 0 and f1:
		ans += 1
		f1, f2 = False, True
	elif tmp == 0:
		ans += 1
		f1, f2 = True, True
	else:
		break

if ans == 1999999 + 1:
	print('infinity')
else:
	print(ans-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*t1 + a2*t2 == b1*t1+b2*t2:
    print("infinity")
    exit()

ans = 0
if a1 > b1:
    now = 1
else:
    now = -1
a = t1*a1
b = t1*b1
pre = 0
oe = 1
ans = -1
while now != pre:
    if oe == 1:
        a += t2*a2
        b += t2*b2
        oe = 0
    else:
        a += t1*a1
        b += t1*b1
        oe = 1
    pre = now
    if a > b:
        now = 1
    else:
        now = -1
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
def examF():
    T = LI()
    A = LI()
    B = LI()
    loop1 = T[0]*(A[0]-B[0])
    loop2 = T[1]*(A[1]-B[1])
    if loop1>0 and loop2>0:
        ans = 0
    elif loop1<0 and loop2<0:
        ans = 0
    else:
        now = loop1
        cur = loop1+loop2
#        print(now,cur)
        if cur!=0:
            ans = abs(now)//abs(cur)*2
            if abs(now)%abs(cur)!=0:
                ans +=1
        else:
            ans = "infinity"
    print(ans)
    return

import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examF()

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*t1+a2*t2==b1*t1+b2*t2:print("infinity")
else:
  if (a1-b1)*(a2-b2)>0:print(0)
  else:
    if a1>b1:
      firstD=t1*(a1-b1)
      secondD=t2*(b2-a2)
      lead=secondD-firstD
    else:
      firstD=t1*(b1-a1)
      secondD=t2*(a2-b2)
      lead=secondD-firstD
    if firstD<lead:print(1);exit()
    if firstD%lead==0:
      print((firstD//lead)*2+2)
    else:print((firstD//lead)*2+1)

Submission status: Wrong Answer

Solution code:
import sys
T1, T2, A1, A2, B1, B2 = map(int, sys.stdin.read().split())
a1, b1 = T1*A1, T1*B1
a2, b2 = T2*A2, T2*B2
sa, sb = a1+a2, b1+b2
if sa==sb:
    print("infinity")
    exit()
if sa < sb:
    a1, a2, b1, b2, sa, ab = b1, b2, a1, a2, sb, sa

# 2*n-1 回以上出会うか
ok = 0
ng = 10**1800
a = 0
while ok+1 < ng:
    c = (ok+ng)//2
    if (c-1)*sa+a1 <= (c-1)*sb+b1:
        if (c - 1) * sa + a1 == (c - 1) * sb + b1:
            a = 1
        ok = c
    else:
        ng = c
print(max(ok*2-1+a, 0))

Submission status: Wrong Answer

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) 
                else:
                    print((B1 - A1) * T1 // (Atotal - Btotal) * 2 + 1)
            else:
                if B1 > A1: print(0)
                else:
                    print((A1 - B1) * T1 // (Btotal - Atotal) * 2 + 1)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
T1,T2=[int(s) for s in input().split()]
ls1=[int(s) for s in input().split()]
ls2=[int(s) for s in input().split()]
A1,A2=ls1
B1,B2=ls2

#1ループした際の移動距離が同じなら、infinity
if  A1*T1+A2*T2==B1*T1+B2*T2:
  print('infinity')
else:
  if A1*T1+A2*T2-B1*T1-B2*T2<0: #1ループでAが長距離進むとする
    A1,A2=ls2
    B1,B2=ls1
  d=A1*T1+A2*T2-B1*T1-B2*T2 #1ループで着く差
  x=(B1-A1)*T1
  ####ループ開始時に差がxメートル以内なら、２回交わる
  ####ループ開始時に差がちょうどxメートルなら、１回交わる
  if x%d==0:
    print(2*(x//d))
  else:
    print(2*(x//d)+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 > a2 and b1 > b2) or (a1 < a2 and b1 < b2):
  print(0)
else:
  if a1 * t1 + a2 * t2 == b1 * t1 + b2 * t2:
    print("Infinity")
  else:
    d_all = (a1 * t1 + a2 * t2) - (b1 * t1 + b2 * t2)
    d_1 = a1 * t1 - b1 * t1
    if (d_all > 0 and d_1 > 0) or (d_all < 0 and d_1 < 0):
      print(0)
    else:
      if abs(d_all) > abs(d_1):
        print(1)
      else:
        ans = 1
        ans += - (d_1 - 1) // d_all * 2 + (1 if d_1 % d_all == 0 else 0)
        print(ans)

Submission status: Wrong Answer

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

def f(t):
    if t<=T1:
        return (A1-B1)*t/((B1-A1)*T1+(B2-A2)*T2)
    else:
        return ((A1-B1)*T1+(A2-B2)*(t-T1))/((B1-A1)*T1+(B2-A2)*T2)

ans = 0
if ((B1-A1)*T1+(B2-A2)*T2) == 0:
    ans = 'infinity'
else:
    ans += int(max(f(T1),0))
    ans += floor(max(max(f(T1),0),max(f(T1+T2),0)))-ceil(min(max(f(T1),0),max(f(T1+T2),0)))+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 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()
res += (c//d)*2

print(res)

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
	print(max(0, delo // delw * 2 + 1))

Submission status: Wrong Answer

Solution code:
def meet(faster, slower):
  if slower[0] - faster[0] < 0:
    print(0)
  else:
    print((slower[0] - faster[0]) // (faster[1] - slower[1]) + 1)

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

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

if at1 + at2 < bt1 + bt2:
  faster = [bt1, bt1 + bt2]
  slower = [at1, at1 + at2]
  meet(faster, slower)
elif at1 + at2 > bt1 + bt2:
  faster = [at1, at1 + at2]
  slower = [bt1, bt1 + bt2]
  meet(faster, slower)
else:
  print('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())
x1 = (a1 - b1) * t1
x2 = (a2 - b2) * t2
if (x1 > 0 and x2 > 0) or (x1 < 0 and x2 < 0):
  print(0)
elif x1 + x2 == 0:
  print('infinity')
else:
  xn = -min(x1, x2)
  xp = max(x1, x2)
  ans = 0
  if xp > xn:  
    ans += xn // (x1 + x2) * 2
    if x1 == -xn:
      ans += 1
  else:
    ans -= xp // (x1 + x2) * 2
    if x1 == xp:
      ans += 1
  print(ans)

Submission status: Wrong Answer

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


def changed(i):
    x1, y1 = place(i)
    x2, y2 = place(i + 1)
    return (x1 == y1) or (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

if ng == 10 ** 20:
    print("infinity")
else:
    print(ok)

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

diff = sorted([(A[0] - B[0]) * T[0], (A[1] - B[1]) * T[1]])
if sum(diff) == 0:
    print("infinity")
elif diff[0] * diff[1] > 0:
    print(0)

else:
    if abs(diff[0]) > abs(diff[1]):
        print(2*math.ceil(diff[0] / (diff[0] + diff[1]))-3)
    else:
        print(2*math.ceil(diff[1] / (diff[0] + diff[1]))-3)

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:
    ans = bis(p, 10**16, -1) + bis(p2, 10**16, -1) - 1

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:
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 *= -1
  #print(top,d)
  if top % d != 0:
    print(top//d*2+1)
  else:
    print(top//d*2)

Submission status: Wrong Answer

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

elif (A1 > B1 and A2 > B2) or (A1 < B1 and A2 < B2):
    print (0)
    sys.exit()

else:

    pl = abs ( A1 * T1 - B1 * T1 )
    mi = abs ( A2 * T2 - B2 * T2 )

    if pl < mi:

        if pl % (mi - pl) != 0:
            print ( pl // (mi - pl) * 2 + 1)
        else:
            print ( pl // (mi - pl) * 2)

    elif pl > mi:

        if mi % (pl - mi) != 0:
            print ( mi // (pl - mi) * 2 )
        else:
            print ( mi // (pl - mi) * 2 - 1)

    else:
        print ("infinity")

Submission status: Wrong Answer

Solution code:
T1, T2, A1, A2, B1, B2 = map(int, open(0).read().split())
a1, b1 = T1*A1, T1*B1
a2, b2 = T2*A2, T2*B2
sa, sb = a1+a2, b1+b2
if sa==sb:
    print("infinity")
    exit()
if sa < sb:
    a1, a2, b1, b2, sa, ab = b1, b2, a1, a2, sb, sa

# 2*n-1 回以上出会うか
ok = 0
ng = 10**1800
a = 0
while ok+1 < ng:
    c = (ok+ng)//2
    if (c-1)*sa+a1 <= (c-1)*sb+b1:
        if (c - 1) * sa + a1 == (c - 1) * sb + b1:
            a = 1
        ok = c
    else:
        ng = c
print(max(ok*2-1+a, 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())

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

cnt = abs(T1*a)//abs(d)
if cnt*abs(d) == abs(T1*a):
    cnt = 2*cnt+2
else:
    cnt = 2*cnt+1

print(cnt)

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)
	else:
		ans=0
		f=0
		i=0
		K=0
		KK=0
		while f<10:
			if i==0:
				K+=a-b
				i=1
			else:
				K+=aa-bb
				i=0
				
			if K*KK<=0:
				ans+=1
			else:
				f+=1
			KK=K
		
		print(ans-1)
	
	
	
	

	
if __name__ == "__main__":
		main()

Submission status: Wrong Answer

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
    ok = 0
    ng = 10**20
    while abs(ng - ok) > 1:
        med = (ng + ok)//2
        if med*x1 + y1 >= med*x2 + z1:
            ok = med
        else:
            ng = med
    
    return 1 + 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: Wrong Answer

Solution code:
t = input().split(" ")
a = input().split(" ")
b = input().split(" ")

t = [int(i) for i in t]

rs = [int(a[0]) - int(b[0]), int(a[1]) - int(b[1])]

def shaku(x):
  tmpx = x + rs[0] * t[0]
  tmpy = tmpx + rs[1] * t[1]
  
  if x * tmpx < 0:
    if tmpx * tmpy < 0:
      return(2)
    else:
      return(1)
  elif x * tmpx == 0:
    return(1)
  else:
    return(0)

if rs[0] * t[0] + rs[1] * t[1] == 0:
  print("infinity")
elif rs[0] * t[0] * rs[1] * t[1] > 0:
  print(0)
else:
  s = rs[0] * t[0] + rs[1] * t[1]
  i = 1
  while shaku(i * s) != 0:
    i = i + 1  
  print(2 * i - 1)

Submission status: Wrong Answer

Solution code:
T1, T2, A1, A2, B1, B2 = map(int, open(0).read().split())
a1, b1 = T1*A1, T1*B1
a2, b2 = T2*A2, T2*B2
sa, sb = a1+a2, b1+b2
if sa==sb:
    print("infinity")
    exit()
if sa < sb:
    a1, a2, b1, b2, sa, ab = b1, b2, a1, a2, sb, sa

# 2*n-1 回以上出会うか
ok = 0
ng = 10**18000
a = 0
while ok+1 < ng:
    c = (ok+ng)//2
    if (c-1)*sa+a1 <= (c-1)*sb+b1:
        if (c - 1) * sa + a1 == (c - 1) * sb + b1:
            a += 1
        ok = c
    else:
        ng = c
print(max(ok*2-1+a, 0))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


def place(i):
    ret = []

    x = (ts[0] * vs[0] + ts[1] * vs[1]) * (i // 2)
    if i % 2 == 1:
        x += ts[0] * vs[0]
    ret.append(x)

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

    return ret


def changed(i):
    x1, y1 = place(i)
    x2, y2 = place(i + 1)
    return x1 == y1 or 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

if ok == 10 ** 20 - 1:
    print("infinity")
else:
    print(ok)

Submission status: Wrong Answer

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

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
    sum_=run1+run2
    #print(run1,run2,sum_)
    if run1>0 and run2>0:
        print(0)
    elif run1<0 and run2<0:
        print(0)
    elif run1>0 and run2<0:
        if abs(run1)>abs(run2):
            print(0)
        elif abs(run1)==abs(run2):
            print("infinity")
        else:
            print(2*ceil(abs(run1)/abs(sum_))-1)
    else:
        if abs(run1)>abs(run2):
            print(0)
        elif abs(run1)==abs(run2):
            print("infinity")
        else:
            print(2*ceil(abs(run1)/abs(sum_))-1)

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

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

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

elif (A1 > B1 and A2 > B2) or (A1 < B1 and A2 < B2):
    print (0)
    sys.exit()

else:

    pl = abs ( A1 * T1 - B1 * T1 )
    mi = abs ( A2 * T2 - B2 * T2 )

    if pl < mi:

        if pl % (mi - pl) != 0:
            print ( pl // (mi - pl) * 2 + 1)
        else:
            print ( pl // (mi - pl) * 2)

    else:

        if mi % (pl - mi) != 0:
            print ( mi // (pl - mi) * 2 )
        else:
            print ( mi // (pl - mi) * 2 - 1)

Submission status: Wrong Answer

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
    print(gap // lag * 2 + 1)
    exit()

print(0)

Submission status: Wrong Answer

Solution code:

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

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


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

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

    if abs(d1) < abs(d2):
        S = d1 # 初期位置
        d1,d2 = d2,d1
    else:
        S = 0

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

    print(S,d1,d2)

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

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