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:
def main():
    t = list(map(int, input().split()))
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    circle = a[0] * t[0] + a[1] * t[1] - b[0] * t[0] - b[1] * t[1]
    if circle == 0:
        print('infinity')
        return
    d = [0, 0]
    cd = 0
    ans = -1
    for i in range(2):
        d[i] = cd + a[i] * t[i] - b[i] * t[i]
        if cd * d[i] <= 0:
            ans += 1
        cd = d[i]
    if ans == 0:
        print(0)
        return
    if circle < 0:
        if d[0] > 0:
            ans += d[0] // -circle * 2
            if d[0] % (-circle) == 0:
                ans -= 1
        elif d[1] > 0:
            ans += d[1] // -circle * 2
            if d[1] % (-circle) == 0:
                ans -= 1
    else:
        if d[0] < 0:
            ans += -d[0] // circle * 2
            if -d[0] % (circle) == 0:
                ans -= 1
        elif d[1] < 0:
            ans += -d[1] // circle * 2
            if -d[1] % (circle) == 0:
                ans -= 1
    print(ans)
main()

Submission status: Accepted

Solution code:
from math import ceil
t1,t2,a1,a2,b1,b2=map(int,open(0).read().split())
w,x,y,z=t1*a1,t2*a2,t1*b1,t2*b2
if w+x==y+z:
	print("infinity")
	exit()
if (w-y)*(w+x-y-z)>0:
	print(0)
	exit()
a1=min(w,y)
b1=max(w,y)
c1=max(w+x,y+z)
c2=min(w+x,y+z)
print(2*int((b1-a1)//(c1-c2))+int((b1-a1)%(c1-c2)>0))

Submission status: Accepted

Solution code:
import math

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

  sumA = t1 * a1 + t2 * a2
  sumB = t1 * b1 + t2 * b2

  if sumA == sumB:
    return 'infinity'
  if sumA < sumB:
    sumA, sumB = sumB, sumA
    a1, b1 = b1, a1
    a2, b2 = b2, a2
  # A の方が sum が大きいとする

  halfA = t1 * a1
  halfB = t1 * b1
  if halfA > halfB:
    return 0
  div, mod = divmod(halfB - halfA, sumA - sumB)
  if mod == 0:
    return div * 2
  return div * 2 + 1

print(main())

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

p1 = (b1 - a1) * t1
p2 = (b2 - a2) * t2

if p1 + p2 == 0:
    print('infinity')
    sys.exit()

if p1 + p2 < 0:
    p1 *= -1
    p2 *= -1

if p1 > 0:
    print(0)
    sys.exit()

ans = int((-1 * p1) / (p1+p2)) * 2 + 1

if (-1 * p1) % (p1+p2) == 0:
    ans -= 1

print(ans)

Submission status: Accepted

Solution code:
#F
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
l1 = (a1-b1)*t1
l2 = (a2-b2)*t2

    
if (l1+l2)==0:
    print('infinity')
elif (l1)*(l1+l2)>0:
    print(0)
else:
    zure = abs(l1+l2)
    oitsuki = abs(l1)
    n = oitsuki//zure
    if n * zure == oitsuki:
        print(2*n)
    else:
        print(2*n+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()))
x=(a1-b1)*t1 + (a2-b2)*t2
if x == 0:
    print("infinity")
elif x*(a1-b1) > 0:
    print(0)
else:
    s,t = divmod(abs((a1-b1)*t1), abs(x))
    if t:
        print(s*2+1)
    else:
        print(s*2)

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

al1=t1*a1
al2=t2*a2
bl1=t1*b1
bl2=t2*b2

if al1>bl1:
    l1=al1
    l2=al2
    l3=bl1
    l4=bl2
else:
    l1=bl1
    l2=bl2
    l3=al1
    l4=al2

if al1+al2==bl1+bl2:
    print("infinity")
else:
    if (l1-l3)%(l1+l2-l3-l4)==0:
        ans=max(0,2*math.floor((l1-l3)/(l3+l4-l1-l2)))
        print(ans)
    else:
        ans=max(0,2*math.floor((l1-l3)/(l3+l4-l1-l2))+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())
ans = 0
if T1*A1+T2*A2 == T1*B1+T2*B2:
    print("infinity")
    exit()
if A1>B1:
    jtA = True
else:
    jtA = False
if jtA:
    if T1*A1+T2*A2 > T1*B1+T2*B2:
        print(0)
        exit()
    else:
        jA = False
        x = abs(T1*B1+T2*B2 - T1*A1-T2*A2)
else:
    if T1*A1+T2*A2 < T1*B1+T2*B2:
        print(0)
        exit()
    else:
        jA = True
        x = abs(T1*A1+T2*A2 - T1*B1-T2*B2)

if (abs(A1-B1)*T1)%x == 0:
    ans += ((abs(A1-B1)*T1)//x)*2
else:
    ans += ((abs(A1-B1)*T1)//x)*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())

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

def p(x, y):
    if x%y == 0:
        return abs(x//y)
    else:
        return abs(x//y) + 1

if y == 0:
    ans = 'infinity'
else:
    if x%y != 0:
        if x < 0 and y > 0 :
            ans = (abs(x//y) + 1) * 2 - 3
        elif x > 0 and y < 0 :
            ans = (abs(x//y) + 1) * 2 - 3
        else:
            ans = 0
    else:
        if x < 0 and y > 0 :
            ans = (abs(x//y) + 1) * 2 - 2
        elif x > 0 and y < 0 :
            ans = (abs(x//y) + 1) * 2 - 2
        else:
            ans = 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())

a=a1-b1
b=a2-b2
#print(a,b)

ans=0

if(a*b>0):
    ans=-1
else:
    go=a*t1+b*t2
    if(go*a>0):
        ans=0
    elif(go==0):
        ans=-10
    else:
        kai=a*t1
        if(go<0):
            go*=-1
        if(kai<0):
            kai*=-1
        #print(kai//go)
        if(kai%go==0):
            ans=(kai//go)*2
        else:
            ans=(kai//go)*2+1

if(ans==-1):
    print(0)
elif(ans==-10):
    print("infinity")
else:
    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())

Adist1=T1*A1
Adist2=T2*A2
Bdist1=T1*B1
Bdist2=T2*B2

# 最初から同じ速度の場合
if Adist1==Bdist1:
  print("infinity")
# Aが先に行ってしまってBが絶対おいつけない場合
elif Adist1>Bdist1 and Adist1+Adist2>Bdist1+Bdist2:
  print(0)
# Bが先に行ってしまってAが絶対おいつけない場合
elif Adist1<Bdist1 and Adist1+Adist2<Bdist1+Bdist2:
  print(0)
# 無限に出会う場合
elif Adist1+Adist2==Bdist1+Bdist2:
  print("infinity")
# 追いついたり追いつかなかったりパターン(Aが先に行く)
elif Adist1>Bdist1 and Adist1+Adist2<Bdist1+Bdist2:
  diff=(Bdist1+Bdist2)-(Adist1+Adist2)
  print(2*((Adist1-Bdist1)//diff)+((Adist1-Bdist1)%diff!=0))
# 追いついたり追いつかなかったりパターン(Bが先に行く)
elif Adist1<Bdist1 and Adist1+Adist2>Bdist1+Bdist2:
  diff=(Adist1+Adist2)-(Bdist1+Bdist2)
  print(2*((Bdist1-Adist1)//diff)+((Bdist1-Adist1)%diff!=0))

Submission status: Accepted

Solution code:
def f():
    t0, t1 = [int(s) for s in input().split()]
    a = [int(s) for s in input().split()]
    b = [int(s) for s in input().split()]
    sa = t0*a[0] + t1*a[1]
    sb = t0*b[0] + t1*b[1]
    if sa == sb:
        return 'infinity'
    if sa > sb:
        a, b = b,a
        sa, sb = sb, sa
    # sa < sb
    if b[0] > a[0]:
        return 0

    delta = sb - sa

    ans = 1
    d0 = (a[0]-b[0])*t0
    if d0%delta == 0:
        ans += 1
    count = (d0-1)//delta
    ans += 2 * count
    return ans


print(f())

Submission status: Accepted

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:
        if d1 < 0:
            d1 *= -1
            d2 *= -1
        if d1 + d2 > 0:
            print(0)
        elif d1 % (d1+d2) == 0:
            print(d1 // abs(d1+d2) * 2)
        else:
            print(d1 // abs(d1+d2) * 2 + 1)




main()

Submission status: Accepted

Solution code:
import math
T_1, T_2 = map(int, input().split())
A_1, A_2 = map(int, input().split())
B_1, B_2 = map(int, input().split())
ans = 0
if T_1 * A_1 + T_2 * A_2 == T_1 * B_1 + T_2 * B_2:
    print("infinity")
else:
    # 速いほうをＡと仮定
    if T_1 * A_1 + T_2 * A_2 < T_1 * B_1 + T_2 * B_2:
        A_1, A_2, B_1, B_2 = B_1, B_2, A_1, A_2
    if A_1 < B_1:
        sa_12 = T_1 * A_1 + T_2 * A_2 - (T_1 * B_1 + T_2 * B_2)  # 1サイクルでできる差
        sa_1 = -T_1 * A_1 + T_1 * B_1  # T_1でできる差
        if sa_1 % sa_12 == 0:
            ans = int((sa_1 / sa_12)*2)
        else:
            kari = math.ceil(sa_1 / sa_12)
            ans = (kari-1)*2+1
    print(ans)

Submission status: Accepted

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

c = [t[0]*a[0],t[1]*a[1]]
d = [t[0]*b[0],t[1]*b[1]]

if sum(c)==sum(d):
    print('infinity')
    exit()
elif c[0]==d[0]:
    print('infinity')
    exit()

if sum(d)>sum(c) and d[0]>c[0]:
    print(0)
    exit()
elif sum(c)>sum(d) and c[0]>d[0]:
    print(0)
    exit()
else:
    n = (c[0]-d[0])/(sum(d)-sum(c))
    if n==int(n):
        print(2*int(n))
    else:
        print(2*int(n)+1)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    t1,t2=MI()
    a1,a2=MI()
    b1,b2=MI()
    if a1<b1:a1,a2,b1,b2=b1,b2,a1,a2
    if a1*t1+a2*t2==b1*t1+b2*t2:
        print("infinity")
        exit()
    if a1*t1+a2*t2>b1*t1+b2*t2:
        print(0)
        exit()
    x=a1*t1-b1*t1
    y=b1*t1+b2*t2-(a1*t1+a2*t2)
    ans=(x//y)*2
    if x%y:
        ans+=1
    print(ans)


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<b1 and a2<b2) or (a1>b1 and a2>b2):
    print(0)
    exit()

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

_b1 = b1 - a1
_a2 = a2 - b2

v1 = t1*_b1
v2 = t2*_a2

if v1 == v2:
    print('infinity')
    exit()

if v1 > v2:
    print(0)
    exit()

d = v2 - v1
k = v1 // d

ans = k*2 + 1
if v2*k == v1*(k+1):
    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())
C1=A1*T1
D1=A2*T2
C2=B1*T1
D2=B2*T2
if (A1-B1)*(A2-B2)>0:
  print(0)
else:
  C=C1-C2
  D=D1-D2
  if C<0:
    C=-C
    D=-D
  E=C+D
  if E>0:
    print(0)
  elif E==0:
    print("infinity")
  else:
    k=C//int(-E)
    if k==C/int(-E):
      print(2*k)
    else:
      print(2*k+1)

Submission status: Accepted

Solution code:
def meet(faster, slower):
  if slower[0] - faster[0] < 0:
    print(0)
  else:
    count = (slower[0] - faster[0]) // (sum(faster) - sum(slower))
    if (slower[0] - faster[0]) % (sum(faster) - sum(slower)) == 0:
      print(count * 2)
    else:
      print(count * 2 + 1)

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

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

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

Submission status: Accepted

Solution code:
# -*- encoding: utf-8 -*-
import sys
import re
import random
import numpy as np
from math import sqrt,log
from fractions import gcd  # python<=3.4
from collections import Counter, defaultdict

# st = Counter(raw_input())

MOD=1000000007

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


def read_ints():
  return map(int, input().split())


def calc_kmax(az,bz):
    assert az < bz

    lo = 0    # ng
    hi = az # ok
    while lo+1 < hi:
        k = (lo + hi)//2
        if k * bz >= (k+1) * az:
            hi = k
        else:
            lo = k
    return hi


def solve(T1,T2,A1,A2,B1,B2):
    T = T1 + T2
    az = T1*A1 + T2*A2
    bz = T1*B1 + T2*B2
    if az == bz:
        print('infinity')
        return False
    if az > bz:
        A1,A2,B1,B2 = B1,B2,A1,A2
        az,bz = bz,az

    numer = T1*(A1-B1)
    denom = (bz-az)

    g = numer / denom
    gi = int(g)
    gr = gi - g

    if (g > 0):
        if gr == 0:
            ans = -1 + gi*2 + 1
        else:
            ans = -1 + (gi+1)*2
    else:
        ans = 0

    print(ans)


T1,T2 = read_ints()
A1,A2 = read_ints()
B1,B2 = read_ints()
solve(T1,T2, A1,A2, B1,B2)

Submission status: Accepted

Solution code:
import numpy as np

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

S1 = (A1 - B1) * T1
S2 = (A2 - B2) * T2

if (S1 + S2) == 0:
    print('infinity')
elif np.sign(S1) == np.sign(S2):
    print(0)
elif abs(S1) > abs(S2):
    print(0)
else:
    z = 1 + ((abs(S1)) // abs(S1 + S2)) * 2
    if (abs(S1)) % abs(S1 + S2) == 0:
        z = z -1
    print(z)

Submission status: Accepted

Solution code:
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
v1 = [a1,b1]
d = a1*t1 + a2*t2 - b1*t1 - b2*t2
if d == 0:
  print("infinity")
  exit()
if d > 0:
  f = 0
  s = 1
else:
  f = 1
  s = 0
  d = -d
l = t1*(v1[s]-v1[f])
if l < 0:
  print(0)
  exit()
tm = int(l//d)
if int(l%d) == 0:
  print(2*tm)
else:
  print(1+2*tm)

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

sa = A1 * T1
sb = B1 * T1
ea = A2 * T2
eb = B2 * T2
fa = sa + ea
fb = sb + eb

if fa < fb:
  if sa < sb:
    print(0)
  elif sa == sb:
    print(1)
  elif sa > sb:
    d1 = sa - sb
    di = fb - fa
    c = int(d1 / di) + math.ceil(d1 / di)#  + 1
    print(c)
elif fa == fb:
  print("infinity")
elif fa > fb:
  if sa < sb:
    d1 = sb - sa
    di = fa - fb
    c = int(d1 / di) + math.ceil(d1 / di)# + 1
    print(c)
  elif sa == sb:
    print(1)
  elif sa > sb:
    print(0)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from math import ceil

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

# 交互になる
if (b1 - a1) * t1 + (b2 - a2) * t2 == 0:
    print('infinity')

# 高橋君が先を行く
elif (b1 - a1) * t1 + (b2 - a2) * t2 < 0:
    # ずっと高橋君の方が速い
    if (b1 - a1) * t1 < 0 and (b2 - a2) * t2 < 0:
        print(0)
    # T2分間は追いつかれる
    elif (b1 - a1) * t1 < 0 and (b2 - a2) * t2 > 0:
        print(0)
    # T1分間は追いつかれる
    else:
        cross = ceil((b1-a1)*t1 / ((a1-b1)*t1 + (a2-b2)*t2)) - 1
        #print(((b1-a1)*t1 + (b2-a2)*t2) * (cross+1) + (a1-b1)*t1)
        if ((a1-b1)*t1 + (a2-b2)*t2) * (cross+1) + (a1-b1)*t1 == 0:
            print(2* (cross+ 1))
        else:
            print(2 * cross + 1)

# 高橋君が遅れる
else:
    # ずっと高橋君のほうが遅い
    if (b1 - a1) * t1 > 0 and (b2 - a2) * t2 > 0:
        print(0)
    # T2分間は追い上げる
    elif (b1 - a1) * t1 > 0 and (b2 - a2) * t2 < 0:
        print(0)
    # T1分間は追い上げる
    else:
        cross = ceil((b1-a1)*t1 / ((a1-b1)*t1 + (a2-b2)*t2)) - 1
        #print(((a1-b1)*t1 + (a2-b2)*t2) * (cross+1) + (a1-b1)*t1)
        if ((a1-b1)*t1 + (a2-b2)*t2) * (cross+1) + (a1-b1)*t1 == 0:
            print(2 * (cross + 1))
        else:
            print(2 * cross + 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 * t1
c2 = a2 * t2
d1 = b1 * t1
d2 = b2 * t2

if c1 + c2 == d1 + d2:
    print("infinity")
    exit(0)

if c1 + c2 < d1 + d2:
    c1, d1 = d1, c1
    c2, d2 = d2, c2

# print(c1, c2, d1, d2)

def check(mid):
    if mid % 2 == 0:
        a = (c1 + c2) * (mid // 2)
        b = (d1 + d2) * (mid // 2)
        f1 = a < b
        a += c1
        b += d1
        if a == b:
            return True
        f2 = a < b
        return f1 != f2
    else:
        a = (c1 + c2) * (mid // 2) + c1
        b = (d1 + d2) * (mid // 2) + d1
        f1 = a < b
        a += c2
        b += d2
        if a == b:
            return True
        f2 = a < b
        return f1 != f2

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

print(ok)

Submission status: Accepted

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

delta1 = A1 - B1
delta2 = A2 - B2

d = delta1 * T1 + delta2 * T2
T = T1 + T2

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

if delta1 * delta2 > 0:
    print(0)
    exit()

if delta1 * d > 0:
    print(0)
    exit()

n = int(delta1 * T1 // d) * (-1)
ans = 2 * n - 1
if delta1 * T1 == n * d * (-1):
    ans += 1

print(max(ans, 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 and a2 > b2) or (a1 < b1 and a2 < b2):
  print(0)
else:
  if a1 * t1 + a2 * t2 == b1 * t1 + b2 * t2:
    print("infinity")
  else:
    d_all = (a1 * t1 + a2 * t2) - (b1 * t1 + b2 * t2)
    d_1 = a1 * t1 - b1 * t1
    if (d_all > 0 and d_1 > 0) or (d_all < 0 and d_1 < 0):
      print(0)
    else:
      if abs(d_all) > abs(d_1):
        print(1)
      else:
        ans = 1
        ans += abs(d_1) // abs(d_all) * 2 - (1 if d_1 % d_all == 0 else 0)
        print(ans)

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
a2 -= b2

if t1*a1 + t2*a2 == 0:
    print("infinity")
    exit()
if a1*a2 > 0:
    print(0)
    exit()
a1 *= t1
a2 *= t2
if (a1 > 0 and a1+a2>0) or (a1<0 and a1+a2<0):
    print(0)
    exit()

if a1 > 0:
    x = -a1/(a1+a2)
    n = ceil(x)
    if n == x:
        print(2*n)
    else:
        print(2*n-1)
if a2 > 0:
    x = a1 / (-(a1 + a2))
    n = ceil(x)
    if n == int(x):
        print(2 * n)
    else:
        print(2 * n - 1)

Submission status: Accepted

Solution code:
import math

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

c = [t[0]*a[0],t[1]*a[1]]
d = [t[0]*b[0],t[1]*b[1]]

if sum(c)==sum(d):
    print('infinity')
    exit()
elif c[0]==d[0]:
    print('infinity')
    exit()

if sum(d)>sum(c) and d[0]>c[0]:
    print(0)
    exit()
elif sum(c)>sum(d) and c[0]>d[0]:
    print(0)
    exit()



if sum(d)>sum(c):
    if d[0]>c[0]:
        print(0)
        exit()
    elif d[0]<c[0]:
        n = (c[0]-d[0])/(sum(d)-sum(c))
        if n==int(n):
            print(2*int(n))
        else:
            print(2*math.ceil(n)-1)
elif sum(c)>sum(d):
    if c[0]>d[0]:
        print(0)
        exit()
    elif c[0]<d[0]:
        n = (c[0]-d[0])/(sum(d)-sum(c))
        if n==int(n):
            print(2*int(n))
        else:
            print(2*math.ceil(n)-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 * A1
a2 = T2 * A2
a = a1 + a2
b1 = T1 * B1
b2 = T2 * B2
b = b1 + b2


def bis(p, ok, ng):
    mid = (ok + ng) // 2
    return (
        ok if abs(ok - ng) == 1 else
        bis(p, mid, ng) if p(mid) else
        bis(p, ok, mid)
    )


def p(k):
    x = k * a
    y = k * b
    # print(f"k: {k}, x: {x}, y: {y}")
    #print(f"k: {k}, x+: {x + a1}, y+: {y + b1}")
    if x < y:
        return x + a1 >= y + b1
    else:
        return y + b1 >= x + a1


def p2(k):
    x = k * a + a1
    y = k * b + b1
    if x < y:
        return x + a2 >= y + b2
    elif x > y:
        return y + b2 >= x + a2
    else:
        return False

if a == b:
    ans = "infinity"
else:
    # print(bis(p, -1, 10**16))
    # print(bis(p2, -1, 10**16))
    c = 1 if (a1 < b1 and a > b) or (b1 < a1 and b > a) else 0
    # k = bis(lambda k: k * a + a1 >= k * b + b1, 0, 10**16) if a < b else bis(lambda k: k * a + a1 <= k * b + b1, 0, 10**16)
    # d = if k * a + a1 == k * b + b1
    ans = bis(p, 0, 10**16) + bis(p2, 0, 10**16) + c

print(ans)

# print('-------')

# x = 0
# y = 0
# for i in range(114):
#     x += a1
#     y += b1
#     #print(x, y)
#     print(f"{i} {x < y}")
#     x += a2
#     y += b2
#     print(f"{i} {x < y}")
#     #print(x, y)

Submission status: Accepted

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

if a1*t1 + a2*t2 == b1*t1 + b2*t2:
    ans = -1
else:
    if a1 >= b1 and  a2 >= b2:
        ans = 0
    elif a1 <= b1 and  a2 <= b2:
        ans = 0
    else:
        if a1*t1 + a2*t2 < b1*t1 + b2*t2:
            a1, b1 = b1, a1
            a2, b2 = b2, a2

        mind = a1*t1 - b1*t1
        if mind > 0:
            ans = 0
        else:
            lastd = a1*t1 + a2*t2 - b1*t1 - b2*t2
            mind = abs(mind)
            if mind % lastd == 0:
                ans = 2 * (mind // lastd) 
            else:
                ans = 2 * (mind // lastd) + 1
    
print(ans if ans != -1 else "infinity")

Submission status: Accepted

Solution code:

#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    t1, t2 = LI()
    a1, a2 = LI()
    b1, b2 = LI()
    if t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
        print("infinity")
        return
    if t1 * a1 > t1 * b1:
        if t1 * a1 + t2 * a2 > t1 * b1 + t2 * b2:
            print(0)
            return
        else:
            x = t1 * (a1 - b1) / (-t1 * (a1 - b1) + t2 * (b2 - a2))
            if x.is_integer():
                print(2 * (t1 * (a1 - b1) // (-t1 * (a1 - b1) + t2 * (b2 - a2))))
            else:
                print(2 * (t1 * (a1 - b1) // (-t1 * (a1 - b1) + t2 * (b2 - a2))) + 1)
            return
    elif t1 * a1 < t1 * b1:
        if t1 * a1 + t2 * a2 < t1 * b1 + t2 * b2:
            print(0)
            return
        else:
            x = t1 * (b1 - a1) / (-t1 * (b1 - a1) + t2 * (a2 - b2))
            if x.is_integer():
                print(2 * (t1 * (b1 - a1) // (-t1 * (b1 - a1) + t2 * (a2 - b2))))
            else:
                print(2 * (t1 * (b1 - a1) // (-t1 * (b1 - a1) + t2 * (a2 - b2))) + 1)
            return
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

X1 = T1 * (A1 - B1)
X2 = T2 * (A2 - B2)

D = X1 + X2
if D == 0:
	print("infinity")
else:
	if X1 < 0:
		X1, X2 = [-X1, -X2]
	else:
		D = -D
	
	if D < 0:
		print("0")
	else:
		if X1 % D == 0:
			print((X1 // D) * 2)
		else:
			print((X1 // D) * 2 + 1)

Submission status: Accepted

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

    # aの方がt1のときに先行している
    if a1 < b1:
        a1, b1 = b1, a1
        a2, b2 = b2, a2

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

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

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

    if move==0:
        return -1

    if move*mid > 0:
        return 0

    if move < 0:
        move *= -1
    else:
        mid *= -1

    if mid%move == 0:
        return 2*(mid//move)
    else:
        return 2*(mid//move)+1

if __name__ == "__main__":
    ans = main()
    if ans==-1:
        print("infinity")
    else:
        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())

if A1 > B1:
    F1, F2 = A1, A2
    S1, S2 = B1, B2
else:
    F1, F2 = B1, B2
    S1, S2 = A1, A2

oi = F1 * T1 - S1 * T1
nuki = S2 * T2 - F2 * T2 - oi

if nuki == 0:
    print("infinity")
elif nuki > 0:
    enc = oi // nuki * 2
    if oi % nuki != 0:
        enc = enc + 1
    print(enc)
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())
if A1 < B1:
    A1, A2, B1, B2 = B1, B2, A1, A2

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

if (A1-B1)*T1 > (B2-A2)*T2:
    print(0)
    exit()

d = abs(A1*T1+A2*T2 - B1*T1-B2*T2)  # T1,T2が終わった後に開く距離
L = abs(A1-B1)*T1  # T1の間に開く距離
# print(d, L)

if L % d == 0:
    print(L//d*2)
else:
    ans = L//d*2+1
    print(ans)
    # if d*L//d <= L:
    #     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())
import math
f = (a1-b1)*t1
s = (a2-b2)*t2
if f == -s:
  print("infinity")
elif (f>0 and f+s>0) or (f<0 and f+s<0):
  print(0)
elif f//(-(f+s)) == math.ceil(f/(-(f+s))):
  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())
x,y=t1*(a1-b1),t2*(a2-b2)
if (x+y)==0:
  print("infinity")
elif x*(x+y)>0:
  print(0)
else:
  if x>0:
    x1,x2=x,x+y
  else:
    x1,x2=-x,-(x+y)
  if x1%x2==0:
    print(2*abs(x1//x2))
  else:
    print(2*abs(x1//x2)-1)

Submission status: Accepted

Solution code:
def main():
    t1, t2 = map(int, raw_input().split())
    a1, a2 = map(int, raw_input().split())
    b1, b2 = map(int, raw_input().split())
    ax1 = t1 * a1
    bx1 = t1 * b1
    ax2 = t2 * a2
    bx2 = t2 * b2
    if ax1 + ax2 == bx1 + bx2:
        print 'infinity'
        return
    if ax1 + ax2 < bx1 + bx2:
        ax1, bx1 = bx1, ax1
        ax2, bx2 = bx2, ax2
    init_d = bx1 - ax1
    if init_d < 0:
        print 0
        return
    total_delta = ax1 + ax2 - bx1 - bx2
    ans = 1 + (init_d / total_delta) * 2
    if init_d % total_delta == 0:
        ans -= 1
    print ans

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


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

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

# print (bunshi)
# print (bunbo)

n = 1 + bunshi // bunbo

if bunshi % bunbo == 0:
    print (int(n) * 2 - 2)
else:
    print (int(n) * 2 - 1)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
t=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
at1=a[0]*t[0]
at2=a[1]*t[1]
bt1=b[0]*t[0]
bt2=b[1]*t[1]
if at1+at2==bt1+bt2:
    print("infinity")
    quit()

if at1>bt1:
    if at1+at2>bt1+bt2:
        print(0)
        quit()
    sa1=at1-bt1
    sa2=bt1+bt2-at1-at2
    # print(sa1//sa2*2+1)
else:
    if bt1+bt2>at1+at2:
        print(0)
        quit()
    sa1=bt1-at1
    sa2=-(bt1+bt2-at1-at2)
    # print(sa1//sa2*2+1)
if sa1%sa2==0:
    print(sa1//sa2*2)
else:
    print(sa1//sa2*2+1)

Submission status: Accepted

Solution code:
def main():
    import sys
    import math
    s,t=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=a[0]-b[0]
    q=a[1]-b[1]
    x,y=p*s,q*t

    if q>0 or x+y>0:
        print(0)
        sys.exit()
    elif x+y==0:
        print('infinity')
        sys.exit()


    y=abs(y)
    if x%(y-x)==0: ans=int(x/(y-x))*2
    else: ans=math.floor(x/(y-x))*2+1
    print(ans)


 
if __name__ == '__main__':
    main()