Problem:


Score: 600 points

Problem Statement

Takahashi and Aoki are training for long-distance races in an infinitely long straight course running from west to east.

They start simultaneously at the same point and moves as follows towards the east:

Takahashi runs A_1 meters per minute for the first T_1 minutes,  then runs at A_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.

Aoki runs B_1 meters per minute for the first T_1 minutes,  then runs at B_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.

How many times will Takahashi and Aoki meet each other, that is, come to the same point? We do not count the start of the run. If they meet infinitely many times, report that fact.

Constraints

1 \leq T_i \leq 100000

1 \leq A_i \leq 10^{10}

1 \leq B_i \leq 10^{10}

A_1 \neq B_1

A_2 \neq B_2

All values in input are integers.

Input

Input is given from Standard Input in the following format:

T_1 T_2
A_1 A_2
B_1 B_2

Output

Print the number of times Takahashi and Aoki will meet each other.

If they meet infinitely many times, print infinity instead.  

Sample Input 1

1 2
10 10
12 4

Sample Output 1

1

They will meet just once, \frac{4}{3} minutes after they start, at \frac{40}{3} meters from where they start.

Sample Input 2

100 1
101 101
102 1

Sample Output 2

infinity

They will meet 101, 202, 303, 404, 505, 606, ... minutes after they start, that is, they will meet infinitely many times.

Sample Input 3

12000 15700
3390000000 3810000000
5550000000 2130000000

Sample Output 3

113

The values in input may not fit into a 32-bit integer type.



Sample input:
1 2
10 10
12 4


Sample output:
1

Submission status: Accepted

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


t1, t2 = list(map(int, sys.stdin.buffer.readline().split()))
a1, a2 = list(map(int, sys.stdin.buffer.readline().split()))
b1, b2 = list(map(int, sys.stdin.buffer.readline().split()))

da1 = t1 * a1
da2 = t2 * a2
db1 = t1 * b1
db2 = t2 * b2
if da1 + da2 > db1 + db2:
    a1, b1 = b1, a1
    a2, b2 = b2, a2
    da1, db1 = db1, da1
    da2, db2 = db2, da2
# b のほうがはやい

# 無限
if da1 + da2 == db1 + db2:
    print('infinity')
    exit()
# 1回も会わない
if da1 < db1:
    print(0)
    exit()

# t1 で出会う回数
cnt = abs(da1 - db1) / abs(da1 + da2 - db1 - db2)
ans = int(cnt) * 2 + 1
if cnt == int(cnt):
    ans -= 1
print(ans)

Submission status: Accepted

Solution code:
tt = list(map(int, input().split()))
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

p = (aa[0] - bb[0]) * tt[0]
q = (aa[1] - bb[1]) * tt[1]

if p > 0:
	p *= -1
	q *= -1

if p + q < 0:
	print(0)
elif p + q == 0:
	print("infinity")
else:
	r, s = divmod((-1 * p), (p + q))
	if s == 0:
		print(r * 2)
	else:
		print(r * 2 + 1)

Submission status: Accepted

Solution code:
t=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if a[0]>b[0]:
    a,b=b,a
p=t[0]*(a[0]-b[0])
q=t[1]*(a[1]-b[1])
if p+q==0:
    print('infinity')
elif p+q<0:
    print(0)
else:
    k=-p//(p+q)
    if -p%(p+q)==0:
        print(2*k)
    else:
        print(2*k+1)

Submission status: Accepted

Solution code:
from sys import stdin
T1,T2 = [int(x) for x in stdin.readline().rstrip().split()]
A1,A2 = [int(x) for x in stdin.readline().rstrip().split()]
B1,B2 = [int(x) for x in stdin.readline().rstrip().split()]
f = (A1-B1)*T1
s = (A2-B2)*T2
 
if f + s == 0:
    print("infinity")

else:
    if (f > 0) and ((f + s) > 0):
        print(0)
    
    elif (f < 0) and ((f + s) < 0):
        print(0)
        
    elif f > 0 and f + s < 0:
        if  f % (-(f + s)) == 0:
            print((f//-(f+s))*2)
        else:
            print((f//-(f+s))*2+1)
    
    elif f < 0 and f + s > 0:
        if (-f) % (f+s) == 0:
            print((-f//(f+s))*2)
        else:
            print((-f//(f+s))*2+1)

Submission status: Accepted

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

Submission status: Accepted

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

fin_gap = t1 * (a1 - b1) + t2 * (a2 - b2)
mid_gap = t1 * (a1 - b1) 

if fin_gap == 0:
  print("infinity")
else:
  if mid_gap * fin_gap > 0:
    print(0)
  else:
    
    mod = abs(mid_gap) % abs(fin_gap)
    q = abs(mid_gap) // abs(fin_gap)
    
    if mod == 0:
      ans = q * 2
    else:
      ans = q * 2 + 1
    
    print(ans)

Submission status: Accepted

Solution code:
t1,t2 = [int(i) for i in input().split()]
a1,a2 = [int(i) for i in input().split()]
b1,b2 = [int(i) for i in input().split()]

c1,c2 = (a1-b1)*t1,(a2-b2)*t2

if c1 < 0:
    c1 *= -1
    c2 *= -1

if  c1 + c2 == 0:
    print('infinity')
elif c1 + c2 > 0:
    print(0)
else:
    num = -c1 // (c1 + c2)
    print(num * 2 if -c1 % (c1 + c2) == 0 else num * 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())
d1 = (A1-B1)*T1
d2 = (A2-B2)*T2
if d1<0: d1,d2 = -d1,-d2
if d1+d2==0: print("infinity")
elif d1+d2>0: print(0)
elif d1%(-d1-d2) == 0: print(2*(d1//(-d1-d2)))
else: print(2*(d1//(-d1-d2))+1)

Submission status: Accepted

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

dis1 = (A1 - B1) * T1
dis2 = (A2 - B2) * T2

if dis1 > 0:
    dis1 = -dis1
    dis2 = -dis2

"""
if dis1 + dis2 <0:
    print(0)

elif dis1 == -dis2:
    print("infinity")
"""

if dis1 + dis2 == 0:
    print("infinity")

elif dis1+dis2 < 0:
    print(0)

else:
    S = dis1//(dis1+dis2)
    R = dis1%(dis1+dis2) 
    if R == 0:
        ans = S*2 
    else:
        ans = S*2 + 1
    
    print(abs(ans))

Submission status: Accepted

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

dis1 = (A1 - B1) * T1
dis2 = (A2 - B2) * T2

if dis1 > 0:
    dis1 = -dis1
    dis2 = -dis2

if dis1 + dis2 <0:
    print(0)

elif dis1 == -dis2:
    print("infinity")
    
else:
    S = dis1//(dis1+dis2)
    R = dis1%(dis1+dis2) 
    if R == 0:
        ans = S*2 
    else:
        ans = S*2 + 1
    
    print(abs(ans))

Submission status: Accepted

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

    if A1 < B1 and A2 < B2:
        return 0
    elif A1 > B1 and A2 > B2:
        return 0

    C1, C2 = B1-A1, B2-A2

    if C1*T1 + C2*T2 == 0:
        return 'infinity'

    if C1*T1 + C2*T2 < 0:
        C1, C2 = -C1, -C2

    if abs(C1*T1) > abs(C2*T2):
        return 0

    x1, x2 = T1, 2*T1+T2
    y1, y2 = C1*T1, 2*C1*T1+C2*T2

    x = -y1 * (x2-x1) // (y2-y1) + x1

    z = x-T1
    T = T1+T2
    num = z//T
    ans = num*2
    dist = C1*T1 + (C1*T1 + C2*T2)*num
    if dist != 0:
        ans += 1

    return int(ans)


print(solve())

Submission status: Accepted

Solution code:
def solve():
    maxCycle = 10**20

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

    C1, C2 = B1-A1, B2-A2
    if C1 > 0:
        C1, C2 = -C1, -C2

    dist = C1*T1 + C2*T2

    def binarySearch(isOK):
        ng, ok = -1, maxCycle
        while abs(ok-ng) > 1:
            mid = (ng+ok) // 2
            if isOK(mid):
                ok = mid
            else:
                ng = mid
        return ok

    def isOK1(c):
        return dist*(c+1) >= 0
    cycle1 = binarySearch(isOK1)
    if cycle1 == maxCycle:
        return 0

    def isOK2(c):
        return dist*c > 0
    cycle2 = binarySearch(isOK2)
    if cycle2 == maxCycle:
        return 'infinity'

    def isOK3(c):
        return dist*c + C1*T1 >= 0
    cycle3 = binarySearch(isOK3)

    def isOK4(c):
        return dist*c + C1*T1 > 0
    cycle4 = binarySearch(isOK4)

    ans = 1*(cycle2-cycle1) + 2*(cycle3-cycle2) + 1*(cycle4-cycle3)
    return ans


print(solve())

Submission status: Accepted

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

T1, T2, A1, A2, B1, B2 = map(int, read().split())
answer = 0
v1 = A1 - B1
v2 = A2 - B2
d = v1 * T1 + v2 * T2

if d == 0:
    print('infinity')
    exit()
elif v1 * d > 0:
    print(0)
    exit()

if v1 * T1 % -d == 0:
    print(v1 * T1 // -d * 2)
else:
    print(v1 * T1 // -d * 2 + 1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from itertools import combinations
import sys
input = sys.stdin.readline
MOD = 10**9 + 7

t1, t2 = [int(item) for item in input().split()]
a1, a2 = [int(item) for item in input().split()]
b1, b2 = [int(item) for item in input().split()]
a1 *= t1
b1 *= t1
a2 *= t2
b2 *= t2

if a1 + a2 == b1 + b2:
    print("infinity")
    exit()
if a1 + a2 > b1 + b2:
    a1, a2, b1, b2 = b1, b2, a1, a2

diff = (b1 + b2) - (a1 + a2)
mid_diff = b1 - a1
if mid_diff > 0:
    print(0)
else:
    ans = (abs(mid_diff) + diff - 1) // diff * 2
    if abs(mid_diff) % diff == 0:
        ans += 1
    print(ans - 1)

Submission status: Accepted

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
mid = (A1 - B1) * T1
end = mid + (A2 - B2) * T2
if end == 0:
	print("infinity")
elif mid*end > 0:
	print(0)
else:
	if mid % end == 0:
		print(-2*(int(mid/end)))
	else:
		print(1-2*(int(mid/end)))

Submission status: Accepted

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
 
P = (A1 - B1) * T1
Q = (A2 - B2) * T2
if P > 0:
	P *= -1
	Q *= -1
 
if P + Q < 0:
	print(0)
elif P + Q == 0:
	print("infinity")
else:
	S = (-P) // (P + Q)
	T = (-P) % (P + Q)
	if T != 0:
		print(S * 2 + 1)
	else:
		print(S * 2)

Submission status: Accepted

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

P = (A1 - B1) * T1
Q = (A2 - B2) * T2

if P > 0:
    P, Q = -P, -Q

if P + Q < 0:
    print(0)
elif P + Q == 0:
    print("infinity")
else:
    S, T = divmod(-P, P + Q)
    print(S * 2 + (T != 0))

Submission status: Accepted

Solution code:
def main():
    # input
    import math
    t1, t2 = map(int, input().split())
    a1, a2 = map(int, input().split())
    b1, b2 = map(int, input().split())

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

    if x == -y:
        print("infinity")
        return
    elif x*y>0 or abs(x)>abs(y):
        print(0)
        return

    x = abs(x)
    diff = abs(diff)
    ans = x//diff
    remainder = x%diff
    if remainder==0:
        print(ans*2)
    else:
        print(ans*2+1)

    # calc

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())
x = (a1 - b1) * t1
y = (a2 - b2) * t2
p, q = x // abs(x), y // abs(y)
#print(x, y)
if x + y == 0:
    print('infinity')
else:
    l, r = 0, 10 ** 40 + 1
    ans = 0
    while r - l > 1:
        m = (l + r) // 2
        t = (x + y) * m
        if (t - y < 0) ^ (t <= 0):
            l = m
        else:
            r = m
    ans += l
    l, r = 0, 10 ** 40 + 1
    while r - l > 1:
        m = (l + r) // 2
        t = (x + y) * m
        if (t + x <= 0) ^ (t < 0):
            l = m
        else:
            r = m
    ans += l
    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*a1
a2 = t2*a2
b1 = t1*b1
b2 = t2*b2

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

elif (a1 + a2) > (b1 + b2):
    a1, b1 = b1, a1
    a2, b2 = b2, a2
if b1 > a1:
    print(0)
    exit(0)

tmp00 = a1 - b1
tmp01 = b1 + b2 - a1 - a2

ans = tmp00 // tmp01 * 2 + 1
if tmp00 % tmp01 == 0:
    ans -= 1

print(ans)

Submission status: Accepted

Solution code:
t1, t2 = map(int,input().split())
a1, a2 = map(int,input().split())
b1, b2 = map(int,input().split())
 
def ispos(x):
    return x==abs(x)

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

Submission status: Accepted

Solution code:
import sys

#+++++
		
def main():
	t1, t2 = map(int, input().split())
	a1, a2 = map(int, input().split())
	b1, b2 = map(int, input().split())
	
	a_d = t1*a1 + t2 * a2
	b_d = t1*b1 + t2 * b2
	
	if a_d == b_d:
		return 'infinity'
	
	pp=a_d-b_d
	pc=t1*(a1-b1)
	if pp*pc > 0:
		return 0
	
	#pa((pp,pc))

		
	return (2 * (-pc // pp) + 1) -(1 if pc % pp == 0 else 0)
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    def nibutan(ok,ng):
        while abs(ok-ng) > 1:
            mid = (ok + ng) // 2
            if solve(mid,2):
                ok = mid
            else:
                ng = mid
        return ok
    
    def solve(mid,n):
        dif=(d_0+d_1)*(mid-1)
        c=0
        if dif*(dif+d_0) == 0:
            c+=1
        elif dif*(dif+d_0) < 0:
            c+=1
            if (dif+d_0)*(dif+d_0+d_1) < 0:
                c+=1
        if c==n:
            return True
        else:
            return False
    
    T=list(map(int,input().split()))
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))
    d_0=T[0]*(A[0]-B[0])
    d_1=T[1]*(A[1]-B[1])
    if d_0==-d_1:
        print('infinity')
    elif d_0*(d_0+d_1)<0:
        if (d_0*2+d_1)*(d_0*2+d_1*2)<0:
            n=nibutan(2,10**40)
            ans=n*2-1
            ans+=solve(n+1,1)
            print(ans)
        else:
            print(1)
    else:
        print(0)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

x = t[0] * (a[0] - b[0])
y = x + t[1] * (a[1] - b[1])
if x*y > 0:
	print(0)
elif x*y == 0:
	print('infinity')
elif abs(x) % abs(y) == 0:
	print(2 * (abs(x) // abs(y)))
else:
	print(2 * (abs(x) // abs(y)) + 1)

Submission status: Accepted

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

a1 *= t1
a2 *= t2
b1 *= t1
b2 *= t2

if a1 > b1:
    l1 = a1
    l2 = a2
    s1 = b1
    s2 = b2
else:
    l1 = b1
    l2 = b2
    s1 = a1
    s2 = a2


if l1 + l2 == s1 + s2:
    print('infinity')
elif l1 + l2 < s1 + s2:
    lim = l1 - s1
    if lim % (s1+s2-l1-l2):
        print(1 + 2 * (lim // (s1+s2-l1-l2)))
    else:
        print(2 * (lim // (s1+s2-l1-l2)))
else:
    print(0)

Submission status: Accepted

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

if T1 * A1 + T2 * A2 == T1 * B1 + T2 * B2:
    print('infinity')
    exit()
if (A1 > B1 and A1 * T1 + A2 * T2 > B1 * T1 + B2 * T2) or \
        (A1 < B1 and A1 * T1 + A2 * T2 < B1 * T1 + B2 * T2):
    print(0)
    exit()
diff = abs(A1-B1) * T1
diff2 = abs(T1 * A1 + T2 * A2 - (T1 * B1 + T2 * B2))
x, y = divmod(diff, diff2)
print(x * 2 if y == 0 else x * 2 + 1)

Submission status: Accepted

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

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


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


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


def read_str_n():
    return list(map(str, input().split()))


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


def mt(f):
    import time

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

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

    return wrap


@mt
def slv(T1, T2, A1, A2, B1, B2):
    la1 = T1*A1
    la2 = T2*A2
    lb1 = T1*B1
    lb2 = T2*B2

    la = la1 + la2
    lb = lb1 + lb2
    if la == lb:
        return 'infinity'

    if la1 > lb1 and la2 > lb2:
        return 0
    if la1 < lb1 and la2 < lb2:
        return 0

    if la > lb and la1 > lb1:
        return 0

    if la < lb and la1 < lb1:
        return 0


    p = la1 - lb1
    q = la2 - lb2
    if p > q:
        p *= -1
        q *= -1
    ans = -p//(q+p)*2
    if -p % (q+p) != 0:
        ans += 1
    return ans

def main():
    T1, T2 = read_int_n()
    A1, A2 = read_int_n()
    B1, B2 = read_int_n()
    print(slv(T1, T2, A1, A2, B1, B2))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

if a1+a2 < b1+b2:
  a1,b1 = b1,a1
  a2,b2 = b2,a2
#print(a1,a2,b1,b2)

if a1+a2 == b1+b2:
  print('infinity')
else:
  if a1 > b1:
    print(0)
  else:
    c = (b1-a1)/((a1+a2)-(b1+b2))
    print(int(c)*2 if c.is_integer() else int(c)*2+1)

Submission status: Accepted

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


def main():
    T1, T2 = [int(x) for x in input().split()]
    A1, A2 = [int(x) for x in input().split()]
    B1, B2 = [int(x) for x in input().split()]

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


    x = (T1 * A1 + T2 * A2) - (T1 * B1 + T2 * B2)
    y = (T1 * A1) - (T1 * B1)
    if (y > 0 and x > 0) or (y < 0 and x < 0):
        print("0")
        return
    x = abs(x)
    y = abs(y)
    count = (y // x) * 2
    if y % x != 0:
        count += 1
    print(count)






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

p=(a1-b1)*t1
q=(a2-b2)*t2
if p>0:
  p*=-1
  q*=-1

if p+q<0:
  print(0)
elif p+q==0:
  print('infinity')
else:
  s,t=divmod(-p,p+q)
  if t!=0:
    print(s*2+1)
  else:
    print(s*2)

Submission status: Accepted

Solution code:
*T, = map(int,input().split())
*A, = map(int,input().split())
*B, = 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]<B[0] : A,B = B,A
    if A[0]*T[0]+A[1]*T[1] > B[0]*T[0]+B[1]*T[1] :
        print(0)
    else:
        L = (A[0]-B[0]) * T[0]
        d = -((A[0]-B[0]) * T[0] + (A[1]-B[1]) * T[1])
        ans = L//d*2 + (L%d>0)
        print(ans)

Submission status: Accepted

Solution code:
import math
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
#相対速度
c1,c2=a1-b1,a2-b2
#離れる距離
d1,d2=abs(c1*t1),-abs(c2*t2)

if (c1>0 and c2>0) or (c1<0 and c2<0) or d1+d2>0:
    print(0)
elif d1+d2==0:
    print("infinity")
else:
    x=d1//(-(d1+d2))
    y=d1%(-(d1+d2))
    if y==0:
        print(2*x)
    else:
        print(2*x+1)

Submission status: Accepted

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

SA = T1 * A1 + T2 * A2
SB = T1 * B1 + T2 * B2

if SA == SB or A1 == B1:
    print('infinity')
    exit()

if SA > SB and A1 < B1:
    M = (B1 - A1) * T1
    d = SA - SB
    cnt = M // d
    ans = cnt * 2
    if M % d != 0:
        ans += 1
    print(max(0, ans))
elif SB > SA and B1 < A1:
    M = (A1 - B1) * T1
    d = SB - SA
    cnt = M // d
    ans = cnt * 2
    if M % d != 0:
        ans += 1
    print(max(0, ans))
else:
    print(0)

Submission status: Accepted

Solution code:
s,t,a,b,x,y=map(int,open(0).read().split())
i=(x-a)*s
j=(b-y)*t-i
print(max(0,i//j*2+(i%j!=0))if j else'infinity')

Submission status: Accepted

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

d1 = T[0]*(A[0]-B[0])
d2 = d1 + T[1]*(A[1]-B[1])

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

Submission status: Accepted

Solution code:
import sys
from math import ceil
def main():
    T1, T2, A1, A2, B1, B2 = map(int, sys.stdin.read().split())
    
    # 相対速度
    BA1 = A1 - B1
    BA2 = A2 - B2

    if BA1 == 0:
        print(0)
        sys.exit()

    # A1, B1の速い方をAとする
    if BA1 < 0:
        BA1 = -BA1
        BA2 = -BA2
    
    d = BA1 * T1 + BA2 * T2
    if d > 0:
        print(0)
        sys.exit()
    if d == 0:
        print('infinity')
        sys.exit()
    
    # n: n * d + BA1 * T1 <= 0となる最小の整数
    # d < 0なので
    # n >= (- BA1 * T1) / d
    n = ceil((- BA1 * T1) / d)
    if n * d == - BA1 * T1:
        ans = 2 * n
    else:
        ans = 2 * n - 1
    
    print(ans)
    
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())
# 必ずB1の方を大きくする必要がある
if A1 > B1:
    B1, A1 = A1, B1
    B2, A2 = A2, B2
P = T1*(A1-B1)  # Pは負の値
Q = T2*(A2-B2)
if P + Q == 0:
    print("infinity")
    exit()
elif P + Q < 0:  # 出会う位置まで戻ってこれなかった
    print(0)
    exit()
elif P + Q > 0:  # 限られた数だけあえる
    if (-P % (P + Q)) == 0:
        print(2*(-P // (P+Q)))
    else:
        print(2*(-P // (P+Q))+1)

Submission status: Accepted

Solution code:
import sys
from math import ceil

def main():
    T1, T2, A1, A2, B1, B2 = map(int, sys.stdin.read().split())

    BA1 = A1 - B1
    BA2 = A2 - B2

    if BA1 == 0:
        print(0)
        sys.exit()
    
    if BA1 < 0:
        BA1 = -BA1
        BA2 = -BA2
    
    d = BA1 * T1 + BA2 * T2

    if d > 0:
        print(0)
        sys.exit()
    if d == 0:
        print('infinity')
        sys.exit()

    # dは負なので
    n = ceil((- BA1 * T1) / d) 
    if n * d + BA1 * T1 == 0:
        ans = n * 2
    else:
        ans = n * 2 - 1

    print(ans)

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())
Ad = A1 * T1 + A2 * T2
Bd = B1 * T1 + B2 * T2
if Ad == Bd:
    print('infinity')
else:
    if Bd > Ad:
        Ad, Bd = Bd, Ad
        A1, B1 = B1, A1
    if A1 > B1:
        print(0)
    else:
        num = (B1 - A1) * T1 // (Ad - Bd)
        if (B1 - A1) * T1 % (Ad - Bd) == 0:
            print(num * 2)
        else:
            print(num * 2 + 1)

Submission status: Accepted

Solution code:
import sys
from math import ceil
def main():
    T1, T2, A1, A2, B1, B2 = map(int, sys.stdin.read().split())
    
    # 相対速度
    BA1 = A1 - B1
    BA2 = A2 - B2

    if BA1 == 0:
        print(0)
        sys.exit()

    # A1, B1の速い方が速い方をAとする
    if BA1 < 0:
        BA1 = -BA1
        BA2 = -BA2
    
    d = BA1 * T1 + BA2 * T2
    if d > 0:
        print(0)
        sys.exit()
    if d == 0:
        print('infinity')
        sys.exit()
    
    n = ceil((- BA1 * T1) / d)
    if n * d == - BA1 * T1:
        ans = 2 * n
    else:
        ans = 2 * n - 1
    
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from math import ceil
def main():
    T1, T2, A1, A2, B1, B2 = map(int, sys.stdin.read().split())
    
    BA1 = A1 - B1
    BA2 = A2 - B2

    if BA1 == 0:
        print(0)
        sys.exit()

    # A1, B1の速い方が速い方をA1とする
    if BA1 < 0:
        BA1 = -BA1
        BA2 = -BA2
    
    d = BA1 * T1 + BA2 * T2
    if d > 0:
        print(0)
        sys.exit()
    if d == 0:
        print('infinity')
        sys.exit()
    
    n = ceil((- BA1 * T1) / d)
    if n * d == - BA1 * T1:
        ans = 2 * n
    else:
        ans = 2 * n - 1
    
    print(ans)
    
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())
#t1で生まれる差
P = (a1 - b1) * t1
#t2で生まれる差
Q = (a2 - b2) * t2
#P < 0 のことを仮定する
if P > 0:
    P *= -1
    Q *= -1

if P + Q < 0:
    print(0)
elif P + Q == 0:
    print("infinity")
else:
    #つぎのPを足したときに、負にいくかどうか？
    i = P * (-1)
    if i % (P + Q) == 0:
        print(2 * (i // (P + Q)))
    else:
        print(2 * (i // (P + Q)) + 1)

Submission status: Accepted

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

input = sys.stdin.readline


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

    P = t1 * (b1 - a1)
    Q = t2 * (b2 - a2)
    if P + Q == 0:
        print("infinity")
        return
    if P * (P + Q) > 0:
        print(0)
        return
    P = abs(P)
    Q = abs(Q)
    T = P % (Q - P)
    S = P // (Q - P)
    if T == 0:
        ans = 2 * S
    else:
        ans = 2 * S + 1
    print(ans)


main()

Submission status: Accepted

Solution code:
import sys
import math
t1, t2 = [int(i) for i in sys.stdin.readline().split()]
a1, a2 = [int(i) for i in sys.stdin.readline().split()]
b1, b2 = [int(i) for i in sys.stdin.readline().split()]
a1 -= b1
a2 -= b2
_sum = a1 * t1 + a2 * t2
if _sum == 0:
    print("infinity")
elif _sum * a1 > 0:
    print("0")
else:
    cnt = (-a1 * t1) / _sum
    res = math.ceil(cnt) * 2 - 1 + (cnt % 1 == 0)
    print(res)

Submission status: Accepted

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

p = (a1 - b1) * t1
q = (a2 - b2) * t2
if p > 0:
    p = -1 * p
    q = -1 * q

if p + q < 0:
    print(0)
elif p + q == 0:
    print('infinity')
else:
    s = -p // (p+q)
    t = -p % (p+q)
    if t == 0:
        print(2*s)
    else:
        print(2*s+1)

Submission status: Accepted

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

def bisearch_max(mn, mx, func):
    """ 条件を満たす最大値を見つける二分探索 """

    ok = mn
    ng = mx
    while ok+1 < ng:
        mid = (ok+ng) // 2
        if func(mid):
            # 上を探しに行く
            ok = mid
        else:
            # 下を探しに行く
            ng = mid
    return ok

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

# a,bの状態からT1,T2の操作を行った後のa,bと、この操作での交差回数を返す
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
    return a2, b2, cnt

# x+1回目の操作でまだ交差するか
def check(x):
    a = (T1 * A1 + T2 * A2) * x
    b = (T1 * B1 + T2 * B2) * x
    _, _, cnt = calc(a, b)
    return cnt >= 1

# 初回で同値に飛んだらその先もずっと繰り返す
a, b, cnt = calc(0, 0)
if a == b:
    print('infinity')
    exit()

# 最後に交差する直前までの操作回数を調べる
res = bisearch_max(-1, 10**18, check)
# 初回でも交差しなければ0
if res == -1:
    print(0)
    exit()

# 直前までの操作では、初回は1回、以降は2回ずつ交差する
ans = max(0, res * 2 - 1)
# 最後に交差する直前までa,bを移動させる
a = (T1 * A1 + T2 * A2) * res
b = (T1 * B1 + T2 * B2) * res
# 最後が1回か2回か確認
_, _, cnt = calc(a, b)
ans += cnt
print(ans)

Submission status: Accepted

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

    d1 = a1*t1
    d2 = b1*t1

    dd1 = d1 - d2

    d1 += a2*t2
    d2 += b2*t2

    if d1 == d2:
        return "infinity"

    dd2 = d1 - d2

    if dd1 * dd2 > 0:
        return 0
    else:
        d1 += a1*t1
        d2 += b1*t1

        dd3 = d1 - d2

        if dd2 * dd3 > 0:
            return 1
        else:
            d1 += a2*t2
            d2 += b2*t2

            dd4 = d1 - d2
        if dd3 * dd4 > 0:
            return 2
        else:
            if dd1 > dd3:
                if dd1 % (dd1-dd3) == 0:
                    return (dd1 // (dd1-dd3) )*2
                else:
                    return (dd1 // (dd1-dd3) )*2+1
            else:
                if dd2 % (dd2-dd4) == 0:
                    return (dd2 // (dd2-dd4) )*2 +1
                else:
                    return (dd2 // (dd2-dd4) )*2+2



print(main())

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    T1,T2 = MI()
    A1,A2 = MI()
    B1,B2 = MI()
    if T1*A1+T2*A2==T1*B1+T2*B2:
        print("infinity")
        exit()
    A1*=T1
    B1*=T1
    A2*=T2
    B2*=T2
    diff1 = A1-B1
    diff2 = A2-B2
    # print(diff1,diff2)

    if diff1*diff2>0:
        print(0)
    else:
        if abs(diff1)>abs(diff2):
            print(0)
            exit()
        else:
            ans = 2 * (abs(diff1) // abs(diff1+diff2))
            if diff1//(diff1+diff2) == diff1/(diff1+diff2):
                ans -= 1 ##ピッタリ原点
            print(ans + 1)
    

if __name__ == '__main__':
    main()