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

L1 = t1*a1+t2*a2
L2 = t1*b1+t2*b2
if L1 == L2:
    print('infinity')
    exit()
if t1*a1 == t1*b1:
    print(1)
    exit()

flg = 1 if L1 < L2 else 0
ok = 0
ng = 10**15+1
mid = (ok + ng)//2
ev = 0
while ng - ok > 1:
    #print(mid * L1 - t2*a2, mid * L2 - t2*b2)
    if mid * L1 - t2*a2 > mid * L2 - t2*b2:
        if flg:
            ok = mid
        else:
            ng = mid
    elif mid * L1 - t2*a2 == mid * L2 - t2*b2:
        ok = mid
        ev = 1
        break
    else:
        if flg:
            ng = mid
        else:
            ok = mid
    mid = (ok + ng)//2
if ok == 0:
    print(0)
    exit()
if ev:
    print((ok-1) * 2)
else:
    print(ok * 2 - 1)

Submission status: Accepted

Solution code:
T1,T2=map(int,input().split())
a,b=map(int,input().split())
c,d=map(int,input().split())
e=T1*a
f=T1*c
if e>f:
  g=e-f
  e=T2*b
  f=T2*d
  if e>f:
    print(0)
  else:
    h=f-e
    if g==h:
      print("infinity")
    elif g>h:
      print(0)
    else:
      k=h-g
      if k>g:
        print(1)
      elif (g/k)%1!=0:
        print(1+(g//k)*2)
      else:
        print((g//k)*2)
elif e<f:
  g=f-e
  e=T2*b
  f=T2*d
  if e<f:
    print(0)
  else:
    h=e-f
    if g==h:
      print("infinity")
    elif g>h:
      print(0)
    else:
      k=h-g
      if k>g:
        print(1)
      elif (g/k)%1!=0:
        print(1+(g//k)*2)
      else:
        print((g//k)*2)

Submission status: Accepted

Solution code:
def solve(t1, t2, a1, a2, b1, b2):
    if a1 * t1 + a2 * t2 == b1 * t1 + b2 * t2:
        return 'infinity'
    if a1 * t1 + a2 * t2 > b1 * t1 + b2 * t2:
        a1, a2, b1, b2 = b1, b2, a1, a2
    
    at = a1 * t1 + a2 * t2
    bt = b1 * t1 + b2 * t2
        
    ans = 0
    dg = bt - at
    if a1 > b1:
        ans += ((a1 - b1) * t1 - 1) // dg
        ans += (a1 - b1) * t1 // dg + 1
    return ans        
    

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

Submission status: Accepted

Solution code:
def examF():
    T = LI()
    A = LI()
    B = LI()
    loop1 = T[0]*(A[0]-B[0])
    loop2 = T[1]*(A[1]-B[1])
    now = loop1
    cur = loop1 + loop2
    if now>0 and cur>0:
        ans = 0
    elif now<0 and cur<0:
        ans = 0
    else:
#        print(now,cur)
        if cur!=0:
            ans = abs(now)//abs(cur)*2
            if abs(now)%abs(cur)!=0:
                judge = now + cur*ans//2
                if (now>0 and judge+loop1>0) or (now<0 and judge+loop1<0):
                    ans +=1
        else:
            ans = "infinity"
    print(ans)
    return

import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examF()

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

import sys
if d1>d2:
    dif=d1-d2
    gap=(b1-a1)*t1
elif d1<d2:
    dif=d2-d1
    gap=(a1-b1)*t1
else:
    print('infinity')
    sys.exit()

if (b1-a1)*(b2-a2)>0 or (b1-a1)*(d2-d1)>0:
    print(0)
    sys.exit()
if gap%dif!=0:
    print(gap//dif*2+1)
else:
    print(gap//dif*2)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
# import numpy as np
# import scipy
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
 
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return [list(map(int, input().split())) for _ in range(n)]

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
t1, t2 = map(int, raw_input().split(' '))
a1, a2 = map(int, raw_input().split(' '))
b1, b2 = map(int, raw_input().split(' '))
v1 = a1 * t1 + a2 * t2
v2 = b1 * t1 + b2 * t2
if v1 == v2:
	print('infinity')
else:
	if(v1 < v2):
		v1, v2 = v2, v1
		a1, a2, b1, b2 = b1, b2, a1, a2
	lo = 0
	hi = 10**100
	ans = 0
	while lo <= hi:
		mid = (lo + hi) / 2
		at_a = mid * v1
		at_b = mid * v2
		if at_b + b1 * t1 >= at_a + a1 * t1:
			ans = mid
			lo = mid + 1
		else:
			hi = mid - 1
	mv = 0
	if ans * v1 + a1 * t1 < ans * v2 + b1 * t1:
		mv = 1
	print(2*ans + mv)

Submission status: Accepted

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

    div = (a1 - b1) * t1 + (a2 - b2) * t2
    ans = (b1 - a1) * t1

    if div == 0:
        print('infinity')
        return
    elif div > 0:
        # includes a2 > b2
        print('0')
        return

    else:
        if ans % div == 0:
            print(2 * (ans // div))
            return
        else:
            print(2 * (ans // div) + 1)
            return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


def main():
	t1,t2=map(int,input().split())
	a1,a2=map(int,input().split())
	b1,b2=map(int,input().split())
	
	a=a1*t1
	aa=a2*t2
	b=b1*t1
	bb=b2*t2
	
	if a+aa==b+bb:
		print("infinity")
		exit(0)
	elif a<b and a+aa<b+bb:
		print(0)
		exit(0)
	elif a > b and a+aa > b+bb:
		print(0)
		exit(0)
	
	else:
		x=abs(a-b)
		y=abs((a+aa)-(b+bb))
		print((x//y)*2+1-(x%y==0))
		
	
	
	
	
	
	
	

	
if __name__ == "__main__":
		main()

Submission status: Accepted

Solution code:
import math


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


def read_list():
    return list(read_values())


def main():
    T1, T2 = read_values()
    A1, A2 = read_values()
    B1, B2 = read_values()

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

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

    if D1 * D2 > 0:
        print(0)
        return

    D1 = abs(D1)
    D2 = abs(D2)

    if D1 > D2:
        print(0)
        return

    i = D1 // (D2 - D1)
    if D1 == i * (D2 - D1):
        print(i * 2)
    else:
        print(1 + 2 * i)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ptr1 = (b1 - a1) * t1
ptr2 = ptr1 + (b2 - a2) * t2
ptr3 = ptr2 + (b1 - a1) * t1
ptr4 = ptr3 + (b2 - a2) * t2

if ptr2 == 0:
  ans = 'infinity'
elif ptr1 * ptr2 > 0:
  ans = 0
else:
  ans = 1 + 2 * (abs(ptr1) // abs(ptr2))
  if abs(ptr1) % abs(ptr2) == 0:
    ans -= 1

print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

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

    if a1 < b1:
        tmp = b1
        b1 = a1
        a1 = tmp

        tmp = b2
        b2 = a2
        a2 = tmp

    l1 = (a1 - b1) * t1
    l2 = (a2 - b2) * t2
    l3 = l1 + l2

    if l3 == 0:
        print("infinity")
        return
    if l3 > 0:
        print(0)
        return
    ans = 1
    if l1 % (-l3) == 0:
        print(ans + 2 * (l1 // (-l3)) - 1)
        return
    print(ans + 2 * (l1 // (-l3)))






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())
d1 = T1 * (A1 - B1)
d2 = T2 * (A2 - B2)
if d1 > 0 and d1 + d2 > 0:
    print(0)
elif d1 < 0 and d1 + d2 < 0:
    print(0)
elif d1 + d2 == 0:
    print("infinity")
else:
    if d1 < 0:
        d1 = -d1
        d2 = -d2
    ok = 0
    ng = (d1 + d1) // (-d1 - d2) + 1
    x = 0
    while ok + 1 < ng:
        mid = (ok + ng) // 2
        s = mid * (d1 + d2)
        if s + d1 > 0:
            ok = mid
        elif s + d1 == 0:
            ok = mid
            ng = ok + 1
            x = -1
        else:
            ng = mid
    print(ng * 2 - 1 + x)

Submission status: Accepted

Solution code:

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

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

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

if A1 + A2 == B1 + B2:
    print('infinity')
elif A1 + A2 < B1 + B2 or A1 == A2:
    print(0)
else:
    def check(c):
        return c * (A1 + A2) + A1 >= c * (B1 + B2) + B1
    
    l = 0
    h = B1 + B2
    while l < h:
        mid = l + (h - l) // 2
        if check(mid):
            h = mid
        else:
            l = mid + 1

    
    print(2*l-(l * (A1 + A2) + A1 != l * (B1 + B2) + B1))

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())
y1=a1*t1-b1*t1
y2=a2*t2-b2*t2
if y1 == -y2:
    print('infinity')
else:
    if y1<0:
        y1*=-1
        y2*=-1
    n=int(np.trunc(y2/(y1+y2)))
    if n<1:
        print(0)
    elif(y2/(y1+y2)==n):
        print((n-1)*2)
    else:
        print((n-1)*2+1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def fi(): return float(input())
def mfi(): return map(float, input().rstrip().split())
def lmfi(): return list(map(float, input().rstrip().split()))
def li(): return list(input().rstrip())
def debug(*args, sep=" ", end="\n"): print("debug:", *args, file=sys.stderr, sep=sep, end=end) if not __debug__ else None
def exit(*arg): print(*arg); sys.exit()
# template


def main():
    t1, t2 = mi()
    a1, a2 = mi()
    b1, b2 = mi()
    aoki = 0
    taka = 0
    meetF = False
    meetS = False
    # a の方が速くなるようにスワップ
    if t1 * a1 + t2 * a2 < t1 * b1 + t2 * b2:
        x1, x2 = a1, a2
        a1, a2 = b1, b2
        b1, b2 = x1, x2
    # print(a1, a2, b1, b2)
    if t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
        exit("infinity")
    if a1 > b1:
        meetF = False
    else:
        meetF = True
    # 1区間目も2区間目もaの法が速いなら合うことはない
    if not meetF:
        exit(0)
    sa = t1 * a1 + t2 * a2 - t1 * b1 - t2 * b2  # 1区切りごとにＡが速く，付いてしまう差
    # print(sa)
    # 何区切りすると追いつけない差になってしまうか
    ng = 10**18
    ok = 1
    while(ng - ok > 1):
        mid = (ng + ok) // 2
        # シミュレーション（mid区切りのあと追いつけるか）
        a = 0
        b = -mid * sa
        if(a + t1 * a1 <= b + t1 * b1):
            ok = mid
        else:
            ng = mid
    # シミュレーション(ok*2回は確実に会う)
    cnt = 2 * (ok - 1) - 1
    a = 0
    b = -(ok - 1) * sa
    for i in range(ok - 1, ok + 10):
        a += t1 * a1
        b += t1 * b1
        if a < b:
            cnt += 2
        if a == b:
            cnt += 1
        a += t2 * a2
        b += t2 * b2
    print(cnt)
    # exit(2 * ok + 1)
    return


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())
s1=t1*a1+t2*a2
s2=t1*b1+t2*b2
if s1>s2:
	s2,s1=s1,s2
if s1==s2:
	print("infinity")
	exit()
import math
cnt=math.ceil(s1/(s2-s1))
aa=t1*a1
bb=t1*b1
aaa=t1*a1+t2*a2
bbb=t1*b1+t2*b2
if (aa>bb and aaa>bbb) or (aa<bb and aaa<bbb):
	print(0)
	exit()
q=abs(aa-bb)
ans=q//(s2-s1)*2+1
if q%(s2-s1)==0:
	ans-=1
print(ans)

Submission status: Accepted

Solution code:
import sys
T1, T2, A1, A2, B1, B2 = map(int, sys.stdin.read().split())
a1, b1 = T1*A1, T1*B1
a2, b2 = T2*A2, T2*B2
sa, sb = a1+a2, b1+b2
if sa==sb:
    print("infinity")
    exit()
if sa < sb:
    a1, a2, b1, b2, sa, sb = b1, b2, a1, a2, sb, sa
# 2*n-1 回以上出会うか
ok = 0
ng = 10**1800
a = 0
while ok+1 < ng:
    c = (ok+ng)//2
    if (c-1)*sa+a1 <= (c-1)*sb+b1:
        if (c - 1) * sa + a1 == (c - 1) * sb + b1:
            a = -1
        ok = c
    else:
        ng = c
if ok > 10**1799:
    print("a")
    print("infinity")
else:
    print(max(ok*2-1+a, 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 * t1 + a2 * t2 == b1 * t1 + b2 * t2:
    print('infinity')
elif (a1 > b1 and a1 * t1 + a2 * t2 > b1 * t1 + b2 * t2) or (a1 < b1 and a1 * t1 + a2 * t2 < b1 * t1 + b2 * t2):
    print(0)
elif a1 * t1 + a2 * t2 > b1 * t1 + b2 * t2:
    sub1 = (a1 * t1 + a2 * t2) - (b1 * t1 + b2 * t2)
    dev1 = ((b1 - a1) * t1) // sub1
    if ((b1 - a1) * t1) % sub1 == 0:
        print(dev1 * 2)
    else:
        print(dev1 * 2 + 1)
else:
    sub1 = (b1 * t1 + b2 * t2) - (a1 * t1 + a2 * t2)
    dev1 = ((a1 - b1) * t1) // sub1
    if ((a1 - b1) * t1) % sub1 == 0:
        print(dev1 * 2)
    else:
        print(dev1 * 2 + 1)

Submission status: Accepted

Solution code:
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
c1=a1*t1+a2*t2
c2=b1*t1+b2*t2
if c1==c2:
    print("infinity")
    exit()
if c1>c2:
    flag=1
else:
    flag=2
if flag==1:
    if a1>b1:
        print(0)
    else:
        m=(b1-a1)*t1
        ans=((m-1)//(c1-c2))*2+1
        if m%(c1-c2)==0:
            ans+=1
        print(ans)
else:
    if a1<b1:
        print(0)
    else:
        m=(a1-b1)*t1
        ans=((m-1)//(c2-c1))*2+1
        if m%(c2-c1)==0:
            ans+=1
        print(ans)

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]
    x = k//(-A)
    ans = 2*x+1
    if A*x+k == 0:
        ans -= 1
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
T = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
A1 = A[0]*T[0]
A2 = A[1]*T[1]
B1 = B[0]*T[0]
B2 = B[1]*T[1]
D1 = A1-B1
D2 = A2-B2
if D1*D2>0:
    output = 0
elif (D1==0)and(D2==0):
    output = "infinity"
elif D1==0:
    output = "infinity"
elif D1+D2==0:
    output = "infinity"
elif abs(D1)>abs(D2):
    output = 0
else:
    Z = abs(D1+D2)
    M1 = abs(D1)
    M2 = abs(D2)
    kanzen = M1//Z
    if Z*kanzen==M1:
        output = kanzen*2
    else:
        output = kanzen*2+1
print(output)

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) * t1
delta2 = (a2 - b2) * t2

if delta1 + delta2 == 0:
    print('infinity')
    exit()

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

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

steps = abs(delta1 + delta2)

ans = 2 * (min(abs(delta1), abs(delta2)) // steps) + 1

if min(abs(delta1), abs(delta2)) % steps == 0:
    ans -= 1

print(ans)

Submission status: Accepted

Solution code:

from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))



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

    saf = (a1-b1)*t1

    sas = (a2-b2)*t2

    wa = saf+sas

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

    if saf>0 and wa > 0:
        print(0)
        exit()
    elif saf<0 and wa < 0:
        print(0)
        exit()

    flag = False
    if not saf%wa:
        flag = True


    a = abs(saf)//abs(wa)
    
    ans = a*2+1

    if flag:
        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())

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

import math
if d == 0:
    ans = 'infinity'
else:
    ans = 0

    # T1
    if d1 == 0:
        ans += 1
    elif d * d1 < 0:
        ans += abs(d1) // abs(d)

    # T2
    if d1 * d < 0:
        ans += math.ceil(abs(d1) / abs(d))

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 t[0]*a[0]+t[1]*a[1]==t[0]*b[0]+t[1]*b[1]:
  print("infinity")
else:
  if t[0]*a[0]>t[0]*b[0]:
    if t[0]*a[0]+t[1]*a[1]>t[0]*b[0]+t[1]*b[1]:
      print(0)
    else:
      d=(t[0]*b[0]+t[1]*b[1])-(t[0]*a[0]+t[1]*a[1])
      c=t[0]*a[0]-t[0]*b[0]
      if c%d==0:
        print(c//d*2)
      else:
        print(c//d*2+1)
  else:
    if t[0]*a[0]+t[1]*a[1]<t[0]*b[0]+t[1]*b[1]:
      print(0)
    else:
      d=(t[0]*a[0]+t[1]*a[1])-(t[0]*b[0]+t[1]*b[1])
      c=t[0]*b[0]-t[0]*a[0]
      if c%d==0:
        print(c//d*2)
      else:
        print(c//d*2+1)

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
T1, T2 = inpl()
A1, A2 = inpl()
B1, B2 = inpl()
DA = A1*T1 + A2*T2
DB = B1*T1 + B2*T2
if DA == DB:
    print('infinity')
    exit()
elif DB > DA:
    A1, A2, B1, B2, DA, DB = B1, B2, A1, A2, DB, DA
    # swap A & B. DA > DB.
if A1 > B1:
    print(0)
    exit()

dD = DA - DB
CA = A1*T1
CB = B1*T1
dC = CB - CA

n = dC // dD
if dC % dD == 0:
    m = 2*n - 1
else:
    m = 2*n
print(m+1)

Submission status: Accepted

Solution code:
import fractions
def main():
    T1, T2 = map(int, input().split())
    f = fractions.gcd(T1, T2)
    T1 //= f
    T2 //= f
    A1, A2 = map(int, input().split())
    B1, B2 = map(int, input().split())
    f = fractions.gcd(fractions.gcd(A1, A2), fractions.gcd(B1, B2))
    A1 //= f
    A2 //= f
    B1 //= f
    B2 //= f

    dif_half = T1*A1 - T1*B1
    dif_full = T1*A1 + T2*A2 - T1*B1 - T2*B2

    if dif_full == 0 or A1 == B1:
        print("infinity")
        return
    elif dif_full * dif_half > 0:   # 増減が入れ替わらない場合は会うことはない
        print(0)
        return
    elif dif_full > 0:
        flg = 1
    else:
        flg = -1

    if flg == 1 and dif_half <= 0:
        ans = 1
    elif flg == -1 and dif_half >= 0:
        ans = 1
    else:
        ans = 0

    ans += (- dif_half // dif_full) * 2 - (- dif_half % dif_full == 0)

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

d1 = T1*A1 - T1*B1
d2 = T2*A2 - T2*B2
d3 = d1+d2
if d3 == 0:
    print('infinity')
    exit()

if (d1 > 0 and d3 > 0) or (d1 < 0 and d3 < 0):
    print(0)
    exit()

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

s1=a1*t1+a2*t2
s2=b1*t1+b2*t2
ts=t1+t2
if s1==s2:
    print("infinity")
    exit()
faster=1 if s1>s2 else 2

if (a1>b1 and faster==1) or (a1<b1 and faster==2):
    print(0)
    exit()

if faster==2:
    a1,b1=b1,a1
    a2,b2=b2,a2

d=abs(s1-s2)
cnt=(b1*t1-a1*t1)//d
r=(b1*t1-a1*t1)%d
print(cnt*2+1 if r!=0 else cnt*2)

Submission status: Accepted

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

taka = T1 * A1 + T2 * A2
aoki = T1 * B1 + T2 * B2
if taka == aoki:
    print("infinity")
    quit()
if taka > aoki:
    taka, aoki = aoki, taka
    A1, B1 = B1, A1
    A2, B2 = B2, A2
if A1 < B1:
    print(0)
    quit()
if A1 == B2:
    print(1)
    quit()
first_diff = A1 * T1 - B1 * T1
diff = aoki - taka
print(first_diff // diff * 2 + (first_diff % diff != 0))

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')
	exit(0)

if (a1*t1 < b1*t1 and a1*t1+a2*t2 < b1*t1+b2*t2) or (a1*t1 > b1*t1 and a1*t1+a2*t2 > b1*t1+b2*t2):
	print(0)
	exit(0)

m = abs(a1*t1-b1*t1)
s = abs(a1*t1+a2*t2-b1*t1-b2*t2)
if m%s == 0:
	print((m//s)*2)
else:
	print((m//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())
dist_lap1 = T1 * A1 + T2 * A2
dist_lap2 = T1 * B1 + T2 * B2
if dist_lap1 == dist_lap2:
    print("infinity")
else:
    if dist_lap1 > dist_lap2:
        diff = T1 * B1 - T1 * A1
        if diff < 0:
            print(0)
        else:
            if diff % (dist_lap1 - dist_lap2) == 0:
                print((diff // (dist_lap1 - dist_lap2)) * 2)
            else :
                print((diff // (dist_lap1 - dist_lap2)) * 2 + 1)
    else:
        diff = T1 * A1 - T1 * B1
        if diff < 0:
            print(0)
        else:
            if diff % (dist_lap2 - dist_lap1) == 0:
                print((diff // (dist_lap2 - dist_lap1)) * 2)
            else :
                print((diff // (dist_lap2 - dist_lap1)) * 2 + 1)

Submission status: Accepted

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

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

q = a1*t1-b1*t1
w = (b1*t1+b2*t2) - (a1*t1+a2*t2)
we = int(q/w)
e = we*2-1
if q%w:
    e+=2
else:
    e+=1
print(e)

Submission status: Accepted

Solution code:
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
a3 = a1 * t1 + a2 * t2
b3 = b1 * t1 + b2 * t2
if a3 == b3:
    print("infinity")
else:
    if a3 < b3:
        a1, a2, a3, b1, b2, b3 = b1, b2, b3, a1, a2, a3
    diff = a3 - b3
    width = (b1 - a1) * t1
    if width < 0:
        print(0)
    else:
        ans = 1
        ans += (width // diff) * 2
        if not width % diff:
            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())
firsttaka = T1 * A1
firstao = T1 * B1
sumtaka = T1 * A1 + T2 * A2
sumao = T1 * B1 + T2 * B2
if sumtaka == sumao:
    print('infinity')
elif firsttaka > firstao and sumtaka > sumao:
    print(0)
elif firsttaka < firstao and sumtaka < sumao:
    print(0)
else:
    sumabs = abs(sumtaka - sumao)
    firstabs = abs(firsttaka - firstao)
    ans = (firstabs // sumabs) * 2
    amari = firstabs % sumabs
    if amari > 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())
a=t1*a1+t2*a2
b=t1*b1+t2*b2
if a==b:
    print('infinity')
    exit()
if a>b:
    if a1>b1:
        print(0)
    else:
        m=b1-a1
        k=m*t1
        p=a-b
        q=1+(k//p)*2
        if k%p==0:
            q-=1
        print(q)
        
if a<b:
    if a1<b1:
        print(0)
    else:
        m=a1-b1
        k=m*t1
        p=b-a
        q=1+(k//p)*2
        if k%p==0:
            q-=1
        print(q)

Submission status: Accepted

Solution code:
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
if a1*t1+a2*t2==b1*t1+b2*t2:print("infinity")
else:
  if (a1-b1)*(a2-b2)>0:print(0)
  else:
    if a1>b1:
      firstD=t1*(a1-b1)
      secondD=t2*(b2-a2)
      lead=secondD-firstD
    else:
      firstD=t1*(b1-a1)
      secondD=t2*(a2-b2)
      lead=secondD-firstD
    if lead<0:print(0);exit()
    if firstD<lead:print(1);exit()
    if firstD%lead==0:
      print((firstD//lead)*2)
    else:print((firstD//lead+1)*2-1)

Submission status: Accepted

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

def f(t):
    if t<=T1:
        return (A1-B1)*t/((B1-A1)*T1+(B2-A2)*T2)
    else:
        return ((A1-B1)*T1+(A2-B2)*(t-T1))/((B1-A1)*T1+(B2-A2)*T2)

ans = 0
if ((B1-A1)*T1+(B2-A2)*T2) == 0:
    ans = 'infinity'
else:
    if f(T1)>0:
        ans += floor(f(T1))
        if f(T1+T2)>0:
            ans += floor(max(f(T1),f(T1+T2)))-ceil(min(f(T1),f(T1+T2)))
        else:
            ans += floor(f(T1))
        if floor(f(T1)) != f(T1):
            ans += 1
    else:
        if f(T1+T2)>0:
            ans += floor(f(T1+T2))+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()))
d = [t[0] * a[0] + t[1] * a[1], t[0] * b[0] + t[1] * b[1]]
if d[0] == d[1]:
	print('infinity')
	exit(0)
if d[0] > d[1]:
	a, b = b, a
	d[0], d[1] = d[1], d[0]
if a[0] < b[0]:
	print(0)
	exit(0)

l, r = 1, 1000000000000000  # номер периода, с которого пересечений не будет больше 2
while l < r:
	mid = (l + r) >> 1
	offset = d[1] * mid - d[0] * mid
	if t[0] * a[0] > offset + t[0] * b[0]:
		l = mid + 1
	else:
		r = mid
offset = d[1] * l - d[0] * l
print(l * 2 - 1 + (t[0] * a[0] == offset + t[0] * b[0]))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

da1 = a1*t1
da2 = da1 + a2*t2
db1 = b1*t1
db2 = db1 + b2*t2

if da2==db2:
	ans = 'infinity'
else:
	if (da1-db1)*(da2-db2)>0:
		ans = 0
	else:
		fdif = abs(da1-db1)
		sdif = abs(da2-db2)
		iteration = fdif//sdif
		mod = fdif%sdif
		if mod==0:
			ans = iteration*2
		else:
			ans = iteration*2+1

print(ans)

Submission status: Accepted

Solution code:
import sys

T1,T2 = map(int,input().split())

A1,A2 = map(int,input().split())

B1,B2 = map(int,input().split())


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

elif (A1 > B1 and A2 > B2) or (A1 < B1 and A2 < B2):
    print (0)
    sys.exit()

else:

    pl = abs ( A1 * T1 - B1 * T1 )
    mi = abs ( A2 * T2 - B2 * T2 )

    #print (pl,mi)

    if pl > mi:
        print (0)

    else:

        if pl < (mi - pl):
            print (1)

        #print ("amari" , pl % (mi - pl))
        elif pl % (mi - pl) != 0:
            print ( pl // (mi - pl) * 2 + 1)
        else:
            print (pl // (mi - pl) * 2)

Submission status: Accepted

Solution code:
T1,T2=[int(s) for s in input().split()]
ls1=[int(s) for s in input().split()]
ls2=[int(s) for s in input().split()]
A1,A2=ls1
B1,B2=ls2

#1ループした際の移動距離が同じなら、infinity
if  A1*T1+A2*T2==B1*T1+B2*T2:
  print('infinity')
else:
  if A1*T1+A2*T2-B1*T1-B2*T2<0: #1ループで長距離進むのはAとする
    A1,A2=ls2
    B1,B2=ls1
  d=A1*T1+A2*T2-B1*T1-B2*T2 #1ループで着く差
  x=(B1-A1)*T1
  ####ループ開始時に差がxメートル以内なら、２回交わる
  ####ループ開始時に差がちょうどxメートルなら、１回交わる
  ####xが負なら永久に交わらない
  if x<0:
    print(0)
  elif x%d==0:
    print(2*(x//d))
  else:
    print(2*(x//d)+1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 
import math
import sys
import itertools
#import numpy as np

# なんこか入力
a,b = map(int, input().split())
# なんこか入力
c,d = map(int, input().split())
# なんこか入力
e,f = map(int, input().split())

p = (e-c)*a
q = (f-d)*b
#print(p,q)
if p*q >0 or p*(p+q)>0:
    print("0")
elif p == -q:
    print("infinity")
elif (-p) % (p+q) == 0:
    print(-p//(p+q)*2)
else:
    print(-p//(p+q)*2+1)