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

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

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


for i in range(10**7):
    a += a2*t2
    b += b2*t2
    if judge and b > a:
        res += 1
    
    if (not judge) and b < a:
        res += 1
    
    if b > a:
        judge = False
    else:
        judge = True
    if a == b:
        print('infinity')
        exit()
    
    a += a1*t1
    b += b1*t1

    if a == b:
        res += 1
    if judge and b > a:
        res += 1
    
    if (not judge) and b < a:
        res += 1
    
    if b > a:
        judge = False
    else:
        judge = True
    
    if a < b:
        print(res)
        exit()

print('infinity')

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 t[0]*a[0]+t[1]*a[1]==t[0]*b[0]+t[1]*b[1]:
    print('infinity')
    exit()

ans = 0

takahashi = 0
aoki = 0

while True:
    if takahashi == aoki:
        before = 0
    elif takahashi>aoki:
        before = 1
    else:
        before = 2
    takahashi+=t[0]*a[0]
    aoki += t[0]*b[0]
    if takahashi == aoki:
        middle = 0
    elif takahashi>aoki:
        middle = 1
    else:
        middle = 2
    takahashi+=t[1]*a[1]
    aoki += t[1]*b[1]
    if takahashi == aoki:
        after = 0
    elif takahashi>aoki:
        after = 1
    else:
        after = 2
    if before==middle==after:
        break
    if before==1 and middle==2:
        ans += 1
    if before==1 and middle==2:
        ans += 1
    if after==1 and middle==2:
        ans += 1
    if after==1 and middle==2:
        ans += 1
print(ans//2)

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")
else:
    if d1 > 0 and d2 > 0:
        print(0)
    elif d1 < 0 and d2 < 0:
        print(0)


    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)

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

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:
        print ( pl // (mi - pl) * 2 + 1)

    else:
        print ( mi // (pl - mi) * 2 )

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")
else:
    if d1 > 0 and d2 > 0:
        print(0)
    elif d1 < 0 and d2 < 0:
        print(0)


    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**20
    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)

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:
    print('swap')
    a1, a2, b1, b2 = b1, b2, a1, a2
    leap1, leap2 = leap2, leap1

lag = leap2 - leap1
print(leap1, leap2, lag)

# 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:
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) != (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:
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

    if(v1<0 and v2<0):
        print(0)
        return
    if(v1>0 and v2>0):
        print(0)
        return
    if(abs(v1*t1)>abs(v2*t2)):
        print(0)
        return
    if(v1*t1+v2*t2==0):
        print("infinity")
        return

    # v1を負、v2を正とする
    v1=abs(v1)
    v2=abs(v2)
    d=v2*t2-v1*t1
    k=(v1*t1-1)//d+1
    ans=2*k-1
    if(d*k==v1*t1): ans-=1
    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())
a1 -= b1
a2 -= b2

if a1*t1+a2*t2 == 0:
	print("infinity")
	quit()

if a1 < 0:
	a1 = -a1
	a2 = -a2

# assert a1 > 0

if a2 > 0:
	print(0)
	quit()

# assert a2 < 0

d = a1*t1+a2*t2

if d > 0:
	print(0)
	quit()

print(a1*t1, -d)

ans = ((a1*t1-1)//(-d)+1)*2-1
if a1*t1%(-d) == 0:
	ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline 

#n = int(input())
#s = input()
#a = [int(i) for i in readline().split()]
#m = [[int(i) for i in readline().split()] for _ in range(n)]

t1,t2 = [int(i) for i in readline().split()]
a1,a2 = [int(i) for i in readline().split()]
b1,b2 = [int(i) for i in readline().split()]


if a1*t1 + a2*t2 == b1*t1 + b2*t2:
    print("infinity")
else:
    d = a1*t1 + a2*t2 - (b1*t1 + b2*t2)
    if d < 0:
        a1,a2 = a2,a1
        b1,b2 = b2,b1
        t1,t2 = t2,t1
        d = -d
    
    if a1 == b1:
        print("infinity")
    elif a1 > b1:
        print(0)
    else:
        c = (b1-a1)*t1
        r = c//d
        #print(r,c,d)
        if c%d == 0:
           print(2*r)
        else:
            print(2*r+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**18
while ok+1 < ng:
    c = (ok+ng)//2
    if (c-1)*sa+a1 <= (c-1)*sb+b1:
        ok = c
    else:
        ng = c
print(max(ok*2-1, 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 * t1 + a2 * t2 < b1 * t1 + b2 * t2:
    a1, a2, b1, b2 = b1, b2, a1, a2

if a1 > b1:
    print(0)
    exit()

x = (b1 - a1) * t1
y = a1 * t1 + a2 * t2 - b1 * t1 - b2 * t2

print(x // y * 2 + 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 = b1, b2, a1, a2, sb

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

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())
    A1 = a1*t1
    A2 = a2*t2
    B1 = b1*t1
    B2 = b2*t2
    A3 = A1+A2
    B3 = B1+B2
    if A3 == B3:
        print("infinity")
    elif A1 < B1 and A3 < B3:
        print(0)
    elif A1 > B1 and A3 > B3:
        print(0)
    elif A3 < B3:
        gap = B3-A3
        x = A1-B1
        num = x//gap+1
        ans = num*2
        if x%gap == 0:
            ans -= 1
        print(ans)
    elif A3 > B3:
        gap = A3-B3
        x = B1-A1
        num = x//gap
        ans = num*2+1
        if x%gap == 0:
            ans -= 1
        print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    t = LI()
    a = LI()
    b = LI()
    a[0] -= b[0]
    a[1] -= b[1]
    if t[0]*a[0] == -t[1]*a[1]:
        print("infinity")
        return
    if a[0] < 0:
        a[0] *= -1
        a[1] *= -1
    if t[0]*a[0]+t[1]*a[1] > 0:
        print(0)
        return
    l = 0
    r = 10**100
    A = t[0]*a[0]+t[1]*a[1]
    while r-l > 1:
        m = (l+r) >> 1
        x = m*A
        y = x+t[0]*a[0]
        if x*y <= 0:
            l = m
        else:
            r = m
    print(l*2+1)
    return

#Solve
if __name__ == "__main__":
    solve()

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 = b1, b2, a1, a2

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

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 == 0:
        x += ts[0] * vs[0]
    ret.append(x)

    y = (ts[0] * ws[0] + ts[1] * ws[1]) * (i // 2)
    if i % 2 == 0:
        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) != (x2 < y2)


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

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from math import ceil
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

    if(v1<0 and v2<0):
        print(0)
        return
    if(v1>0 and v2>0):
        print(0)
        return
    if(abs(v1*t1)>abs(v2*t2)):
        print(0)
        return
    if(v1*t1+v2*t2==0):
        print("infinity")
        return

    # v1を負、v2を正とする
    v1=abs(v1)
    v2=abs(v2)
    d=v2*t2-v1*t1
    print(ceil((v1*t1)/d+1e-9)*2-1)
resolve()

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")
else:
    if d1 > 0 and d2 > 0:
        print(0)
    elif d1 < 0 and d2 < 0:
        print(0)


    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**20
    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)

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")
if sa < sb:
    a1, a2, b1, b2 = b1, b2, a1, a2

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

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] > B[0]:
  A, B = B, A

if A[1] < B[1]:
  print(0)
  exit()

d = (B[0] - A[0]) * T[0]
k = (A[1] - B[1]) * T[1] - d

if k < 0:
  print(0)
  exit()

if k == 0:
  print("infinity")
  exit()

ans = 1 + (d // k) * 2

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

ng = "infinity"

first = t1 * a1 - t1 * b1
second = t1 * a1 + t2 * a2 - t1 * b1 - t2 * b2

if second == 0:
    print(ng)
    exit()

if first * second > 0:
    print(0)
    exit()

if first < 0:
    first *= -1
    second *= -1
second *= -1
ans = 2 * (first // second) + 1
if first % second == 0:ans +=1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

# 一人止める

def F(T1,T2,A1,A2):
    if A1 == 0:
        return 'infinity'
    if A1 < 0:
        A1 = -A1
        A2 = -A2
    if A2 >= 0:
        return 0
    R = T1 * A1
    L = T2 * (-A2)
    dx = R - L
    if dx == 0:
        return 'infinity'
    if dx > 0:
        return 0
    dx = -dx
    return 2 * (R//dx) + 1

answer = F(T1,T2,A1-B1,A2-B2)
print(answer)

Submission status: Time Limit Exceeded

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

Solution code:
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
if a1==b1 and b2==a2:print(0)
elif t1*(a1-b1)+t2*(-b2+a2)==0:print('infinity')
else:
    if t1*(a1-b1)+t2*(-b2+a2)<0:a1,a2,b1,b2=b1,b2,a1,a2
    if t1*(a1-b1)>0:print(0)
    else:
        #(ng,ok]type
        ok,ng=10**100000,-1
        ng,ok=ok,ng
        eps=10**(-8)
        while abs(ok-ng)>1:
            mid=(ok+ng)//2
            if t1*(b1-a1)-mid>=0:ok=mid
            else:ng=mid
        print(2*(mid//(t1*(a1-b1)+t2*(-b2+a2))+1)-1)

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 a1==b1 and b2==a2:print(0)
elif t1*(a1-b1)+t2*(-b2+a2)==0:print('infinity')
else:
    if t1*(a1-b1)+t2*(-b2+a2)<0:a1,a2,b1,b2=b1,b2,a1,a2
    if t1*(a1-b1)>0:print(0)
    else:
        #(ng,ok]type
        ok,ng=10**300000,-1
        ng,ok=ok,ng
        eps=10**(-8)
        while abs(ok-ng)>1:
            mid=(ok+ng)//2
            if t1*(b1-a1)-mid>=0:ok=mid
            else:ng=mid
        print(2*(mid//(t1*(a1-b1)+t2*(-b2+a2))+1)-1)

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')
else:
  ans=0
  d=0 # a-b
  pre=0
  while True:
    if d!=0:
      if (d+(t1*a1-t1*b1))*d<=0:
        ans+=1
    d+=t1*a1-t1*b1
    if d!=0:
      if (d+(t2*a2-t2*b2))*d<=0:
        ans+=1
    d+=t2*a2-t2*b2
    if pre==ans:break
    pre=ans
  print(ans)

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()))
if T[0]* A[0] + T[1] * A[1] == T[0] * B[0] + T[1] * B[1]:
    print('infinity')
else:
    ans = 0
    A_pos = 0
    B_pos = 0
    before_state = -1 # 0:A>B, 1:A < B, -1:A=B
    flag = True
    while flag:
        for i in range(2):
            A_pos += T[i]* A[i]
            B_pos += T[i]* B[i]
            if A_pos > B_pos:
                if before_state == 0:
                    ans += 1
                before_state = 1
                if A_pos - B_pos > T[(i+1)%2] * B[(i+1)%2]:
                    flag = False
                    break
            elif A_pos < B_pos:
                if before_state == 1:
                    ans += 1
                before_state = 0
                if B_pos - A_pos > T[(i+1)%2] * A[(i+1)%2]:
                    flag = False
                    break
            elif A_pos == B_pos:
                ans += 1
                before_state = -1
    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())
Asum=0
Bsum=0
cnt=1
tcnt=0
if a1*t1+a2*t2==b1*t1+b2*t2:
  print('infinity')
elif (a1>b1 and a2>b2) or (a1<b1 and a2<b2):
  print(0)
else:
  while cnt>0:
    cnt=0
    if (Asum>Bsum and Asum+a1*t1<=Bsum+b1*t1) or (Asum<Bsum and Asum+a1*t1>=Bsum+b1*t1):
      cnt+=1
    Asum+=a1*t1
    Bsum+=b1*t1
    if (Asum>Bsum and Asum+a2*t2<Bsum+b2*t2) or (Asum<Bsum and Asum+a2*t2>Bsum+b2*t2):
      cnt+=1
    Asum+=a2*t2
    Bsum+=b2*t2
    tcnt+=cnt
  print(tcnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

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

A1 = (A1 - B1) * T1
A2 = (A2 - B2) * T2
#print(A1, A2)
if A1 + A2 == 0:
    print('infinity')
    exit()
if A1 * (A1 + A2) > 0:
    print(0)
    exit()
cur = 0
answer = -1
while True:
    cur1 = cur + A1
    if cur * cur1 <= 0:
        answer += 1
    else:
        break
    if cur1 == 0:
        break
    cur = cur1
    cur2 = cur + A2
    if cur * cur2 <= 0:
        answer += 1
    else:
        break
    if cur2 == 0:
        break
    cur = cur2
print(answer)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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

def calc(a, b):
    cnt = 0
    a1 = a + T1 * A1
    b1 = b + T1 * B1
    if a > b and a1 <= b1 or a < b and a1 >= b1:
        cnt += 1
    a2 = a1 + T2 * A2
    b2 = b1 + T2 * B2
    if a1 > b1 and a2 <= b2 or a1 < b1 and a2 >= b2:
        cnt += 1
    if a == b and a2 == b2:
        print('infinity')
        exit()
    return a2, b2, cnt

a = b = 0
ans = 0
while True:
    a, b, cnt = calc(a, b)
    if cnt == 0:
        break
    ans += cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def sign(x):
    if x==0:
        return 0
    else:
        return abs(x)//x
def flag(x,y):
    return sign(x)==sign(y)
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
X=(B1-A1)*T1
Y=(B2-A2)*T2
tmp=0
while(1):
    if tmp==0:
        tmp+=X
    else:
        if not(flag(tmp,tmp+X)):
            ans+=1
            tmp+=X
    if tmp==0:
        tmp+=Y
    else:
        if not(flag(tmp,tmp+Y)):
            ans+=1
            tmp+=Y
    if flag(tmp,tmp+X) and flag(tmp+X,tmp+X+Y):
        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())

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

if D1 * D2 > 0:
    print(0)
elif D2 == 0:
    print("infinity")
else:
    c = 1
    D1 = abs(D2 - D1)
    D2 = abs(D2)
    Dp = D2
    while True:
        Dp += D2
        if Dp > D1:
            break
        elif Dp == D1:
            c += 1
            break
        c += 2
    print(c)

Submission status: Time Limit Exceeded

Solution code:
def is_cross(la, lb, x1, x2):
    ya1 = la[0] * x1 + la[1]
    ya2 = la[0] * x2 + la[1]
    yb1 = lb[0] * x1 + lb[1]
    yb2 = lb[0] * x2 + lb[1]
    if ya1 == yb1:
        return False
    return (ya1 - yb1) * (ya2 - yb2) <= 0

def get_line(c, x, y):
    return [c, y - c * x]

t = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xa = sum([ti * ai for ti, ai in zip(t, a)])
xb = sum([ti * bi for ti, bi in zip(t, b)])
if xa / sum(t) == xb / sum(t):
    print('infinity')
else:
    ans = 0
    t_pre = 0
    da = 0
    db = 0
    while(True):
        ans_t = 0
        for ti, ai, bi in zip(t, a, b):
            t_post = t_pre + ti
            la = get_line(ai, t_pre, da)
            lb = get_line(bi, t_pre, db)
            ans_t += is_cross(la, lb, t_pre, t_post)
            t_pre = t_post
            da += ti * ai
            db += ti * bi

        if ans_t == 0:
            break
        ans += ans_t
    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())

chk = []

tmp = 0
odd = 1

for i in range(10**8):
    if odd == 1:
        tmp += (a1-b1)*t1
        chk.append(tmp)
        odd = 0
    else:
        tmp += (a2-b2)*t2
        chk.append(tmp)
        odd = 1

ans = 0

if chk[1] == 0:
    print('infinity')
    exit()

for i in range(len(chk)-1):
    if chk[i]*chk[i+1] <0 or chk[i] == 0:
        ans += 1

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

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

ans = 0
subs = 0  
sub1 = T1 * (A1 - B1)
sub2 = T2 * (A2 - B2)
X = [1, 1]
i = 0
while (X != [0, 0]):
  i += 1
  if subs != 0:
    if subs ** 2 + subs * sub1 <= 0:
      ans += 1
      X[0] = 1
    else:
      X[0] = 0
  subs = subs + sub1
  if subs != 0:
    if subs ** 2 + subs * sub2 <= 0:
      ans += 1
      X[1] = 1
    else:
      X[1] = 0
  subs = subs + sub2

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(a1, a2)
    g = gcd(g, b1)
    g = gcd(g, b2)
    a1 //= g
    a2 //= g
    b1 //= g
    b2 //= g

    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'

    if a > b and ax > bx:
        return 0

    if a < b and ax < bx:
        return 0

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

    ret = 0
    curr_a, curr_b = 0, 0
    # print(k)
    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
        if abs(curr_b - curr_a) > min(abs(ax - bx), abs(ay - by)):
            break
    return ret


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

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

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

d = 0
count = 0
while (a1 * t1 + a2 * t2) - (b1 * t1 + b2 * t2 - d) > 0:
    d = (a1 * t1 + a2 * t2) - (b1 * t1 + b2 * t2 - d)
    count += 1
print(count)

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()
d1,d2=(a1-b1)*t1,(a2-b2)*t2
tmp=0
ans=0
flag=0
dtmp=0
dold=0
while dtmp*dold<=0:
    dold=dtmp
    if flag==0:
        dtmp+=d1
        flag=1
    else:
        dtmp+=d2
        flag=0
    if dtmp==0:
        ans+=1
        break
    if dtmp*dold<0:
        ans+=1
print(ans)