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 sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

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

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

if P+Q == 0:
	print("infinity")
	exit()
elif 0 <= P*(P+Q):
	print(0)
	exit()
else:
	a = abs(P)
	b = abs(P - (2*P+Q))
	n = a//b
	if a%b:
		print(2*n+1)
	else:
		print(2*n)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

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

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

elif 0 < ((A1-B1)*T1) * ((A1-B1)*T1 + (A2-B2)*T2):
	print(0)

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

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+a2*t2==b1*t1+b2*t2:
    print("infinity")
    quit()
x = (a1-b1)*t1
y = (a2-b2)*t2
if x < 0:
    x,y = -x,-y
d = x+y
if d > 0:
    print(0)
    quit()
if x%(-d) == 0:
    print(2*(x//(-d)))
else:
    print(2*(x//(-d))+1)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
def sumitb2019_f():
    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:
        ans = 0
    elif p + q == 0:
        ans = 'infinity'
    else:
        s, t = divmod(-p, p + q)
        if t == 0: ans = s * 2
        else: ans = s * 2 + 1
    print(ans)

if __name__ == '__main__':
    sumitb2019_f()

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>b1 and a2>b2:
    print(0)
elif b1>a1 and b2>a2:
    print(0)
elif a1*t1+a2*t2==b1*t1+b2*t2:
    print("infinity")
else:
    ans=-1
    num2=-1
    num3=-1
    if a1*t1+a2*t2>b1*t1+b2*t2:
        if a1>b1:
            ans=0
        else:
            num2=(b1-a1)*t1
            num3=((a1-b1)*t1)+((a2-b2)*t2)
            if num2//num3==num2/num3:
                ans=(num2//num3)*2
            else:
                ans=(num2//num3)*2+1
    else:
        if b1>a1:
            ans=0
        else:
            num2=(a1-b1)*t1
            num3=((b1-a1)*t1)+((b2-a2)*t2)
            if num2//num3==num2/num3:
                ans=(num2//num3)*2
            else:
                ans=(num2//num3)*2+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())

a=a1-b1
b=a2-b2

if a<=0:
    a *= -1
    b *= -1

X=t1*a
Y=t2*b


if X+Y==0:
    print('infinity')
elif X+Y>0:
    print(0)
else:
    T=X//abs(X+Y)
    if X%abs(X+Y)==0:
        print(T*2)
    else:
        print(T*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())

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

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

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


diff1 = abs(a1-b1)
diff2 = abs(a2-b2)

first_dist = diff1*t1
second_dist = diff2*t2

down = second_dist - first_dist
if down < 0:
    print(0)
    exit()
if down == 0:
    print('infinity')
    exit()

if first_dist%down != 0:
    ans = (first_dist//down) * 2 + 1
else:
    ans = ((first_dist//down)-1) * 2 + 2

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

a = (a1-b1)*t1
b = (a2-b2)*t2
if a < 0:
    a *= -1
    b *= -1

if a == -b:
    print("infinity")
    exit()
if a+b > 0:
    print(0)
else:
    c = -a-b
    print((a+c-1)//c + a//c)

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
b1 *= t1
a2 *= t2
b2 *= t2

if a1 + a2 == b1 + b2:
    print("infinity")
    exit()
if (a1 < b1 and a1+a2 < b1+b2) or (b1 < a1 and b1+b2 < a1+a2):
    print(0)
    exit()

d2 = abs(a1+a2-b1-b2)
d1 = abs(a1-b1)
print(1 + 2*(d1//d2) - (d1 % d2 == 0))

Submission status: Accepted

Solution code:

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

d1 = t1 * (a1 - b1)
d2 = t2 * (a2 - b2)

if d1 > 0:
    d1 *= -1
    d2 *= -1

if d1 + d2 == 0:
    print("infinity")
elif d1 + d2 < 0:
    print(0)
else:
    cnt = -d1 // (d1 + d2)
    t = -d1 % (d1 + d2)
    ans = 2 * cnt + int(t != 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())
if a1 < b1:
    a1, b1 = b1, a1
    a2, b2 = b2, a2
a1 *= t1
b1 *= t1
a2 *= t2
b2 *= t2
a = a1 + a2
b = b1 + b2
if a == b:
    print("infinity")
elif a > b:
    print(0)
else:
    d1 = a1 - b1
    d2 = b2 - a2
    if d1 % (d2 - d1) == 0:
        ans = d1 // (d2 - d1) * 2
    else:
        ans = d1 // (d2 - d1) * 2 + 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())
atot1 = (a1 - b1) * t1
atot2 = (a2 - b2) * t2
if atot1 > 0:
    atot1 *= -1
    atot2 *= -1


if atot1 + atot2 < 0:
    print(0)
    exit()
if atot1 == - atot2:
    print('infinity')
    exit()
S = (-atot1) // (atot1 + atot2)
T = (-atot1) % (atot1 + atot2)
if T != 0:
    print(2 * S + 1)
else:
    print(2 * S)

Submission status: Accepted

Solution code:
import sys

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

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

e1 = (a1-b1)*t1
e2 = e1 + (a2-b2)*t2

if e1*e2 > 0:
  print(0)
  sys.exit()

e1, e2 = abs(e1), abs(e2)

if e1%e2:
  print(1+2*(e1//e2))
else:
  print(2*(e1//e2))

Submission status: Accepted

Solution code:
s,t = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
a = a1-b1
b = a2-b2
if a*b>0:print(0);exit()
if abs(a)*s ==abs(b)*t:print('infinity');exit()
elif abs(a)*s >abs(b)*t:print(0);exit()
if (abs(a)*s)%(abs(b)*t-abs(a)*s):
    print(int(abs(a)*s//(abs(b)*t-abs(a)*s))*2+1)
else:
    print(int(abs(a)*s//(abs(b)*t-abs(a)*s))*2)

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
q=a2-b2
if p*t1+q*t2==0:print('infinity');exit()
elif p<0:p,q=-p,-q
#p>0
if p*t1+q*t2>0:print(0)
else:
    a=p*t1
    d=-(a+q*t2)
    print(2*(a//d)+int(not(not a%d)))

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

if A2>=B2:
    print(0)
else:
    sta=T1*(A1-B1)
    gen=T2*(A2-B2)
    if sta==-gen:
        print("infinity")
    elif sta+gen>0:
        print(0)        
    else:
        ans=sta//abs(sta+gen)*2+1
        if sta%abs(sta+gen)==0:
            ans-=1
        print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict


def input():
    return sys.stdin.readline().rstrip()


def main():
    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
    first1 = T1 * A1
    first2 = T1 * B1
    second1 = T2 * A2
    second2 = T2 * B2

    if len1 == len2:
        print("infinity")
        exit()

    if len1 > len2:
        if first1 < first2:
            a, b = divmod(first2 - first1, len1 - len2)
            if b == 0:
                print(a * 2)
                exit()
            else:
                print(a * 2+1)
                exit()
        else:
            print(0)
            exit()

    if len2 > len1:
        if first2 < first1:
            a, b = divmod(first1 - first2, len2 - len1)
            if b == 0:
                print(a * 2 )
                exit()
            else:
                print(a * 2+1)
                exit()
        else:
            print(0)
            exit()



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())
a = a1 - b1
b = a2 - b2
if a < 0:
    a = -a
    b = -b
p = t1 * a + t2 * b
if p > 0:
    print(0)
    exit()
if p == 0:
    print('infinity')
    exit()
top = a * t1
p = -p
ans = (top // p + 1) * 2 - 1 - (top % p == 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())

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

if T > A:
  if A1 > B1:
    print(0)
    exit()
  else:
    tdif = T-A #7-5
    pdif = T1*(B1-A1) #5
    if pdif%tdif == 0:
      ans = 2*(pdif//tdif)+1
    else:
      ans = 2*(1+pdif//tdif)
    ans -=1 #原点の分を引く
else:
  if B1 > A1:
    print(0)
    exit()
  else:
    tdif = A-T #7-5
    pdif = T1*(A1-B1) #5
    if pdif%tdif == 0:
      ans = 2*(pdif//tdif)+1
    else:
      ans = 2*(1+pdif//tdif)
    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())
if T1*A1+T2*A2==T1*B1+T2*B2:
    cnt="infinity"
elif T1*A1+T2*A2>T1*B1+T2*B2:
    L = T1*(B1-A1)
    d = T1*A1+T2*A2-(T1*B1+T2*B2)
    if L<0:
        cnt = 0
    elif L==0:
        cnt = 1
    else:
        cnt = 1
        n = L//d
        r = L%d
        if r>0:
            cnt += 2*n
        else:
            cnt += 2*(n-1)+1
elif T1*A1+T2*A2<T1*B1+T2*B2:
    L = T1*(A1-B1)
    d = (T1*B1+T2*B2)-(T1*A1+T2*A2)
    if L<0:
        cnt = 0
    elif L==0:
        cnt = 1
    else:
        cnt = 1
        n = L//d
        r = L%d
        if r>0:
            cnt += 2*n
        else:
            cnt += 2*(n-1)+1
print(cnt)

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(' ')))
n = t1*a1+t2*a2
m = t1*b1+t2*b2
if(n==m):
    print("infinity")
    exit(0)
ans1 = max(0,math.ceil((b1-a1)*t1/(n-m)))
ans2 = max(0,math.ceil((t1*(b1-a1)+t2*(b2-a2))/(n-m))-1)
#if((t1*(b1-a1))%(n-m) == 1):
#    ans3 = 1
#else:
#    ans3 = 0
ans3 = max(0,math.floor(t1*(b1-a1)/(n-m))+1)
print(max(ans1+ans2+ans3-1,0))

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

if D1*T1 + D2*T2 > 0:
    print(0)
    exit()
if D1*T1 + D2*T2 == 0:
    print('infinity')
    exit()

p = D1*T1
q = -D2*T2
d = q-p
k = p//d
b = int(q*k == p*(k+1))
print(1 + k*2 - b)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

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

if not (A1-B1)*(A2-B2)<0:
    print(0)
else:
    if A1<B1:
        A1, B1 = B1, A1
        A2, B2 = B2, A2
    if A1*T1+A2*T2==B1*T1+B2*T2:
        print('infinity')
        exit(0)
    rest = (A1-B1)*T1
    come = (B2-A2)*T2
    if come<rest:
        print(0)
    else:
        ans = come//(come-rest)
        last = 1 if come%(come-rest)==0 else 0
        print(ans*2-1-last)

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 - b1) * (a2 - b2) > 0) or (abs(a1 - b1) * t1 > abs(a2 - b2) * t2):
  print(0)
elif abs(a1 - b1) * t1 == abs(a2 - b2) * t2:
  print("infinity")
else:
  d, m = divmod(abs(a1 - b1) * t1, abs(a2 - b2) * t2 - abs(a1 - b1) * t1)
  ans = d * 2
  if m > 0:
    ans += 1
  print(ans)

Submission status: Accepted

Solution code:
import sys
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
n1=(b1-a1)*t1
n2=(b2-a2)*t2
if n1*n2>0 or abs(n1)>abs(n2):
  print(0)
  sys.exit()
if abs(n1)==abs(n2):
  print('infinity')
  sys.exit()
c=abs(n1)//abs(n1+n2)
while (c+1)*abs(n1+n2)<=abs(n1):
  c+=1
ans=2*c
if abs(n1)%(abs(n1+n2))!=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())


Ax = A1 * T1 + A2 * T2
Bx = B1 * T1 + B2 * T2
diff = abs(Ax - Bx)

if diff == 0:
    print('infinity')
    exit()

if not ((A1 * T1 > B1 * T1 and Ax < Bx) or (A1 * T1 < B1 * T1 and Ax > Bx)):
    print(0)
    exit()


ans = 2 * ((abs(B1 - A1) * T1) // diff)
if (abs(B1 - A1) * T1) % diff != 0:
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

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

# a1 が先行にする
if s1<0:
    s1*=-1
    s2*=-1

d = s1*t1 + s2*t2
mxd = s1*t1

if d>0:
    print(0)
elif d==0:
    print('infinity')
else:
    if mxd%abs(d)==0:
        print(mxd//abs(d)*2)
    else:
        print(mxd//abs(d)*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())

C1 = A1 - B1
C2 = A2 - B2

if C1 * C2 >= 0:
    print(0)

else:
    if C1 < 0:
        C1, C2 = -C1, -C2
    P = T1 * C1
    Q = P + T2 * C2
    if Q > 0:
        print(0)
    elif Q == 0:
        print('infinity')
    else:
        hi = 10**18
        lo = -1
        while hi - lo > 1:
            mid = (hi + lo) // 2
            if Q * mid + P <= 0:
                hi = mid
            else:
                lo = mid
        if Q * hi + P == 0:
            print(hi * 2)
        else:
            print(hi * 2 - 1)

Submission status: Accepted

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


#A1>=B1にする
if A1>=B1:
    pass
else:
    A1,B1=B1,A1
    A2,B2=B2,A2

D1=(A1-B1)*T1#>=0
D2=(A2-B2)*T2#?

if D1==-D2:
    print("infinity")
elif D1+D2>=0:
    print("0")
else:
    diff = abs(D1+D2)#マイからこれだけ差が開く
    if D1%diff==0:
        print(D1//diff*2)
    else:
        print(ceil(D1/diff)*2-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()))


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

if L1 * L2 > 0:
    print(0)
elif L2 == 0:
    print("infinity")
else:
    M = (-L1) / L2
    ans = 2 * int(M) + 1
    if float(int(M)) == M:
        ans -= 1
    print(ans)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
T1,T2 = IL()
A1,A2 = IL()
B1,B2 = IL()
C1,C2 = B1-A1,B2-A2

if C1<0:
  C1,C2 = -C1,-C2

if T1*C1+T2*C2==0:
  print("infinity")
  exit()
elif T1*C1+T2*C2>0:
  print(0)
else:
  x = abs(T1*C1+T2*C2)
  print(T1*C1//x*2+1-int((T1*C1)%x==0))

Submission status: Accepted

Solution code:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    t1,t2 = map(int,ipt().split())
    a1,a2 = map(int,ipt().split())
    b1,b2 = map(int,ipt().split())
    d1 = t1*(a1-b1)
    d2 = t2*(a2-b2)
    if d1*(d1+d2) > 0:
        print(0)
    elif d1+d2 == 0:
        print("infinity")
    else:
        dt = d1+d2
        if abs(d1)%abs(dt):
            print(abs(d1)//abs(dt)*2+1)
        else:
            print(abs(d1)//abs(dt)*2)

    return None

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())
c1,c2=a1-b1,a2-b2
if t1*c1+t2*c2==0:
  print('infinity')
else:
  # t1+t2でどれだけ二人が離れるか。
  d=t1*c1+t2*c2
  if c2*d<0:
    print(0)
  elif abs(t2*c2)<abs(d):
    print(0)
  else:
    tmp=((t2*c2)//d)
    if (t2*c2)%d==0:
      print(2*tmp-2)
    else:
      print(2*tmp-1)

Submission status: Accepted

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

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

d1 = (a1 - b1) * t1
d2 = (b2 - a2) * t2
k = d1 // (d2 - d1)
if d1 % (d2 - d1) == 0:
    print(2 * k)
else:
    print(2 * k + 1)

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>b1:
    a1, a2, b1, b2 = b1, b2, a1, a2
init = (a1-b1)*t1+(a2-b2)*t2
if init<0:
    print(0)
    exit(0)
elif init==0:
    print('infinity')
    exit(0)
lim = (b1-a1)*t1
if lim%init==0:
    print(lim//init*2)
else:
    print(lim//init*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())

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 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
if a1*t1 + a2*t2 == b1*t1 + b2*t2:
    print("infinity")
else:
    A = a1*t1 + a2*t2
    B = b1*t1 + b2*t2
    M1,m1 = max(a1*t1,b1*t1),min(a1*t1,b1*t1)
    M2,m2 = max(A,B),min(A,B)
    gap1,gap2 = M1-m1,M2-m2
    Mv,mv = max(a1,b1),min(a1,b1)
    vgap = Mv-mv
    possible = vgap*t1
    count = possible//gap2
    if possible%gap2 == 0:
        pitta = 1
    else:
        pitta = 0
    if (a1*t1 > b1*t1 and A > B) or (a1*t1 < b1*t1 and A < B):
        print(0)
    elif (a1*t1 > b1*t1 and A < B) or (a1*t1 < b1*t1 and A > B):
        if pitta == 1:
            print(1+count*2-1)
        else:
            print(1+count*2)

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*d2>0:
    print(0)
    exit()

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

if d1+d2>0:
    print(0)
    exit()

if d1+d2==0:
    print('infinity')
    exit()

t = -d2-d1

if d1%t==0:
    print(d1//t*2)
else:
    print(d1//t*2+1)

Submission status: Accepted

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
INF = -1
def main(T1, T2, A1, A2, B1, B2):
    D1 = T1 * (A1 - B1)
    D2 = D1 + T2 * (A2 - B2)

    if D1 * D2 > 0:
        return 0

    if abs(D1) == abs(D2) or D2 == 0:
        return INF

    d, m = divmod(abs(D1), abs(D2))
    return (1 if m else 0) + 2 * d


if __name__ == '__main__':
    input = sys.stdin.readline
    T1, T2 = map(int, input().split())
    A1, A2 = map(int, input().split())
    B1, B2 = map(int, input().split())
    ans = main(T1, T2, A1, A2, B1, B2)
    print(ans if ans >= 0 else 'infinity')

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>B1 and A2>B2:
  print(0)
elif A1<B1 and A2<B2:
  print(0)
elif T1*A1+T2*A2==T1*B1+T2*B2:
  print('infinity')
elif A1>B1 and A2<B2:
  if T1*A1+T2*A2>T1*B1+T2*B2:
    print(0)
  else:
    se=abs(T1*A1+T2*A2-(T1*B1+T2*B2))
    ad=T1*A1-T1*B1
    d=-(-ad//se)
    ans=2*d-1
    if ad%se==0:
      ans+=1
    print(ans)
elif A1<B1 and A2>B2:
  if T1*A1+T2*A2<T1*B1+T2*B2:
    print(0)
  else:
    se=abs(T1*A1+T2*A2-(T1*B1+T2*B2))
    ad=T1*B1-T1*A1
    d=-(-ad//se)
    ans=2*d-1
    if ad%se==0:
      ans+=1
    print(ans)