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: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

t = inpl()
a = inpl()
b = inpl()
if (a[0] > b[0] and a[1] > b[1]) or (a[0] < b[0] and a[1] < b[1]):
    print(0)
    quit()
c = [0,0]
for i in range(2):
    c[i] = abs(a[i]-b[i]) * t[i]
if c[0] == c[1]:
    print('infinity')
    quit()
if c[0] > c[1]:
    print(0)
else:
    print((c[1]-1) // (c[1]-c[0]) *2 - 1)

Submission status: Wrong Answer

Solution code:
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 < B1:
    A1, B1 = B1, A1
    A2, B2 = B2, A2
C = A1 - B1
D = B2 - A2

if A1 + A2 == B1 + B2:
    print("infinity")
    exit()
elif C > D:
    print(0)
    exit()

k = (C + (D - C) - 1) // (D - C)
ans = 2 * k - 1
print(ans)

Submission status: Wrong Answer

Solution code:
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 < B1:
    A1, B1 = B1, A1
    A2, B2 = B2, A2
C = A1 - B1
D = B2 - A2

if A1 + A2 == B1 + B2:
    print("infinity")
    exit()
elif C > D:
    print(0)
    exit()

k = (C + (D - C) + 1) // (D - C)
ans = 2 * k - 1
print(ans)

Submission status: Wrong Answer

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:
    d = A1*T1+A2*T2-(B1*T1+B2*T2)
    n = ((B1-A1)*T1)//d
    print(2*n+1)
elif B1<A1 and A1*T1+A2*T2<B1*T1+B2*T2:
    d = B1*T1+B2*T2-(A1*T1+A2*T2)
    n = ((A1-B1)*T1)//d
    print(2*n+1)
else:
    print(0)

Submission status: Wrong Answer

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

c = 0
c += a1*t1-b1*t1
c += a2*t2-b2*t2


#print(c)
if c == 0:
    print('infinity')
    exit()

if c > 0:
    if b1*t1-a1*t1 < 0:
        ans = 0
    else:
        ans = (b1*t1-a1*t1)//c+1
else:
    c *= (-1)
    if -b1*t1+a1*t1 < 0:
        ans = 0
    else:
        ans = (-b1*t1+a1*t1)//c+1
print(2*ans-1)

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 998244353

#############
# Main Code #
#############

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

# グラフにして考える
# 周期は同じT1, T2
# T1の時とT2の時とで順位が入れ替わっているなら出会っている
t1_diff = (A1 - B1) * T1
t2_diff = (A1 - B1) * T1 + (A2 - B2) * T2
if t1_diff == 0 or t2_diff == 0: # 無限に出会う
    print('infinity')
    exit()
if t1_diff * t2_diff > 0: # ずっとどちらかが前にいる
    print(0)

# t2_diff分ずつずれていく

# 順位が逆転する場合
# クロスする時とちょうど接する時を考える
if abs(t1_diff) % abs(t2_diff) == 0:
    # 最後の１回は１回しかクロスしない
    print((abs(t1_diff) // abs(t2_diff)) * 2)
else:
    print((abs(t1_diff) // abs(t2_diff)) * 2 + 1)

Submission status: Wrong Answer

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

a, b, ans = 0, 0, 0
n1 = (A1-B1)*T1
n2 = n1 + (A2-B2)*T2
if n2 == 0:
  print('infinity')
elif n1*n2 < 0:
  ans += abs(n1) // abs(n2)
  print(2*ans+1)
else:
  print(0)

Submission status: Wrong Answer

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

a, b, ans = 0, 0, 0
n1 = (A1-B1)*T1
n2 = n1 + (A2-B2)*T2
tmp1, tmp2 = n1, n2
while True:
  if (tmp1<0<tmp2) or (tmp2<0<tmp1):
    ans += 1
  elif n2 == 0:
    print('infinity')
    exit()
  else:
    break
  tmp1 += n2
  if (tmp1<0<tmp2) or (tmp2<0<tmp1):
    ans += 1
  else:
    break
  tmp2 += n2
print(ans)

Submission status: Wrong Answer

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

a, b, ans, n1, n2 = 0, 0, 0, 0, 0
while True:
  if A1 < B1:
    b += (B1-A1) * T1
  else:
    a += (A1-B1) * T1
  n1 = a-b
  if n1*n2 < 0:
    ans += 1
  if A2 < B2:
    b += (B2-A2) * T2
  else:
    a += (A2-B2) * T2
  n2 = a-b
  if n1*n2 < 0:
    ans += 1
    if a < b:
      a, b = 0, b-a
    else:
      a, b = a-b, 0
  elif n2 == 0:
    print('infinity')
    exit()
  else:
    print(ans)
    exit()

Submission status: Wrong Answer

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

a, b, ans, n1, n2 = 0, 0, 0, 0, 0
while True:
  if A1 < B1:
    b += (B1-A1) * T1
  else:
    a += (A1-B1) * T1
  n1 = a-b
  if n1*n2 < 0:
    ans += 1
  if A2 < B2:
    b += (B2-A2) * T2
  else:
    a += (A2-B2) * T2
  n2 = a-b
  if n1*n2 < 0:
    ans += 1
  elif n2 == 0:
    print('infinity')
    exit()
  else:
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
c1 = T1*A1-T1*B1
c2 = T2*A2-T2*B2
power = c1+c2
if power == 0:
    print('infinity')
    exit()
else:
    ans = 2*(abs(c1)//abs(power))+1
print(ans)

Submission status: Wrong Answer

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
c1 = T1*A1-T1*B1
c2 = T2*A2-T2*B2
power = c1+c2
if power == 0:
    print('infinity')
    exit()
elif power > 0:
    ans = 2*(abs(c1)//abs(power))+1
else:
    ans = 2*(abs(c2)//abs(power))+1
print(ans)

Submission status: Wrong Answer

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
c1 = T1*A1-T1*B1
c2 = T2*A2-T2*B2
power = c1+c2
if power == 0:
    print('infinity')
    exit()
elif power > 0:
    ans = 2*(abs(c1)//abs(power))+1
else:
    ans = 2*(abs(c2)//abs(power))
print(ans)

Submission status: Wrong Answer

Solution code:
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
c1 = T1*A1-T1*B1
c2 = T2*A2-T2*B2
if c1+c2 == 0:
    print('infinity')
    exit()

t1, t2 = c1, 0 
ans = 0
while True:
    t2 = t1 + c2
    if t1*t2 <= 0:
        ans += 1
    else:
        break
    
    t1 = t2 + c1 
    if t1*t2 <= 0:
        ans += 1
    else:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
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<b1:
    a1,a2,b1,b2=b1,b2,a1,a2
#a1>b2
if a1+a2==b1+b2:
    print('infinity')
    exit()

a1-=b1;b2-=a2
if b2<0:
    print(0)
    exit()
b2-=a1

ans=1+(a1//b2)*2
if a1%b2==0:
    ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
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<b1:
    a1,a2,b1,b2=b1,b2,a1,a2
#a1>b2
if a1+a2==b1+b2:
    print('infinity')
    exit()

a1-=b1;b2-=a2
b2-=a1
if b2<0:
    print(0)
    exit()
ans=1+(a1//b2)*2
if a1%b2==0:
    ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
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<b1:
    a1,a2,b1,b2=b1,b2,a1,a2
#a1>b2
if a1+a2==b1+b2:
    print('infinity')
    exit()

a1-=b1;b2-=a2
b2-=a1
if b2<0:
    print(1)
    exit()
ans=1+(a1//b2)*2
if a1%b2==0:
    ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
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<b1:
    a1,a2,b1,b2=b1,b2,a1,a2
#a1>b2
if a1+a2==b1+b2:
    print('infinity')
    exit()

a1-=b1;b2-=a2
b2-=a1
ans=1+(a1//b2)*2
if a1%b2==0:
    ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys
# sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def ep(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        ep(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(T, A, B):
    aa = list(map(lambda x: x[0]*x[1], zip(T, A)))
    bb = list(map(lambda x: x[0]*x[1], zip(T, B)))
    if sum(aa) == sum(bb):
        return 'infinity'

    if sum(aa) < sum(bb):
        aa, bb = bb, aa

    if aa[0] >= bb[0] and aa[1] >= bb[1]:
        return 0

    # ep(sum(aa) - sum(bb))
    # ep(aa[0])
    # ep(bb[0])
    # ep(sum(aa) - sum(bb), (bb[0] - aa[0]))
    ans = 2*(bb[0] - aa[0]) // (sum(aa) - sum(bb))
    return ans


def main():
    T = read_int_n()
    A = read_int_n()
    B = read_int_n()
    print(slv(T, A, B))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

p,q = 0,0
d = []
for i in range(4):
  if i % 2==0:
    p += a1*t1
    q += b1*t1
  else:
    p += a2*t2
    q += b2*t2
  d.append(p-q)
  
if d[0]*d[1]>0:
  print(0)
  exit()
  
if d[0]==d[2]:
  print('infinity')
  exit()
  
if d[0]>=0:
  if d[2]<d[0]:
    print(d[0]//(d[0]-d[2])*2+1)
    exit()

if d[0]<0:
  if d[2]>d[0]:
    print(-d[0]//(-(d[0]-d[2]))*2+1)
    exit()
    
if d[1]>=0:
  if d[3]<d[1]:
    print(d[1]//(d[1]-d[3])*2+1)
    exit()
    
if d[1]<0:
  if d[3]>d[1]:
    print(-d[1]//(-(d[1]-d[3]))*2+1)
    exit()

Submission status: Wrong Answer

Solution code:
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
if (a1*t1-b1*t1)*(a1*t1+a2*t2-b1*t1-b2*t2)>0:print(0)
elif (a1*t1-b1*t1)*(a1*t1+a2*t2-b1*t1-b2*t2)==0:print('infinity')
else:print(-(a1*t1-b1*t1)//(a1*t1+a2*t2-b1*t1-b2*t2)*2+1)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline


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

    if D1 == D2:
        print('infinity')
    else:
        if D2 > D1:
            A1, B1 = B1, A1
            A2, B2 = B2, A2
            D1, D2 = D2, D1
        M1 = T1 * A1
        M2 = T1 * B1
        if M1 > M2:
            print(0)
        else:
            cnt = (M2 - M1) // (D1 - D2)
            print(cnt * 2 + 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


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


def meet(diff, T1, T2, A1, A2, B1, B2):
    ans = 0
    pre_diff = diff
    a = T1 * A1
    b = T1 * B1

    diff += a
    diff -= b

    if (pre_diff * diff) < 0 or (pre_diff != 0 and diff == 0):
        ans += 1

    pre_diff = diff
    a = T2 * A2
    b = T2 * B2

    diff += a
    diff -= b

    if (pre_diff * diff) < 0 or (pre_diff != 0 and diff == 0):
        ans += 1

    return ans


def solve():
    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")
        return

    # 0回
    if (A1 > B1 and A2 > B2) or (A1 < B1 and A2 < B2):
        print(0)
        return

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

    ans = 0
    diff = 0
    for i in range(100):
        num = meet(diff, T1, T2, A1, A2, B1, B2)
        if num == 0:
            break
        ans += num
        diff += one_diff

    left = -1
    right = INF
    while right - left > 0:
        m = (right + left) // 2
        tmp = diff + m * one_diff
        if meet(tmp, T1, T2, A1, A2, B1, B2):
            left = m
        else:
            right = m

    x = max(0, left - 10)
    ans += 2 * x
    diff += one_diff * x

    for i in range(100):
        num = meet(diff, T1, T2, A1, A2, B1, B2)
        if num == 0:
            break
        ans += num
        diff += one_diff

    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


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


def meet(diff, T1, T2, A1, A2, B1, B2):
    ans = 0
    pre_diff = diff
    a = T1 * A1
    b = T1 * B1

    diff += a
    diff -= b

    if (pre_diff * diff) < 0:
        ans += 1

    pre_diff = diff
    a = T2 * A2
    b = T2 * B2

    diff += a
    diff -= b

    if (pre_diff * diff) < 0:
        ans += 1

    return ans


def solve():
    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")
        return
    # 0回
    if (A1 > B1 and A2 > B2) or (A1 < B1 and A2 < B2):
        print(0)
        return

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

    ans = 0
    diff = 0
    while True:
        num = meet(diff, T1, T2, A1, A2, B1, B2)
        if num == 0:
            break
        ans += num
        diff += one_diff

    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

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

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

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

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

    da1 = T1 * A1
    da2 = T2 * A2
    db1 = T1 * B1
    db2 = T2 * B2

    if da1 == db1:
        if db1 == db2:
            ans = "infinity"
        else:
            ans = 1
    else:
        if da1 + da2 == db1 + db2:
            ans = "infinity"
        elif da1 + da2 < db1 + db2:
            ans = 0
        else:
            d1 = db1 - da1
            d2 = (da1 + da2) - (db1 + db2)
            ans = 1 + (d1 // d2) * 2

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    T1, T2 = list(map(int, input().split()))
    A1, A2 = list(map(int, input().split()))
    B1, B2 = list(map(int, input().split()))
    # 青木君を基準にした高橋君の位置を追い、0に何回ぶつかるかを調べる
    V1, V2 = A1 - B1, A2 - B2
    if V1 == 0 or V2 == 0 or V1 * V2 > 0:
        print(0)
        return
    # この後は V1 * V2 < 0
    d1, d2 = V1 * T1, V2 * T2
    d12 = d1 + d2
    if d1 * d12 > 0:
        print(0)
    elif d12 == 0:  # d1 * d12 == 0
        print('infinity')
    else:  # d1 * d12 < 0
        cnt = 0
        # d1で0に達する
        cnt += abs(d1) // abs(d12)
        # d2で0に達する
        cnt += 1 + abs(d1) // abs(d12)
        print(cnt)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

a, b = a1-b1, a2-b2
if a < b:
  a, b = -a, -b
  
sec = a*t1+b*t2

if sec == 0:
  print("infinity")
  exit()
  
elif sec > 0:
  print(0)
  exit()
  
else:  
  ans = ((a*t1)//(-sec))*2+1
  if (a*t1)%(-sec) == 0:
    ans -= 1
  
  print(ans)

Submission status: Wrong Answer

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

a, b = a1-b1, a2-b2
if a < b:
  a, b = -a, -b
  
sec = a*t1+b*t2

if sec == 0:
  print("infinity")
  
elif sec > 0:
  print(0)
  
else:  
  ans = ((a*t1)//(-sec))*2+1
  if (a*t1)%(-sec) == 0:
    ans -= 1
  
  print(ans)

Submission status: Wrong Answer

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

a, b = a1-b1, a2-b2
if a < b:
  a, b = -a, -b
  
sec = a*t1+b*t2

if sec == 0:
  print("infinity")
  
elif sec > 0:
  print(0)
  
else:  
  ans = ((a*t1)//(-sec)+1)*2-1
  if (a*t1)%(-sec) == 0:
    ans -= 1
  
  print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())

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

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

if A1*T1 + A2*T2 > B1*T1 + B2*T2:
    A1,B1 = B1,A1
    A2,B2 = B2,A2

if A1 < B1:
    print(0)
    exit()

print(((A1-B1)*T1)//(B1*T1+B2*T2-A1*T1-A2*T2)*2+1)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

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

    if a2 > b2:
        return print(0)
    else:
        d1 = t1 * a1 + t2 * a2
        d2 = t1 * b1 + t2 * b2
        if d1 == d2:
            return print("infinity")
        else:
            ans = 1
            diff = d2 - d1
            x = t1 * (a1 - b1)
            cnt = x // diff
            ans += cnt * 2
            if x % diff == 0:
                ans -= 1
            return print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

a = a1 * t1 + a2 * t2
b = b1 * t1 + b2 * t2

if a == b:
    print('infinity')
else:
    if b > a:
        if a1 * t1 < b1 * t1:
            print(0)
        elif a1 * t1 == b1 * t1:
            print(1)
        elif a1 * t1 > b1 * t1:
            c = b - a
            d = a1 * t1 - b1 * t1
            print(2 * (d // c) + 1)
    else:
        if b1 * t1 < a1 * t1:
            print(0)
        elif b1 * t1 == a1 * t1:
            print(1)
        elif b1 * t1 > a1 * t1:
            c = a - b
            d = b1 * t1 - a1 * t1
            print(2 * (d // c) + 1)

Submission status: Wrong Answer

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

ave_a, ave_b = t1 * a1 + t2 * a2, t1 * b1 + t2 * b2
if ave_a < ave_b:
    a1, a2, b1, b2 = b1, b2, a1, a2
    ave_a, ave_b = ave_b, ave_a

if ave_a == ave_b:
    print('infinity')
    exit()

first, second = t1 * (b1 - a1), ave_a - ave_b
ans = first // second * 2 + (first % second != 0)
print(ans)

Submission status: Wrong Answer

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

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

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

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

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

Submission status: Wrong Answer

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

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

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

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

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

Submission status: Wrong Answer

Solution code:
import sys

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


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

    if (A1 < B1 and A2 < B2) or (A1 > B1 and A2 > B2):
        print(0)
    else:
        d1 = T1 * A1 + T2 * A2
        d2 = T1 * B1 + T2 * B2
        if d1 == d2:
            print("infinity")
        else:
            now_A = now_B = 0
            res = 0
            flg = None
            for i in range(10 ** 7):
                if i % 2 == 0:
                    now_A += T1 * A1
                    now_B += T1 * B1
                else:
                    now_A += T2 * A2
                    now_B += T2 * B2
                if flg is None:
                    flg = (now_A < now_B)
                else:
                    if flg != (now_A < now_B):
                        res += 1
                        flg = (now_A < now_B)
                    else:
                        break
            print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

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


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

    if (A1 < B1 and A2 < B2) or (A1 > B1 and A2 > B2):
        print(0)
        exit()
    else:
        d1 = abs(T1 * B1 - T1 * A1)
        d2 = abs(T2 * A2 - T2 * B2)

    diff = abs(d2 - d1)
    if diff == 0:
        print("infinity")
    else:
        print(d1 // diff + d2 // diff)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
def sumitb2019_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: return 0
    if a1 < b1 and a2 < b2: return 0

    ya = a1 * t1 + a2 * t2
    yb = b1 * t1 + b2 * t2
    if ya == yb: return 'infinity'
    if ya > yb:
        c1, c2 = b1, b2
        b1, b2 = a1, a2
        a1, a2 = c1, c2

    x1 = (b1 - a1) * t1
    x2 = (b2 - a2) * t2
    k = x2 // (x1 + x2)
    res = 2 * (k-1)
    if x2 % (x1 + x2) > 0: res += 1
    return res

if __name__ == '__main__':
    print(sumitb2019_f())

Submission status: Wrong Answer

Solution code:
def sumitb2019_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: return 0
    if a1 < b1 and a2 < b2: return 0

    ya = a1 * t1 + a2 * t2
    yb = b1 * t1 + b2 * t2
    if ya == yb: return 'infinity'
    if ya > yb:
        c1, c2 = b1, b2
        b1, b2 = a1, a2
        a1, a2 = c1, c2

    x1 = (b1 - a1) * t1
    x2 = (b2 - a2) * t2
    k = x2 // (x1 + x2)
    res = 2 * k - 1
    if x2 % (x1 + x2) == 0: res -= 1
    return res

if __name__ == '__main__':
    print(sumitb2019_f())

Submission status: Wrong Answer

Solution code:
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
if a1>b1 and a2>b2:
    print(0)
elif b1>a1 and b2>a2:
    print(0)
elif a1*t1+a2*t2==b1*t1+b2*t2:
    print("infinity")
else:
    ans=-1
    num2=-1
    num3=-1
    if a1*t1+a2*t2>b1*t1+b2*t2:
        if a1>b1:
            ans=0
        else:
            num2=(b1-a1)*t1
            num3=((a1-b1)*t1)+((a2-b2)*t2)
            ans=(num2//num3)*2+1
    else:
        if b1>a1:
            ans=0
        else:
            num2=(a1-b1)*t1
            num3=((b1-a1)*t1)+((b2-a2)*t2)
            ans=(num2//num3)*2+1
    print(ans)

Submission status: Wrong Answer

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 B1<A1 and B1*T1+B2*T2>A1*T1+A2*T2:
    k = ((A1-B1)*T1)//(B1*T1+B2*T2-A1*T1-A2*T2)
    print(2*k+1)
elif A1<B1 and A1*T1+A2*T2>B1*T1+B2*T2:
    k = ((B1-A1)*T1)//(A1*T1+A2*T2-B1*T1-B2*T2)
    print(2*k+1)
else:
    print(0)

Submission status: Wrong Answer

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 B1<A1 and B1*T1+B2*T2>A1*T1+A2*T2:
    k = (A1-B1)*T1//(B1*T1+B2*T2-A1*T1-A2*T2)
    print(2*k+1)
elif A1<B1 and A1*T1+A2*T2>B1*T1+B2*T2:
    k = (B1-A1)*T1//(A1*T1+A2*T2-B1*T1-B2*T2)
    print(2*k+1)
else:
    print(0)