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: Time Limit Exceeded

Solution code:
t = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = [a[0] - b[0], a[1] - b[1]]
w = t[0] * d[0] + t[1] * d[1]
if w < 0:
    d[0] *= -1
    d[1] *= - 1
    w *= -1
if t[0] * d[0] + t[1] * d[1] == 0:
    print("infinity")
else:
    now = 0
    ans = 0
    i, last = 0, 0
    while True:
        new = now + d[i] * t[i]
        last += 1
        if (now < 0 and new > 0) or (now > 0 and new < 0) or new == 0:
            ans += 1
            last = 0
        now = new
        i = 1 - i
        if last > 2:
            break
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def solve():
    from math import ceil

    def gcd(a, b):
        if b == 0:
            return a
        else:
            return gcd(b, a % b)

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

    g = gcd(t1, t2)
    t1 //= g
    t2 //= g

    ax = a1 * t1
    ay = a2 * t2
    bx = b1 * t1
    by = b2 * t2

    a = a1 * t1 + a2 * t2
    b = b1 * t1 + b2 * t2

    g = gcd(a, b)
    a //= g
    b //= g

    if a == b:
        return 'infinity'
    min_ = min(a, b)
    abs_ = abs(a - b)
    k = ceil(min_ / abs_)
    # kセット走った後は、会うことはない

    ret = 0
    curr_a, curr_b = 0, 0
    for j in range(k):
        if ((curr_a < curr_b) and (curr_a + ax >= curr_b + bx)) or ((curr_b < curr_a) and (curr_b + bx >= curr_a + ax)):
            ret += 1
        curr_a += ax
        curr_b += bx
        if ((curr_a < curr_b) and (curr_a + ay >= curr_b + by)) or ((curr_b < curr_a) and (curr_b + by >= curr_a + ay)):
            ret += 1
        curr_a += ay
        curr_b += by
    return ret


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

Submission status: Time Limit Exceeded

Solution code:
import sys

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")
    sys.exit()

a, b = 0, 0
key = 0
ans = 0
while True:
    key = ans
    while True:
        a_past, b_past = a, b
        a += a1*t1
        b += b1*t1
        if a_past == b_past:
            break
        if (a_past-b_past)*(a-b) <= 0:
            ans += 1
        break
    while True:
        a_past, b_past = a, b
        a += a2*t2
        b += b2*t2
        if a_past == b_past:
            break
        if (a_past-b_past)*(a-b) <= 0:
            ans += 1
        break
    if key == ans:
        break
print(ans)

Submission status: Time Limit Exceeded

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

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

ans=1
a,b=A1*T1+A2*T2,B1*T1+B2*T2
while b-a<=T1*A1-T1*B1:
  if b-a<T1*A1-T1*B1:
    ans+=2
  else:
    ans+=1
  a+=A1*T1+A2*T2
  b+=B1*T1+B2*T2
  
print(ans)

Submission status: Time Limit Exceeded

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

p1 = t1 * a1 + t2 * a2
p2 = t1 * b1 + t2 * b2

if (p1 == p2):
    print("infinity")
else:
    if ((t1 * a1 > t1 * b1 and p1 > p2) or (t1 * a1 < t1 * b1 and p1 < p2)):
        print(0)
    else:
        ans = 0
        akun = 0
        bkun = 0
        flag = 0
        while(True):
            akun += t1 * a1
            bkun += t1 * b1
            if (akun > bkun):
                tflag = 1
            elif (akun == bkun):
                tflag = 0
            else:
                tflag = -1
            if (flag != 0 and flag != tflag):
                ans += 1
            flag = tflag

            akun += t2 * a2
            bkun += t2 * b2
            if (akun > bkun):
                tflag = 1
            elif (akun == bkun):
                tflag = 0
            else:
                tflag = -1
            if (flag != 0 and flag != tflag):
                ans += 1
            flag = tflag
            if (abs(akun - bkun) > max(p1, p2)):
                break
        print(ans)

Submission status: Time Limit Exceeded

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()
a = 0
b = 0
check = "same"
deai = 0
x = 0
while True:
    a += a1*t1
    b += b1*t1
    if check == "same":
        if a > b:
            check = "a"
            x = 0
        else:
            check = "b"
            x = 0
    elif check == "a":
        if b > a:
            deai += 1
            check = "b"
            x = 0
        elif a == b:
            deai += 1
            check = "same"
            x = 0
        else:
            x += 1
    elif check == "b":
        if a > b:
            deai += 1
            check = "a"
            x = 0
        elif a == b:
            deai += 1
            check = "same"
            x = 0

        else:
            x += 1
    a += a2*t2
    b += b2*t2
    if check == "same":
        if a > b:
            check = "a"
            x = 0
        else:
            check = "b"
            x = 0
    elif check == "a":
        if b > a:
            deai += 1
            check = "b"
            x = 0
        elif a == b:
            deai += 1
            check = "same"
            x = 0
        else:
            x += 1
    elif check == "b":
        if a > b:
            deai += 1
            check = "a"
            x = 0
        elif a == b:
            deai += 1
            check = "same"
            x = 0
        else:
            x += 1
    if x >= 2:
        print(deai)
        break

Submission status: Time Limit Exceeded

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

s1 = a1*T1
s2 = a2*T2
w1 = s1 + s2
t1 = b1*T1
t2 = b2*T2
w2 = t1 + t2
#print(w1, w2)

if w1==w2:
    print('infinity')
    exit()

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

posa = 0
posb = 0
ans = 0
while True:
    meet = 0
    preva = posa
    prevb = posb
    posa += s1
    posb += t1
    if (preva<prevb and posa>=posb) or (prevb<preva and posb>=posa):
        meet += 1
    preva = posa
    prevb = posb
    posa += s2
    posb += t2
    if (preva<prevb and posa>=posb) or (prevb<preva and posb>=posa):
        meet += 1
    if meet==0:
        break
    ans += meet
print(ans)

Submission status: Time Limit Exceeded

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

A = 0
B = 0
ans = 0

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

while True:
    flag1,flag2 = False,False
    if A>B and B+B1*T1>=A+A1*T1:
        ans += 1
    elif A<B and B+B1*T1<=A+A1*T1:
        ans += 1
    else:
        flag1 = True
    A += A1*T1
    B += B1*T1
    if A>B and B+B2*T2>=A+A2*T2:
        ans += 1
    elif A<B and B+B2*T2<=A+A2*T2:
        ans += 1
    else:
        flag2 = True
    A += A2*T2
    B += B2*T2

    if flag1 and flag2:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def running(T1,T2,A1,A2,B1,B2):
    v1 = A1 - B1
    v2 = A2 - B2
    if v1*T1+v2*T2 == 0:
        print("infinity")
        return
    drift = v1*T1 + v2*T2
    i = 0
    if v1 > 0 and drift < 0:
        while drift*i + v1*T1 > 0:
            i += 1
        if drift*i + v1*T1 == 0:
            print(2*i)
        else:
            print(2*i-1)
    elif v1 < 0 and drift > 0:
        while drift*i + v1*T1 < 0:
            i += 1
        if drift*i + v1*T1 == 0:
            print(2*i)
        else:
            print(2*i-1)
    else:
        print(0)

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

Submission status: Time Limit Exceeded

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 and t1*a1+t2*a2>t1*b1+t2*b2 or a1<b1 and t1*a1+t2*a2<t1*b1+t2*b2:
    print(0)
    exit()
ad1,ad2,bd1,bd2=a1*t1,a2*t2,b1*t1,b2*t2
da1,db1,da2,db2,ans=0,0,0,0,0
flag=0
while abs(da1-db1)<=abs(ad1+ad2-bd1-bd2) or abs(da1-db1)<=abs(ad1-bd1):
    if flag==0:
        da1+=ad1
        db1+=bd1
        flag=1
    else:
        da1+=ad2
        db1+=bd2
        flag=0
    if da2>db2 and da1<=db1 or da2<db2 and da1>=db1:
        ans+=1
    da2,db2=da1,db1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
def main():
    T1, T2 = map(int, input().split())
    f = fractions.gcd(T1, T2)
    T1 //= f
    T2 //= f
    A1, A2 = map(int, input().split())
    B1, B2 = map(int, input().split())
    f = fractions.gcd(fractions.gcd(A1, A2), fractions.gcd(B1, B2))
    A1 //= f
    A2 //= f
    B1 //= f
    B2 //= f

    a_half = T1*A1
    a_full = a_half + T2*A2
    b_half = T1*B1
    b_full = b_half + T2*B2

    if a_full == b_full:
        print("infinity")
        return
    elif a_full > b_full:
        flg = 1
    else:
        flg = -1

    if flg == 1 and a_half <= b_half:
        ans = 1
    elif flg == -1 and a_half >= b_half:
        ans = 1
    else:
        ans = 0

    a = a_full
    b = b_full
    while True:
        if flg == 1 and a + a_half < b + b_half:
            ans += 2
        elif flg == -1 and a + a_half > b + b_half:
            ans += 2
        elif a + a_half == b + b_half:
            ans += 1
        else:
            break
        a += a_full
        b += b_full

    print(ans)

main()

Submission status: Time Limit Exceeded

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


def main():
    T1,T2 = mis()
    A1,A2 = mis()
    B1,B2 = mis()
    d1 = T1*A1 - T1*B1
    d2 = T2*A2 - T2*B2
    if d1 + d2 == 0:
        print('infinity')
    elif d1 < 0 > d2 or d1 > 0 < d2:
        print(0)
    else:
        ans = -1
        pos = 0
        if d1 < 0:
            d1 *= -1
            d2 *= -1
        while True:
            pos += d1
            if pos == 0:
                ans += 1
                break
            elif pos < 0:
                break
            else:
                ans += 1
            pos += d2
            ans += 1
        print(ans)




main()

Submission status: Time Limit Exceeded

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(0)
a=0
b=0
c=0
z=0
while True:
	f=False
	a+=T1*A1
	b+=T1*B1
	if a==b:
		z+=1
		break
	t=1 if a<b else -1
	if c*t==-1:
		z+=1
		f=True
	c=t;
	a+=T2*A2
	b+=T2*B2
	t=1 if a<b else -1
	if c*t==-1:
		z+=1
		f=True
	c=t
	if not f:
		break
print(z)

Submission status: Time Limit Exceeded

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

ans = 0
d0 = 0
while True:
    d1 = d0 + (T1 * A1 - T1 * B1)
    d2 = d1 + (T2 * A2 - T2 * B2)
    # print(d0, d1, d2)

    if d2 == 0:
        ans = 'infinity'
        break

    c = 0
    if d0 == 0:
        if d1 == 0:
            c = 1
        elif d1 * d2 < 0:
            c = 1
    else:
        if d1 == 0:
            c = 1
        else:
            if d0 * d1 < 0:
                c += 1
            if d1 * d2 < 0:
                c += 1

    if c == 0:
        break
    else:
        ans += c
        d0 = d2

print(ans)

Submission status: Time Limit Exceeded

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])]
m = rs[0] * t[0]
s = rs[0] * t[0] + rs[1] * t[1]

def shaku(x):
  tmpx = x + m
  tmpy = x + s
  
  if x * tmpx < 0:
    if tmpx * tmpy < 0:
      return(2)
    else:
      return(1)
  elif x * tmpx == 0:
    return(1)
  else:
    return(0)

if s == 0:
  print("infinity")
elif m * s > 0:
  print(0)
else:
  i = 1
  res = 1
  while shaku(i * s) != 0:
    res = res + shaku(i * s)
    i = i + 1
  print(res)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

t = inpl()
a = inpl()
b = inpl()
if (a[0] > b[0] and a[1] > b[1]) or (a[0] < b[0] and a[1] < b[1]):
    print(0)
    quit()
c = [0,0]
for i in range(2):
    c[i] = abs(a[i]-b[i]) * t[i]
if c[0] == c[1]:
    print('infinity')
    quit()
if c[0] > c[1]:
    print(0)
else:
    sa = c[1]-c[0]
    tmp = 0
    if c[1]%sa:
        tmp = 1
    print((c[1]-1)//(c[1]-c[0])*2 - tmp)

Submission status: Accepted

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
A1 *= T1
A2 *= T2
B1 *= T1
B2 *= T2
if A1 > B1:
    A1, B1 = B1, A1
    A2, B2 = B2, A2
C = B1 - A1
D = A2 - B2

if C == D:
    print("infinity")
elif C > D:
    print(0)
else:
    q, r = divmod(C, (D - C))
    if r == 0:
        print(2 * q)
    else:
        print(2 * q + 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 *= T1
A2 *= T2
B1 *= T1
B2 *= T2
if A1 > B1:
    A1, B1 = B1, A1
    A2, B2 = B2, A2
C = B1 - A1
D = A2 - B2

if A1 + A2 == B1 + B2:
    print("infinity")
    exit()
elif C > D:
    print(0)
    exit()

q, r = divmod(C, (D - C))
if r == 0:
    print(2 * q)
else:
    print(2 * q + 1)

Submission status: Accepted

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

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

#print(c)
if c == 0:
    print('infinity')
    exit()

if c > 0:
    if b1*t1-a1*t1 < 0:
        ans = 0
    else:
        q, r = divmod(b1*t1-a1*t1, c)
        if r == 0:
            ans = 2*q
        else:
            ans = 2*q+1
else:
    c *= (-1)
    if -b1*t1+a1*t1 < 0:
        ans = 0
    else:
        q, r = divmod(-b1*t1+a1*t1, c)
        if r == 0:
            ans = 2*q
        else:
            ans = 2*q+1
print(ans)

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 998244353

#############
# Main Code #
#############

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

# グラフにして考える
# 周期は同じT1, T2
# T1の時とT2の時とで順位が入れ替わっているなら出会っている
t1_diff = (A1 - B1) * T1
t2_diff = (A1 - B1) * T1 + (A2 - B2) * T2
if t1_diff == 0 or t2_diff == 0: # 無限に出会う
    print('infinity')
    exit()
if t1_diff * t2_diff > 0: # ずっとどちらかが前にいる
    print(0)
    exit()

# t2_diff分ずつずれていく

# 順位が逆転する場合
# クロスする時とちょうど接する時を考える
if abs(t1_diff) % abs(t2_diff) == 0:
    # 最後の１回は１回しかクロスしない
    print((abs(t1_diff) // abs(t2_diff)) * 2)
else:
    print((abs(t1_diff) // abs(t2_diff)) * 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())

a, b, ans = 0, 0, 0
n1 = (A1-B1)*T1
n2 = n1 + (A2-B2)*T2
if n2 == 0:
  print('infinity')
elif n1*n2 < 0:
  ans += 2 * (abs(n1)//abs(n2))
  if abs(n1)%abs(n2) > 0:
    ans += 1
  print(ans)
else:
  print(0)

Submission status: Accepted

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
c1 = T1*A1-T1*B1
c2 = T2*A2-T2*B2
if c1 > 0:
    c1 *= -1
    c2 *= -1
power = c1+c2
if power == 0:
    print('infinity')
elif power < 0:
    print(0)
else:
    ans = 2*(-c1//power)+(c1%power > 0)
    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())
a1*=t1
a2*=t2
b1*=t1
b2*=t2
if a1<b1:
    a1,a2,b1,b2=b1,b2,a1,a2
#a1>b2
if a1+a2==b1+b2:
    print('infinity')
    exit()

a1-=b1;b2-=a2

b2-=a1
if b2<0:
    print(0)
    exit()

ans=1+(a1//b2)*2
if a1%b2==0:
    ans-=1####

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys
# sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def ep(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        ep(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(T, A, B):
    P = (A[0] - B[0]) * T[0]
    Q = (A[1] - B[1]) * T[1]

    if P > 0:
        P *= -1
        Q *= -1

    if P+Q == 0:
        return 'infinity'
    elif P+Q < 0:
        return 0

    S = -P // (P + Q)
    T = -P % (P + Q)
    return S * 2 + (1 if T != 0 else 0)


def main():
    T = read_int_n()
    A = read_int_n()
    B = read_int_n()
    print(slv(T, A, B))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
if (a1*t1-b1*t1)*(a1*t1+a2*t2-b1*t1-b2*t2)>0:print(0)
elif (a1*t1-b1*t1)*(a1*t1+a2*t2-b1*t1-b2*t2)==0:print('infinity')
else:print(-(a1*t1-b1*t1)//(a1*t1+a2*t2-b1*t1-b2*t2)*2+((a1*t1-b1*t1)%(a1*t1+a2*t2-b1*t1-b2*t2)!=0))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    if D1 == D2:
        print('infinity')
    else:
        if D2 > D1:
            A1, B1 = B1, A1
            A2, B2 = B2, A2
            D1, D2 = D2, D1
        M1 = T1 * A1
        M2 = T1 * B1
        if M1 > M2:
            print(0)
        else:
            cnt = (M2 - M1) // (D1 - D2)
            ans = cnt * 2
            diff = (M2 - M1) % (D1 - D2)
            if diff > 0:
                ans += 1
            print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


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


def meet(diff, T1, T2, A1, A2, B1, B2):
    ans = 0
    pre_diff = diff
    a = T1 * A1
    b = T1 * B1

    diff += a
    diff -= b

    if (pre_diff * diff) < 0 or (pre_diff != 0 and diff == 0):
        ans += 1

    pre_diff = diff
    a = T2 * A2
    b = T2 * B2

    diff += a
    diff -= b

    if (pre_diff * diff) < 0 or (pre_diff != 0 and diff == 0):
        ans += 1

    return ans


def solve():
    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")
        return

    # 0回
    if (A1 > B1 and A2 > B2) or (A1 < B1 and A2 < B2):
        print(0)
        return

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

    ans = 0
    diff = 0
    for i in range(100):
        num = meet(diff, T1, T2, A1, A2, B1, B2)
        if num == 0:
            break
        ans += num
        diff += one_diff

    left = -1
    right = 10**20
    while right - left > 1:
        m = (right + left) // 2
        tmp = diff + m * one_diff
        if meet(tmp, T1, T2, A1, A2, B1, B2):
            left = m
        else:
            right = m

    x = max(0, left - 10)
    ans += meet(diff, T1, T2, A1, A2, B1, B2) * x
    diff += one_diff * x

    for i in range(100):
        num = meet(diff, T1, T2, A1, A2, B1, B2)
        if num == 0:
            break
        ans += num
        diff += one_diff

    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def solve(T1, T2, A1, A2, B1, B2):
    if (Da := T1 * A1 + T2 * A2) == (Db := T1 * B1 + T2 * B2) or A1 == B1:
        return 'infinity'

    # Aの方が速くなるようそろえる
    if Da < Db:
        A1, A2, Da, B1, B2, Db = B1, B2, Db, A1, A2, Da

    if A1 > B1:
        return 0
    else:
        D1 = (B1 - A1) * T1
        if D1 % (Da - Db):
            return D1 // (Da - Db) * 2 + 1
        else:
            return D1 // (Da - Db) * 2


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

Submission status: Accepted

Solution code:
import sys

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

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

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

    if A1 > B1:
        A1, B1 = B1, A1
        A2, B2 = B2, A2

    da1 = T1 * A1
    da2 = T2 * A2
    db1 = T1 * B1
    db2 = T2 * B2

    if da1 == db1:
        if db1 == db2:
            ans = "infinity"
        else:
            ans = 1
    else:
        if da1 + da2 == db1 + db2:
            ans = "infinity"
        elif da1 + da2 < db1 + db2:
            ans = 0
        else:
            d1 = db1 - da1
            d2 = (da1 + da2) - (db1 + db2)
            ans = 1 + (d1 // d2) * 2
            if d1 % d2 == 0:
                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    T1, T2 = list(map(int, input().split()))
    A1, A2 = list(map(int, input().split()))
    B1, B2 = list(map(int, input().split()))
    # 青木君を基準にした高橋君の位置を追い、0に何回ぶつかるかを調べる
    V1, V2 = A1 - B1, A2 - B2
    if V1 == 0 or V2 == 0 or V1 * V2 > 0:
        print(0)
        return
    # この後は V1 * V2 < 0
    d1, d2 = V1 * T1, V2 * T2
    d12 = d1 + d2
    if d1 * d12 > 0:
        print(0)
    elif d12 == 0:  # d1 * d12 == 0
        print('infinity')
    else:  # d1 * d12 < 0
        cnt = 0
        # d1で0に達する
        cnt += abs(d1) // abs(d12)
        # d2で0に達する
        cnt += 1 + (abs(d1) - 1) // abs(d12)
        print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())

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

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

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

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

    if a2 > b2:
        return print(0)
    else:
        d1 = t1 * a1 + t2 * a2
        d2 = t1 * b1 + t2 * b2
        if d1 == d2:
            return print("infinity")
        elif d2 > d1:
            ans = 1
            diff = d2 - d1
            x = t1 * (a1 - b1)
            cnt = x // diff
            ans += cnt * 2
            if x % diff == 0:
                ans -= 1
            return print(ans)
        else:
            return print(0)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

a = a1 * t1 + a2 * t2
b = b1 * t1 + b2 * t2

if a == b:
    print('infinity')
else:
    if b > a:
        if a1 * t1 < b1 * t1:
            print(0)
        elif a1 * t1 == b1 * t1:
            print(1)
        elif a1 * t1 > b1 * t1:
            c = b - a
            d = a1 * t1 - b1 * t1
            e = d % c != 0
            print(2 * (d // c) + e)
    else:
        if b1 * t1 < a1 * t1:
            print(0)
        elif b1 * t1 == a1 * t1:
            print(1)
        elif b1 * t1 > a1 * t1:
            c = a - b
            d = b1 * t1 - a1 * t1
            e = d % c != 0
            print(2 * (d // c) + e)

Submission status: Accepted

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

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

half, all = t1 * (b1 - a1), ave_a - ave_b
ans = half // all * 2 + (half % all != 0)
print(max(0, ans))

Submission status: Accepted

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

ave_a = t1 * a1 + t2 * a2
ave_b = t1 * b1 + t2 * b2
if ave_a < ave_b:
    (a1, a2), (b1, b2) = (b1, b2), (a1, a2)
    ave_a, ave_b = ave_b, ave_a

if ave_a == ave_b:
    print('infinity')
    exit()

half, all = t1 * (b1 - a1), ave_a - ave_b
ans = half // all * 2 + (half % all != 0)
print(max(0, ans))

Submission status: Accepted

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

ave_a, ave_b = t1 * a1 + t2 * a2, t1 * b1 + t2 * b2
if ave_a < ave_b:
    a1, a2, b1, b2 = b1, b2, a1, a2
    ave_a, ave_b = ave_b, ave_a

if ave_a == ave_b:
    print('infinity')
    exit()

first, second = t1 * (b1 - a1), ave_a - ave_b
ans = first // second * 2 + (first % second != 0)
print(max(0, ans))

Submission status: Accepted

Solution code:
def main(v1, v2, t1, t2):
    if v1 * v2 >= 0:  # どんどん離れる
        return 0
    # v1>0, v2<0とする
    d1 = abs(v1) * t1
    d2 = abs(v2) * t2

    if d1 > d2:  # 追いつけない
        return 0
    if d1 == d2:  # 毎回ちょうど追いつく
        return "infinity"

    dL = d2 - d1
    peak = (d1 + dL - 1) // dL
    if d1 % dL == 0:
        return peak * 2
    else:
        return peak * 2 - 1


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