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:
#!/usr/bin/env python3

import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union
# import time
# import math
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import Fraction               # Fraction(a, b) => a / b ∈ Q. note: Fraction(0.1) do not returns Fraciton(1, 10). Fraction('0.1') returns Fraction(1, 10)



def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79e+308
    # inf = 2 ** 63 - 1             # (for fast JIT compile in PyPy) 9.22e+18
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def isp():   return input().split()
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())


    def solve():
        x = (a1 - b1) * t1
        y = (a2 - b2) * t2
        # print(x, y, x + y)
        if x * (x + y) > 0:
            # 二度と会わない
            return 0
        elif abs(x) % abs(x + y) == 0:
            return 1 - 1 + 2 * (abs(x) // abs(x + y))
        else:
            return 1 + 2 * (abs(x) // abs(x + y))
        
    
    
    t1, t2 = mi()
    a1, a2 = mi()
    b1, b2 = mi()

    if a1 * t1 + a2 * t2 == b1 * t1 + b2 * t2:
        print('infinity')
    else:
        print(solve())


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

T1,T2 = map(int,readline().split())
A1,A2 = map(int,readline().split())
B1,B2 = map(int,readline().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())

d1 = T1*(A1-B1)
d2 = T2*(A2-B2)
if d1>d2:
    d1 *= -1
    d2 *= -1

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


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


def main():
    T1, T2 = map(int, input().split())
    A1, A2 = map(int, input().split())
    B1, B2 = map(int, input().split())
    A = A1 * T1 + A2 * T2
    B = B1 * T1 + B2 * T2

    if A1 < B1 and A < B:
        print(0)
        return

    if A1 > B1 and A > B:
        print(0)
        return

    if A == B:
        print("infinity")
        return

    # T1 + T2 分後にできる差
    diff = abs(A - B)

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from math import gcd


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


sys.setrecursionlimit(20000000)

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


def main():
    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 = P * (-1)
        Q = Q * (-1)

    if P + Q < 0:
        print(0)
        return
    elif P + Q == 0:
        print("infinity")
        return
    else:
        S = (-P) // (P + Q)
        T = (-P) % (P + Q)
        if T == 0:
            print(2 * S)
        else:
            print(2 * S + 1)


if __name__ == "__main__":
    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())

if A1 < B1:
    A1, B1 = B1, A1
    A2, B2 = B2, A2

if A1 * T1 + A2 * T2 > B1 * T1 + B2 * T2:
    print(0)
    sys.exit()

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

N = (T1 * (A1 - B1) - 1) // (T1 * (B1 - A1) + T2 * (B2 - A2)) + 1

ans = N * 2 - 1

if N * (T1 * (B1 - A1) + T2 * (B2 - A2)) == T1 * (A1 - B1):
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, 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
from fractions import gcd
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 ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

if T1*A1+T2*A2 == T1*B1+T2*B2:
    print("infinity")
    exit()
if (A1 < B1 and A2 < B2) or (B1 < A1 and B2 < A2):
    print(0)
    exit()

if A1 > B1:
    A1, B1 = B1, A1
    A2, B2 = B2, A2

if A1*T1+A2*T2 < B1*T1+B2*T2:
    print(0)
    exit()

F = A1*T1-B1*T1
L = A2*T2-B2*T2

S, T = divmod(-F, F+L)

if T == 0:
    print(2*S)
else:
    print(2*S+1)

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def main():
    T = NLI()
    A = NLI()
    B = NLI()
    gap = [a-b for a, b in zip(A, B)]

    if T[0]*gap[0] + T[1]*gap[1] == 0:
        print("infinity")
        exit()

    if gap[0]*gap[1] > 0 or abs(T[0]*gap[0]) > abs(T[1]*gap[1]):
        print(0)
        exit()

    x = abs(gap[0]*T[0])
    y = abs(T[0]*gap[0] + T[1]*gap[1])
    if x % y == 0:
        print(x // y * 2)
        exit()
    else:
        print(x // y * 2 + 1)



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:
  if((-P)%(P+Q)==0):
    print((-P)//(P+Q)*2)
  else:
    print((-P)//(P+Q)*2+1)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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:
    ans = "infinity"
else:
    if a1+a2 < b1+b2:
        a1,a2,b1,b2 = b1,b2,a1,a2
    # a1+a2が大きい
    if a1>b1:
        ans = 0
    else:
        ans = ((b1-a1) // (a1+a2-b1-b2)) * 2 + 1
        if (b1-a1) % (a1+a2-b1-b2) ==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())
x=(a1-b1)*t1
y=(a2-b2)*t2
if x>0:
  x*=-1
  y*=-1
if x+y<0:print(0)
elif x+y==0:print("infinity")
else:
  ans=(-x)//(x+y)
  if (-x)%(x+y):print(ans*2+1)
  else:print(ans*2)

Submission status: Accepted

Solution code:
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)
elif abs(n1)==abs(n2):
  print('infinity')
else:
  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())
first = a1 * t1 - b1 * t1
second = (a2 * t2 - b2 * t2)
last = second + first
if first * last > 0:
    ans = 0
elif (abs(last) == 0):
    ans = 'infinity'
else:
    s = abs(first) // abs(last)
    t = abs(first) % abs(last)
    ans = 2 * s
    if t != 0:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

t, tt = map(int, input().split())
a, aa = map(int, input().split())
b, bb = map(int, input().split())

if a*t+aa*tt == b*t+bb*tt or a*t == b*t:
    print('infinity')
    exit()

diff1 = a*t-b*t

diff12 = a*t+aa*tt - (b*t+bb*tt)

if diff1*diff12>0:
    print(0)
    exit()

diff1 = abs(diff1)
diff12 = abs(diff12)

if diff1%diff12==0:
    print(-(-diff1//diff12)*2)
else:
    print(-(-diff1//diff12)*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())
d = a1*t1+a2*t2-b1*t1-b2*t2
de = -a1*t1+b1*t1
if d == 0:
    print('infinity')
else:
    if a1<b1:
        if d < 0:
            print(0)
        else:
            if de%d == 0:
                print(2*(de//d))
            else:
                print(2*(de//d)+1)
    else:
        if d > 0:
            print(0)
        else:
            de = -de
            d = -d
            if de%d == 0:
                print(2*(de//d))
            else:
                print(2*(de//d)+1)

Submission status: Accepted

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

f_a = t[0]*a[0]
f_b = t[0]*b[0]
if f_a < f_b:
    x = 1
else:
    x = -1

s_a = f_a + t[1]*a[1]
s_b = f_b + t[1]*b[1]
if s_a < s_b:
    y = 1
else:
    y = -1

if s_a==s_b:
    print('infinity')
else:
    if x==y:
        print(0)
    else: 
        s = abs(f_a-f_b)
        t = abs(s_a-s_b)
        print((s//t*2)+(s%t!=0))

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

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()))
if T[0]* A[0] + T[1] * A[1] == T[0] * B[0] + T[1] * B[1]:
    print('infinity')
else:
    ans = 0
    A_pos = 0
    B_pos = 0
    before_state = -1 # 0:A>B, 1:A < B, -1:A=B
    flag = True
    diff = abs(T[0]* A[0] + T[1]* A[1] - (T[0]* B[0] + T[1]* B[1]))
    diff_max = max(abs(A[0] - B[0])*T[0],  abs(A[1] - B[1])*T[1])
    max_num = int(diff_max/diff)
    up_num_first = 0
    for i in range(2):
        A_pos += T[i]* A[i]
        B_pos += T[i]* B[i]
        if A_pos > B_pos:
            if before_state == 0:
                up_num_first += 1
            before_state = 1
        elif A_pos < B_pos:
            if before_state == 1:
                up_num_first += 1
            before_state = 0
        elif A_pos == B_pos:
            up_num_first += 1
            before_state = -1
    A_pos_first = A_pos
    B_pos_first = B_pos
            
    up_num_second = 0
    for i in range(2):
        A_pos += T[i]* A[i]
        B_pos += T[i]* B[i]
        if A_pos > B_pos:
            if before_state == 0:
                up_num_second += 1
            before_state = 1
        elif A_pos < B_pos:
            if before_state == 1:
                up_num_second += 1
            before_state = 0
        elif A_pos == B_pos:
            up_num_second += 1
            before_state = -1

    A_pos = A_pos_first * (max_num - 1)
    B_pos = B_pos_first * (max_num - 1)
    up_num_final = 0
    for i in range(2):
        A_pos += T[i]* A[i]
        B_pos += T[i]* B[i]
        if A_pos > B_pos:
            if before_state == 0:
                up_num_final += 1
            before_state = 1
        elif A_pos < B_pos:
            if before_state == 1:
                up_num_final += 1
            before_state = 0
        elif A_pos == B_pos:
            up_num_final += 1
            before_state = -1
    if max_num > 1:
        ans = up_num_first  + (max_num - 2) * up_num_second  + up_num_final
    else:
        ans = up_num_first  + (max_num - 1) * up_num_second
    print(ans)

Submission status: Accepted

Solution code:
t1,t2=map(int,input().split())
a,b=map(int,input().split())
c,d=map(int,input().split())
x=t1*(a-c)
y=t2*(b-d)
if x<0:
  x=-x
  y=-y
if x+y==0:
  print('infinity')
elif x+y>0:
  print(0)
else:
  print(min(1,x%abs(x+y))+2*(x//abs(x+y)))

Submission status: Accepted

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

x1 = (a1-b1) * t1
x2 = (a2-b2) * t2 + x1
      
if x2 ==0:
    print("infinity")
elif x1*x2>0:
    print(0)
else:
    print(abs(x1) // abs(x2) * 2 + (abs(x1)%abs(x2) > 0))

Submission status: Accepted

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    T = LI()
    A = LI()
    B = LI()

    p1 = (A[0] - B[0]) * T[0]
    p2 = p1 + (A[1] - B[1]) * T[1]
    if p1 * p2 > 0:
        print(0)
        return
    elif p2 == 0:
        print('infinity')
        return

    ans = 1
    p2 = abs(p2)
    p1 = abs(p1)
    if p1 % p2 == 0:
        ans -= 1
    ans += (p1 // p2) * 2
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    T = LI()
    A = LI()
    B = LI()

    p1 = (A[0] - B[0]) * T[0]
    p2 = p1 + (A[1] - B[1]) * T[1]
    if p1 * p2 > 0:
        print(0)
        return
    elif p2 == 0:
        print('infinity')
        return

    ans = 1
    if p2 + p1 == 0:
        print(2)
        return
    p2 = abs(p2)
    p1 = abs(p1)
    if p2 > p1:
        print(1)
        return
    if p1 % p2 == 0:
        ans -= 1
    ans += (p1 // p2) * 2
    print(ans)

if __name__ == '__main__':
    main()

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

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

a, b = A1 * T1, A2 * T2
c, d = B1 * T1, B2 * T2

if a + b == c+ d:
    print("infinity")
    exit()
if a + b < c+d:
    a,b,c,d = c,d,a,b
if a > c:
    print(0)
    exit()
else:
    num = 0
    if (c-a) % (a+b-c-d)  == 0:
        num = 1
    print((c-a-1) // (a+b-c-d) * 2+num+1)

Submission status: Accepted

Solution code:
a,b,c,d,e,f=map(int,open(0).read().split());g,h=(c-e)*a,(d-f)*b
if g>0:g,h=-g,-h
i=g+h
if i<0:print(0)
elif i==0:print("infinity")
else:print(2*(-g//i)+[0,1][int(bool(-g%i))])

Submission status: Accepted

Solution code:
t1,t2=[int(j) for j in input().split()]
a1,a2=[int(j) for j in input().split()]
b1,b2=[int(j) for j in input().split()]
c1,c2=a1-b1,a2-b2
d1,d2=c1*t1,c2*t2
if d1>0:
    d1=-d1
    d2=-d2
if d1+d2<0:
    print(0)
elif d1+d2==0:
    print("infinity")
else:
    print(2*(-d1//(d2+d1))+[0,1][int(bool(-d1%(d2+d1)))])

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

xa = A1*T1 + A2*T2
xb = B1*T1 + B2*T2
if xa > xb:
  print(0)
elif xa == xb:
  print('infinity')
else:
  d1 = (A1 - B1) * T1
  d2 = xb - xa
  n = d1 // d2
  if d1 % d2 == 0:
    print(2*n)
  else:
    print(2*n + 1)

Submission status: Accepted

Solution code:
T = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))

S = [0]*2
for i in range(2):
    S[0] += T[i]*A[i]
    S[1] += T[i]*B[i]
if S[0]==S[1]:
    print("infinity")
else:
    if S[0]<S[1]:
        A,B = B,A
        S = S[::-1]
    if A[0]==B[0] or A[1]==B[1]:
        print(0)
    elif A[0]>B[0] and A[1]>B[1]:
        print(0)
    elif A[0]>B[0] and A[1]<B[1]:
        print(0)
    elif A[0]<B[0] and A[1]>B[1]:
        S = S[0]-S[1]
        T = T[0]*(B[0]-A[0])
        print(T//S*2-1+(1 if T%S==0 else 2))

Submission status: Accepted

Solution code:
t1,t2 = map(int,input().split())
c1,c2 = map(int,input().split())
d1,d2 = map(int,input().split())
hosei = 0
if t1*c1>t1*d1:
    a1=c1
    a2=c2
    b1=d1
    b2=d2
else:
    a1,a2,b1,b2=d1,d2,c1,c2
if t1*a1+t2*a2 > t1*b1+t2*b2:
    print(0)
    exit()
elif t1*a1+t2*a2 == t1*b1+t2*b2:
    print("infinity")
else:
    # print((t1*(a1-b1))//(t2*(b2-a2)) + 1,t1*(a1-b1),t2*(b2-a2))
    if t2*(b2-a2) % (t2*(b2-a2)-t1*(a1-b1)) == 0:
        hosei = 1
    print((t2*(b2-a2)) // (t2*(b2-a2)-t1*(a1-b1))*2-1-hosei)
    # print((t2*(a2-b2)))

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    T=LI()
    A=LI()
    B=LI()
    
    SA=T[1]*A[1]+T[0]*A[0]
    SB=T[1]*B[1]+T[0]*B[0]
    
    if SA == SB:
        print("infinity")
    else:
        #t1+t2分後に確実にAが先行するように．
        if SB>SA:
            SB,SA=SA,SB
            A,B=B,A
        
        if A[0]>=B[0]:
            print(0)
        else:
            diff1=(B[0]-A[0])*T[0]
            diff2=SA-SB
            cnt=(diff1//diff2+1)
            ans=(cnt)*2-1
            #基本，1ループで2回，初回は0,0のため-1
            #さらに，cnt回目のループでぎり1回だけという場合があるので，その場合は1ひく
            if diff2*(cnt-1) == diff1:
                ans-=1
            print(ans)

main()

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    T=LI()
    A=LI()
    B=LI()
    
    
    d1=(A[0]-B[0])*T[0]
    d2=(A[1]-B[1])*T[1]
    
    if d1>=0:
        #最初は必ず正の方向へいかないように調整
        d1*=-1
        d2*=-1
        
    
    if d1+d2==0:
        print("infinity")
    elif d1==0:#d1=0のパターンを排除した
        print(0)
    elif d1+d2<=0:
        print(0)
    else:
        cnt=(-1*d1)//(d1+d2)
        ans=cnt*2+1
        if cnt*(d1+d2)==-1*d1:
            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())
x=(a1-b1)*t1
y=(a2-b2)*t2
if x+y==0:
  print('infinity')
elif x*(x+y)>0:
  print(0)
else:
  k=x//(-x-y)
  if x%(-x-y)==0:
    print(k*2)
  else:
    print(k*2+1)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import math

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

p = a1*t1
q = b1*t1
x = p + a2*t2
y = q + b2*t2

if x < y:
  if p < q:
    print(0)
  elif p == q:
    print(1)
  elif p > q:
    print(int((p-q) / (y-x)) + math.ceil((p-q) / (y-x)))
elif x == y:
  print("infinity")
elif x > y:
  if p < q:
    print(int((q-p) / (x-y)) + math.ceil((q-p) / (x-y)))
  elif p == q:
    print(1)
  elif p > q:
    print(0)

Submission status: Accepted

Solution code:
T = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))

d = [(A[i] - B[i])*T[i] for i in range(2)]
if d[0] < 0:
    d = [-d[0],-d[1]]
s = sum(d)
if s == 0:
    print('infinity')
elif s > 0:
    print(0)
else:
    print(d[0]//(-s)*2 + 1 - (d[0]%s == 0))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

t1, t2, a1, a2, b1, b2 = map(int, read().split())
v1 = t1 * (a1 - b1)
v2 = t2 * (a2 - b2)
if v1 > 0:
    v1 *= -1
    v2 *= -1
if v1 + v2 == 0:
    print('infinity')
elif v1 + v2 < 0:
    print(0)
else:
    s, t = divmod(-v1, v1 + v2)
    ans = s * 2
    if t != 0:
        ans += 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()))
if a[0]*t[0]+a[1]*t[1]==b[0]*t[0]+b[1]*t[1]:
    print('infinity')
elif a[0]*t[0]+a[1]*t[1]>b[0]*t[0]+b[1]*t[1]:
    if a[0]*t[0]>b[0]*t[0]:
        print(0)
    elif a[0]*t[0]==b[0]*t[0]:
        print(1)
    else:
        n=t[0]*(b[0]-a[0])
        m=t[0]*(a[0]-b[0])+t[1]*(a[1]-b[1])
        q=n//m
        if n%m==0:
            print(2*q)
        else:
            print(2*q+1)
else:
    if a[0]*t[0]<b[0]*t[0]:
        print(0)
    elif a[0]*t[0]==b[0]*t[0]:
        print(1)
    else:
        n=-t[0]*(b[0]-a[0])
        m=-t[0]*(a[0]-b[0])-t[1]*(a[1]-b[1])
        q=n//m
        if n%m==0:
            print(2*q)
        else:
            print(2*q+1)

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())
    dis1_a = A1 * T1
    dis1_b = B1 * T1
    dis2_a = A2 * T2
    dis2_b = B2 * T2

    if dis1_a + dis2_a == dis1_b + dis2_b:
        print('infinity')
        exit()

    if (dis1_a - dis1_b) > 0 and (dis2_a + dis1_a - dis2_b - dis1_b) > 0:
        print(0)
        exit()

    if (dis1_a - dis1_b) < 0 and (dis2_a + dis1_a - dis2_b - dis1_b) < 0:
        print(0)
        exit()

    #print(dis1_a - dis1_b)
    #print(dis2_a + dis1_a - dis2_b - dis1_b)
    #print(dis2_a + dis1_a - dis2_b - dis1_b + (dis1_a - dis1_b))
    #print(2*(dis2_a + dis1_a - dis2_b - dis1_b))

    diff = (dis1_a - dis1_b) - (dis2_a + dis1_a - dis2_b - dis1_b + (dis1_a - dis1_b))
    cnt = (dis1_a - dis1_b) // diff
    print(cnt * 2 if (dis1_a - dis1_b) % diff == 0 else cnt * 2 + 1)

    


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
INF = 10 ** 9
MOD = 10 ** 9 + 7
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from  math import factorial


def main():
    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:
        ans = 'infinity'
    elif a1 == b1 or a2 == b2:
        ans = 'infinity'
    elif (a1 < b1 and a2 < b2) or (a1 > b1 and a2 > b2):
        ans = 0
    else:
        if a1 > b1 and a2 < b2:
            a1,b1 = b1,a1
            a2,b2 = b2,a2
        if (b1 - a1) * t1 + (b2 - a2) * t2 > 0:
            ans = 0
        else:
            x = (a1 - b1) * t1 + (a2 - b2) * t2
            if (b1 * t1 - a1 * t1) < x:
                ans = 1
            elif (b1 * t1 - a1 * t1)%x == 0:
                ans = 1 + 2 * ((b1 * t1 - a1 * t1)//x - 1) + 1
            else:
                ans = 1 + 2 * ((b1 * t1 - a1 * t1)//x)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
T_1, T_2 = map(int, input().split())
A_1, A_2 = map(int, input().split())
B_1, B_2 = map(int, input().split())

f = (A_1 - B_1) * T_1
s = (A_2 - B_2) * T_2

if f * (f + s) > 0:
	ans = 0
elif f * (f + s) == 0:
	ans = 'infinity'
else:
	ans = 1
	if f < 0:
		f *= -1
		s *= -1
	d = f + s
	div, mod = divmod(f, abs(d))
	ans += div * 2
	if mod == 0:
		ans -= 1

print(ans)

Submission status: Accepted

Solution code:

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

d1=a1-b1
d2=a2-b2

if d1*d2>0:
    print(0)
else:
    if d1>0 and d2<0:
        plus=d1*t1+d2*t2
        if plus>0:
            print(0)
        elif plus==0:
            print("infinity")
        elif plus<0:
            d=-plus
            k=d1*t1
            if k%d==0:
                print(2*k//d)
            else:
                print(2*(k//d)+1)
    else:
        d1=-d1
        d2=-d2
        plus=d1*t1+d2*t2
        if plus>0:
            print(0)
        elif plus==0:
            print("infinity")
        elif plus<0:
            d=-plus
            k=d1*t1
            if k%d==0:
                print(2*k//d)
            else:
                print(2*(k//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())
OU=T1*(A1-B1)
HUKU=T2*(A2-B2)
TOTAL=OU+HUKU
if TOTAL==0:
 print("infinity")
elif (OU>0 and TOTAL>0) or (OU<0 and TOTAL<0):
 print(0)
elif (OU>0 and TOTAL*(-1)>OU) or (OU<0 and TOTAL*(-1)<OU):
 print(1)
else:
 K=int(OU/TOTAL)*-1
 if (OU%TOTAL)==0:
  print(K*2)
 else:
  print(K*2+1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
T1,T2,A1,A2,B1,B2=map(int, open(0).read().split())
D1,D2 = (A1-B1)*T1, (A1-B1)*T1+(A2-B2)*T2
if D1*D2 > 0:     print(0)
elif D2 == 0:     print("infinity")
elif -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()) #青木君
D1,D2 = (A1-B1)*T1, (A1-B1)*T1+(A2-B2)*T2
if D1*D2 > 0:     print(0)
elif D2 == 0:     print("infinity")
elif -D1%D2 == 0: print(-D1//D2*2)
else:             print(-D1//D2*2+1)