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:
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split()) #高橋君
B1,B2 = map(int,input().split()) #青木君

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

if D1*D2 > 0:
    print(0)
elif D2 == 0:
    print("infinity")
else:
    if -D1%D2 == 0:
        print(-D1//D2*2)
    else:
        print(-D1//D2*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()) #青木君

# 時間配列
T = [T1]
f = 1
for i in range(3):
    if f:
        Tn = T2
        f = 0
    else:
        Tn = T1
        f = 1
    T.append(T[-1]+Tn)
# print(T)

class runner():
    def __init__(self,v1,v2):
        self.v1 = v1
        self.v2 = v2
        
    def distance(self,t):
        d = t//(T1+T2)*(self.v1*T1 + self.v2*T2)
        tm = t % (T1+T2)
        # print("d",d,"tm",tm)
        if tm < T1:
            d += self.v1 * tm
        else:
            d += self.v2*(tm-T1) + self.v1*T1
        return d

dif = runner(A1-B1,A2-B2)

c = -1
D = 0
DL = [0]
for t in T:
    pD = D
    D = dif.distance(t)
    DL.append(D)
    # print(D)
    if pD*D <= 0:
        c += 1
# print(c)
# print(DL)

if DL[1]*DL[2] > 0:
    print(0)
else:
    if DL[2] == 0:
        print("infinity")
    else:
        a = DL[2]
        if a>0:
            if -DL[1]%a == 0:
                print(-DL[1]//a*2)
            else:
                print(-DL[1]//a*2+1)
        else:
            if -DL[1]%a == 0:
                print(-DL[1]//a*2)
            else:
                print(-DL[1]//a*2+1)

Submission status: Accepted

Solution code:
t0, t1 = map(int, input().split())
a0, a1 = map(int, input().split())
b0, b1 = map(int, input().split())
d0 = (a0 - b0) * t0
d1 = (a1 - b1) * t1
if d0 < 0:
    d0, d1 = -d0, -d1
if d0 + d1 == 0:
    print('infinity')
    exit()
if d0 + d1 > 0:
    print(0)
    exit()
d = -(d0 + d1)
q, r = d0 // d, d0 % d
print(2 * q + min(r, 1))

Submission status: Accepted

Solution code:
t0, t1 = map(int, input().split())
a0, a1 = map(int, input().split())
b0, b1 = map(int, input().split())
d0 = (a0 - b0) * t0
d1 = (a1 - b1) * t1
if d0 < 0:
    d0, d1 = -d0, -d1
if d0 + d1 == 0:
    print('infinity')
    exit()
if d0 + d1 > 0:
    print(0)
    exit()
q, r = d0 // (-(d0+d1)), d0 % (-(d0+d1))
if r == 0:
    print(2 * q)
else:
    print(2 * q + 1)

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())
# まず大きい方にswap
if a1 <= b1:
    a1,b1 = b1,a1
    a2,b2 = b2,a2

# t1での距離
x1 = a1*t1 - b1*t1
# t2での距離
x2 = x1 + a2*t2 - b2*t2
if x2 == 0:
    print("infinity")
elif x2 > 0:
    print(0)
else:
    ans =  int(np.ceil(x1/abs(x2))*2)
    if x1 % abs(x2) != 0:
        ans -= 1
    print(ans)

Submission status: Accepted

Solution code:
#三井住友2019_F
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 < b1 and a2 > b2:
        a1, b1 = b1, a1
        a2, b2 = b2, a2
        
    if t1 * (a1 - b1) > t2 * (b2 - a2):
        print(0)
    elif t1 * (a1 - b1) == t2 * (b2 - a2):
        print('infinity')
    else:
        d = t2 * (b2 - a2) - t1 * (a1 - b1)
        y = t1 * (a1 - b1)
        
        if y % d == 0:
            print(y // d * 2)
        else:
            print(y // d * 2 + 1)

Submission status: Accepted

Solution code:
def solve(mid):
    pos = (d[0] + d[1]) * (mid // 2)
    if mid % 2 == 1:
        pos += d[0]

    if mid % 2 == 1:
        nxt_pos = pos + d[1]
    else:
        nxt_pos = pos + d[0]

    if pos * nxt_pos < 0 or nxt_pos == 0:
        return True
    else:
        return False

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

v0 = a[0] - b[0]
v1 = a[1] - b[1]
d = [t[0] * v0, t[1] * v1]

if d[0] == -d[1]:
    print("infinity")
    exit()
    
ok = 0
ng = 10 ** 60
while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    if solve(mid):
        ok = mid
    else:
        ng = mid
print(ok)

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 *= -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:
#!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
    A = t[0]*a[0]+t[1]*a[1]
    k = t[0]*a[0]
    l = 0
    r = 10**20
    while r-l > 1:
        x = (l+r) >> 1
        if A*x+k > 0:
            l = x
        else:
            r = x
    ans = 2*l+1
    if A*r+k == 0:
        ans += 1
    print(ans)
    return

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

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


if abs(d1) == abs(d2):
    print("infinity")
    exit()

if abs(d1) > abs(d2) or d1*d2>0:
    print(0)
    exit()

dif = abs(d2) - abs(d1)

ans=2*(abs(d1)//dif)
if abs(d1)%dif != 0:
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
TAB = [list(map(int, input().split())) for _ in range(3)]
T = TAB[0]
A = TAB[1]
B = TAB[2]
dV1, dV2 = A[0] - B[0], A[1] - B[1]

dL1, dL2 = dV1 * T[0], dV2 * T[1]
if dL1 + dL2 == 0:
  print("infinity")
else:
  if dL1 * (dL1 + dL2) > 0:
    print(0)
  else:
    m = dL2 * T[0] - dL1 * T[1]
    n = dL1 + dL2
    m += n * T[1]
    cnt = m // (n * (T[0] + T[1]))
    if m % (n * (T[0] + T[1])) == 0:
      print(2*cnt-2)
    else:
      print(2*cnt-1)

Submission status: Accepted

Solution code:
"""from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd"""

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

A1*=T1
A2*=T2
B1*=T1
B2*=T2

if A1+A2==B1+B2:
    print("infinity")
else:
    if A1+A2<B1+B2:
        A1,B1=B1,A1
        A2,B2=B2,A2
    if A1>B1:
        print(0)
    else:
        print(((B1-A1)//(A1+A2-(B1+B2)))*2 +1-(((B1-A1)%(A1+A2-(B1+B2)))==0)*1)

Submission status: Accepted

Solution code:
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
p1 = t1*a1
q1 = t1*b1
p2 = p1+t2*a2
q2 = q1+t2*b2
if p2 == q2:
  print("infinity")
  exit()
if (p1>q1 and p2>q2) or (p1<q1 and p2<q2):
  print(0)
  exit()
x = abs(p1-q1)
y = abs(q2-p2)
if x%y == 0:
  ans = (x//y)*2
else:
  ans = (x//y)*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())
v1, v2 = A1 - B1, A2 - B2
d1, d2 = v1 * T1, v2 * T2
if d1 > 0:
    d1 *= -1
    d2 *= -1

if d1 + d2 < 0:
    print(0)
elif d1 + d2 ==  0:
    print("infinity")
elif d1 + d2 > 0:
    s, t = divmod(-d1, d1+d2)
    print(2*s+(t!=0))

Submission status: Accepted

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
v1, v2 = A1 - B1, A2 - B2
d1, d2 = v1 * T1, v2 * T2
if d1 > 0:
    d1 *= -1
    d2 *= -1

if d1 + d2 < 0:
    print(0)
elif d1 + d2 ==  0:
    print("infinity")
elif d1 + d2 > 0:
    s, t = divmod(-d1, d1+d2)
    ans = 2*s + (t!=0)
    print(ans)

Submission status: Accepted

Solution code:
T1,T2,A1,A2,B1,B2=map(int, open(0).read().split())
C1,C2=A1-B1,A2-B2
if C1<0: C1,C2=-C1,-C2
Y1=C1*T1
Y2=Y1+C2*T2
if Y2>0:
    print(0)
elif Y2==0:
    print("infinity")
else:
    print(1+Y1//(-Y2)*2-(Y1%(-Y2)==0))

Submission status: Accepted

Solution code:
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
c1 = t1*(a1-b1)
c2 = t2*(a2-b2)
d = c1 + c2
if d == 0:
  print("infinity")
elif c1*c2 > 0 or c1*d > 0:
  print(0)
else:
  if abs(c1)%abs(d)==0:
    print((abs(c1)//abs(d))*2)
  else:
    print((abs(c1)//abs(d))*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()))

cycle = t[0] * (a[0] - b[0]) + t[1] * (a[1] - b[1])

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

after_t1 = t[0] * (a[0] - b[0])

if(cycle * after_t1 > 0):
    print(0)
    exit()

after_t1 = abs(after_t1)
cycle = abs(cycle)

ans = 1 + 2 * (after_t1//cycle) - (after_t1 % cycle == 0) * 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 (a1>b1 and a2>b2) or (b1>a1 and b2>a2):
  print(0)
elif abs(a1-b1)*t1>abs(a2-b2)*t2:
  print(0)
elif abs(a1-b1)*t1==abs(a2-b2)*t2:
  print('infinity')
else:
  d1=abs(a1-b1)*t1
  d2=abs(a2-b2)*t2
  l=0
  r=10**18+1
  while r-l!=1:
    mid=(l+r)//2
    if d1*(mid+1)-d2*mid<0:
      r=mid
    else:
      l=mid
  if d1*(l+1)-d2*l==0:
    print(2*l)
  else:
    print(2*l+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 t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
    print("infinity")
    exit(0)
if t1 * a1 + t2 * a2 > t1 * b1 + t2 * b2:
    a1, b1 = b1, a1
    a2, b2 = b2, a2
if a1 < b1:
    print("0")
    exit(0)

l, r, labe, ans = 2, 10**18, False, 10**18

while l <= r:
    m = l + (r - l) // 2
    xa = (m - 1) * (t1 * a1 + t2 * a2) + t1 * a1
    xb = (m - 1) * (t1 * b1 + t2 * b2) + t1 * b1
    if xa == xb:
        labe = True
        ans = m
        break;
    elif xa < xb:
        ans = m
        r = m - 1
    else :
        l = m + 1

if labe:
    print(2 * (ans - 1))
else :
    print(2 * (ans - 1) - 1)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

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

if A1 > B1 and A2 > B2:
    print(0)
    exit()
if A1 < B1 and A2 < B2:
    print(0)
    exit()

if A1 > B1:
    t = (A1-B1)*T1 / (B2-A2)
    delta = (A1-B1)*T1+(A2-B2)*T2
    if t == T2:
        print('infinity')
        exit()
    elif t > T2:
        print(0)
        exit()
    else:
        if delta > 0:
            print(math.floor((T2-t)*(B2-A2)/delta) + 1)
            exit()
        else:
            x = (A1-B1)*T1 / (-delta)
            if int(x) == x:
                print(2*int(x))
            else:
                print(2*math.floor(x)+1)

else:
    t = (B1-A1)*T1 / (A2-B2)
    delta = (B1-A1)*T1+(B2-A2)*T2
    if t == T2:
        print('infinity')
        exit()
    elif t > T2:
        print(0)
        exit()
    else:
        if delta > 0:
            print(math.floor((T2-t)*(A2-B2)/delta) + 1)
            exit()
        else:
            x = (B1-A1)*T1 / (-delta)
            if int(x) == x:
                print(2*int(x))
            else:
                print(2*math.floor(x)+1)

Submission status: Accepted

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

def main():
    T1,T2 = map(int,input().split())
    A1,A2 = map(int,input().split())
    B1,B2 = map(int,input().split())
    l = (A1-B1)*T1 + (A2-B2)*T2
    if l == 0:
        print("infinity")
    elif l > 0:
        if A1 > B1:
            print(0)
        else:
            d = (B1-A1)*T1
            if d%l == 0:
                ans = 2*(d//l)
            else:
                ans = 1+2*(d//l)
            print(ans)
    else:
        l *= -1
        if B1 > A1:
            print(0)
        else:
            d = (A1-B1)*T1
            if d%l == 0:
                ans = 2*(d//l)
            else:
                ans = 1+2*(d//l)
            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())

if A1 - B1 > 0:
  A1, B1 = B1, A1
  A2, B2 = B2, A2
  
d = (A1 - B1) * T1 + (A2 - B2) * T2

if d > 0:
  if ((B1 - A1) * T1) % d == 0:
    print(((B1 - A1) * T1 // d) *  2)
  else:
    print(((B1 - A1) * T1 // d) *  2 + 1)
elif d < 0:
  print(0)
else:
  print("infinity")

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

if C1 > 0:
    if C1*T1+C2*T2 > 0:
        ans = 0
    elif C1*T1+C2*T2 == 0:
        ans = 'infinity'
    else:
        d = abs(C1*T1+C2*T2)
        if C1*T1%d==0:
            ans = 2*C1*T1//d
        else:
            ans = 2*(C1*T1//d)+1
else:
    if C1*T1+C2*T2 < 0:
        ans = 0
    elif C1*T1+C2*T2 == 0:
        ans = 'infinity'
    else:
        d = abs(C1*T1+C2*T2)
        dd = abs(C1*T1)
        if dd%d==0:
            ans = 2*dd//d
        else:
            ans = 2*(dd//d)+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())
dif = T1*A1+T2*A2-T1*B1-T2*B2
if dif==0:
    print("infinity")
elif dif>0:
    if B1>A1:
        k = int(((B1-A1)*T1)/dif)
        if abs(((B1-A1)*T1)/dif-round(((B1-A1)*T1)/dif))<1e-20:
            print(2*round(((B1-A1)*T1)/dif))
        else:
            print(2*k+1)
    else:
        print(0)
else:
    if A1>B1:
        k = int(((A1-B1)*T1)/abs(dif))
        if abs((A1-B1)*T1/abs(dif)-round(((A1-B1)*T1)/abs(dif)))<1e-20:
            print(2*round(((A1-B1)*T1)/abs(dif)))
        else:
            print(2*k+1)
    else:
        print(0)

Submission status: Accepted

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
# https://atcoder.jp/contests/abc145/tasks/abc145_e
# E - All-you-can-eat
"""
DP1[i][j] =the maximum sum of deliciousness of dishes, which are chosen from 1st- to i-th dishes,
such that he can finish them in j minutes

still WA!!!

"""
t1 = gi()
t2 = gi()
a1 = gi()
a2 = gi()
b1 = gi()
b2 = gi()

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

#print(a1, a2, b1, b2)

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

net = d1 + d2


def run():
    if d1 == 0 or net == 0:
        print('infinity')
        return
    elif net > 0:
        print(0)
        return

    d = abs(net)

    ans = 0
    p2 = (d1 // d)
    if (d1 % d == 0):
        ans = 2 * p2
    else:
        ans = 2 * p2 + 1

    print(ans)


run()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    t1,t2 = LI()
    a1,a2 = LI()
    b1,b2 = LI()

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

    sa = t1*a1 + t2*a2
    sb = t1*b1 + t2*b2

    if sa == sb:
        return "infinity"

    if sb > sa:
        return 0

    t = t1*b1 - t1*a1
    st = sa - sb
    if t % st == 0:
        r = t // st * 2
    else:
        r = t // st * 2 + 1

    return r


print(main())

Submission status: Accepted

Solution code:
import sys

#  Input
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
runout = T1 * A1 - T1 * B1
TotalDiff = (T1 * A1 - T1 * B1) + (T2 * A2 - T2 * B2)

if TotalDiff == 0:
    print('infinity')
    sys.exit()
elif runout * TotalDiff > 0:
    print(0)
    sys.exit()

runout = abs(runout)
TotalDiff = abs(TotalDiff)

cnt = 0
cnt = (runout // TotalDiff) * 2
if runout % TotalDiff != 0:
    cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
ma = lambda: map(int, input().split())
t,u = ma(); a,c = ma(); b,d = ma()
p = (a-b)*t; q = (c-d)*u
if p < 0: p *= -1; q *= -1
if p+q > 0: print(0)
elif p+q == 0: print('infinity')
else:
  t = -p-q
  print(1+(p//t)*2 if p%t else (p//t)*2)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(T: "List[int]", A: "List[int]", B: "List[int]"):

    if A[0] < B[0]:
        A, B = B, A

    if A[1] > B[1]:
        print(0)
        return
    elif A[0]*T[0]+A[1]*T[1] > B[0]*T[0]+B[1]*T[1]:
        print(0)
        return
    elif A[0]*T[0]+A[1]*T[1] == B[0]*T[0]+B[1]*T[1]:
        print("infinity")
        return

    K = (B[0]*T[0]+B[1]*T[1])-(A[0]*T[0]+A[1]*T[1])
    L = (A[0]-B[0])*T[0] // K
    if (B[0]-A[0])*T[0] % K == 0:
        print(2*L)
    else:
        print(2*L+1)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    T = [int(next(tokens)) for _ in range(2)]  # type: "List[int]"
    A = [int(next(tokens)) for _ in range(2)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(2)]  # type: "List[int]"
    solve(T, A, B)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
if t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
    print("infinity")
    exit()
if a1 < b1:
    tnp = a1
    a1 = b1
    b1 = tnp
    tnp = a2
    a2 = b2
    b2 = tnp
if (a1-b1) * t1 + (a2-b2) * t2 > 0:
    print(0)
    exit()
num = -(a1-b1) * t1 - (a2-b2) * t2
ans = 1

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
T = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

p = (a[0] - b[0]) * T[0]
q = (a[1] - b[1]) * T[1]

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

Submission status: Accepted

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

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

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

x,y = a1*t1,b1*t1
z,w = a1*t1 + a2*t2, b1*t1 + b2*t2
if (x > y and z > w) or (y > x and w > z):
    print(0)
    quit()
if (z == w) :
    print('infinity')
    quit()
if x < y:
    x,y = y,x
if z < w:
    z,w = w,z
s1 = x - y
s2 = z - w
p = math.ceil(s1/s2)
q = s1 % s2
ans = 2*p - (q != 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*T1-B1*T1
b = A2*T2-B2*T2

if a<0:
    if b<0:
        print(0)
    elif b==0:
        print(0)
    elif b>0:
        if a<-b:
            print(0)
        elif a==-b:
            print('infinity')
        else:
            d = a+b
            
            if a%d==0:
                n = (-a)//d-1
                print(2*n+2)
            else:
                n = (-a)//d
                print(2*n+1)
elif a==0:
    if b<0:
        print(1)
    elif b==0:
        print('infinity')
    elif b>0:
        print(1)
elif a>0:
    if b<0:
        if a>-b:
            print(0)
        elif a==-b:
            print('infinity')
        else:
            d = -a-b
            
            if a%d==0:
                n = a//d-1
                print(2*n+2)
            else:
                n = a//d
                print(2*n+1)
    elif b==0:
        print(0)
    elif b>0:
        print(0)

Submission status: Accepted

Solution code:
import math
t1, t2, a1, a2, b1, b2 = [int(_) for _ in open(0).read().split()]
c1 = (b1 - a1) * t1
c2 = c1 + (b2 - a2) * t2
if c1 * c2 > 0:
    print(0)
    exit()
if c2 == 0:
    print('infinity')
    exit()
if c1 < 0:
    c1 *= -1
    c2 *= -1
ans = 2 * (c1 // (-c2)) + (c1 % c2 != 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)*(a2-b2)>0:
  print(0)
elif a1*t1+a2*t2==b1*t1+b2*t2:
  print('infinity')
else:
  if a1>b1:
    da=(a1-b1)*t1
    db=(b2-a2)*t2
    
    if da>db:
      print(0)
    else:
      temp=da//(db-da)
      if da%(db-da)>0:
        temp=2*temp
      else:
        temp=2*temp-1
      print(temp+1)
  else:
    db=(b1-a1)*t1
    da=(a2-b2)*t2
    
    if db>da:
      print(0)
    else:
      temp=db//(da-db)
      if db%(da-db)>0:
        temp=2*temp
      else:
        temp=2*temp-1
      print(temp+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=(B1-A1)*T1
Y=(B2-A2)*T2
if X>0:
    X=-X
    Y=-Y
if X+Y<0:
    print(0)
elif X+Y==0:
    print("infinity")
else:
    A=(-X)//(X+Y)
    B=(-X)%(X+Y)
    if B!=0:
        print(2*A+1)
    else:
        print(2*A)

Submission status: Accepted

Solution code:
def solve():
    from sys import stdin
    f_i = stdin
    
    T1, T2 = map(int, f_i.readline().split())
    A1, A2 = map(int, f_i.readline().split())
    B1, B2 = map(int, f_i.readline().split())
    
    P = (A1 - B1) * T1
    Q = (A2 - B2) * T2
    
    if P < 0:
        P *= -1
        Q *= -1
    
    cycle = P + Q
    if cycle > 0:
        return 0
    elif cycle == 0:
        return 'infinity'
    else:
        S, T = divmod(P, -cycle)
        if T == 0:
            return 2 * S
        else:
            return 2 * S + 1

print(solve())

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*T1+A2*T2 == B1*T1+B2*T2:
    print('infinity')
elif (A1-B1)*T1/((A1-B1)*T1+(A2-B2)*T2) > 0:
    print(0)    
else:
    n = abs((A1-B1)*T1/((A1-B1)*T1+(A2-B2)*T2))
    meet = math.ceil(n) + int(n)
    print(meet)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


t1, t2 = LI()
a1, a2 = LI()
b1, b2 = LI()
p = (a1 - b1) * t1
q = (a2 - b2) * t2
if p > 0:
    p = -p
    q = -q
if p + q == 0:
    print('infinity')
elif p + q < 0:
    print(0)
else:
    print((-p // (p + q)) * 2 + bool(-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())

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

x1 = C1 * T1 + C2 * T2
x2 = x1 + C1 * T1

if x1 == 0:
    print('infinity')
elif x1 < 0:
    print(0)
else:
    if 0 < x2:
        count = 1
    elif 0 == x2:
        count = 2
    else:
        n = (-x2 + x1 - 1) // x1
        count = 1 + 2 * n
        if x2 % x1 == 0:
            count += 1
    print(count)

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

sa = t1*a1+t2*a2-t1*b1-t2*b2

if sa==0:
    print("infinity")
else:
    if sa>0:
        if a1>b1:
            print(0)
        else:
            ganba=t1*b1-t1*a1
            if ganba%sa!=0:
                gyakuten=(ganba//sa)+1
                print(gyakuten*2-1)
            else:
                gyakuten=(ganba//sa)+1
                print(gyakuten*2-2)
            #print(sa, ganba)
    else:
        sa=sa*(-1)
        if b1>a1:
            print(0)
        else:
            ganba=t1*a1-t1*b1
            if ganba%sa!=0:
                gyakuten=(ganba//sa)+1
                print(gyakuten*2-1)
            else:
                gyakuten=(ganba//sa)+1
                print(gyakuten*2-2)

Submission status: Accepted

Solution code:
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
import time
start=time.time()
x=(a1-b1)*t1
y=(a2-b2)*t2
if x<0:
    if x+y<0:
        print(0)
        exit()
    if x+y==0:
        print("infinity")
        exit()
    if -1*x%(x+y):
        print(-1*x//(x+y)*2+1)
    else:
        print(-1*x//(x+y)*2)
else:
    x*=-1
    y*=-1
    if x+y<0:
        print(0)
        exit()
    if x+y==0:
        print("infinity")
        exit()
    if -1*x%(x+y):
        print(-1*x//(x+y)*2+1)
    else:
        print(-1*x//(x+y)*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,q=(a1-b1)*t1,(a2-b2)*t2
if p>0:
  p*=-1
  q*=-1
if p+q<0:
  print(0)
if p+q==0:
  print('infinity')
if p+q>0:
  s,t=divmod((-p),(p+q))
  print(s*2+1) if t!=0 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())

if (A1 - B1) * T1 + (A2 - B2) * T2 == 0:
    print("infinity")
else:
    D, D1 = (A1 - B1) * T1 + (A2 - B2) * T2, (A1 - B1) * T1
    if (D1 > 0) == (D > 0):
        print(0)
    else:
        if D1 % D == 0:
            print(2 * math.floor(-D1 / D))
        else:
            print(2 * math.floor(-D1 / D) + 1)

Submission status: Accepted

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

sum_a = t1 * a1 + t2 * a2
sum_b = t1 * b1 + t2 * b2

if sum_a == sum_b:
    print('infinity')
    exit()
    
if sum_a > sum_b:
    big_sum = sum_a
    big_1 = a1*t1
    big_2 = a2*t2
    sml_sum = sum_b
    sml_1 = b1*t1
    sml_2 = b2*t2
else:
    big_sum = sum_b
    big_1 = b1*t1
    big_2 = b2*t2
    sml_sum = sum_a
    sml_1 = a1*t1
    sml_2 = a2*t2
    
if sml_1 < big_1:
    print(0)
    exit()
    
#print(big_sum,sml_sum)
    
n = (big_1-sml_1) // (sml_sum-big_sum)

if (big_1-sml_1) % (sml_sum-big_sum) == 0:
    print(n*2)
else:
    print(n*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 = (b2-a2)*t2

if d1 == d2:
    print('infinity')
 
if d1 * (d2 - d1) < 0:
    print(0)

if d1*(d2-d1)>0:
    if d1%(d1-d2)==0:
        print((d1//(d2-d1))*2)
    else:
        print((d1//(d2-d1))*2+1)

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

d1 = T1*(A1-B1)
d2 = T2*(B2-A2)

if d1 == d2:
    print('infinity')

if d1 * (d2 - d1) < 0:
    print(0)

if d1 * (d2 - d1) > 0:
    if d1 % (d2 - d1) != 0:
        print(d1 // (d2 - d1) * 2+ 1)
    else:
        print(d1 // (d2 - d1) * 2)