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:
t1,t2,a1,a2,b1,b2=map(int,open(0).read().split())
w,x,y,z=t1*a1,t2*a2,t1*b1,t2*b2
if w+x==y+z:
	print("infinity")
	exit()
if (w-y)*(w+x-y-z)>0:
	print(0)
	exit()
a1=min(w,y)
b1=max(w,y)
c1=max(w+x,y+z)
c2=min(w+x,y+z)
print(int(min((b1-a1)/(c1-c2),(b1-c1)/(c2-c1)))+int(min(b1/(c1-c2),(a1-b1)/(c2-c1)))+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000000)
from itertools import count
from functools import lru_cache
from collections import defaultdict
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok
#


def main():
    T1,T2 = mis()
    A1,A2 = mis()
    B1,B2 = mis()
    d1 = T1*A1 - T1*B1
    d2 = T2*A2 - T2*B2
    if d1 + d2 == 0:
        print('infinity')
    elif d1 < 0 > d2 or d1 > 0 < d2:
        print(0)
    else:
        if d1 < 0:
            d1 *= -1
            d2 *= -1
        if d1 % (d1+d2) == 0:
            print(d1 // abs(d1+d2) * 2)
        else:
            print(d1 // abs(d1+d2) * 2 + 1)




main()

Submission status: Wrong Answer

Solution code:
T1,T2=list(map(int,input().split()))
A1,A2=list(map(int,input().split()))
B1,B2=list(map(int,input().split()))
BXA = 0
BXB = 0
XA = 0
XB = 0
ans = 0
for i in range(10**5):
    XA = T1*A1 + BXA
    XB = T1*B1 + BXB
    if (XA-XB)*(BXA-BXB)<0 or XA==XB:
        ans+=1
    BXA = XA
    BXB = XB
    XA = T2*A2 + BXA
    XB = T2*B2 + BXB
    if (XA-XB)*(BXA-BXB)<0 or XA==XB:
        ans+=1
    BXA = XA
    BXB = XB
if ans > 10**4:
    print("infinity")
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
d1 = (b1 - a1) * t1
d2 = (b2 - a2) * t2
if d1 + d2 == 0:
    print('infinity')
    exit()
if (d1 > 0) ^ (d2 < 0):
    print(0)
    exit()

ok, ng = 0, 10**20
if d1 > 0:
    d1 = -d1
    d2 = -d2
while ng - ok > 1:
    m = (ok + ng) // 2
    if m * d2 + (m + 1) * d1 <= 0:
        ok = m
    else:
        ng = m
print(2 * ok + 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())

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

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

if al1+al2==bl1+bl2:
    print("infinity")
else:
    if (l1-l3)%(l1+l2-l3-l4)==0:
        ans=2*(l1-l3)/(l3+l4-l1-l2)
        print(ans)
    else:
        ans=max(0,2*int((l1-l3)/(l3+l4-l1-l2))+1)
        print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000000)
from itertools import count
from functools import lru_cache
from collections import defaultdict
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok
#


def main():
    T1,T2 = mis()
    A1,A2 = mis()
    B1,B2 = mis()
    d1 = T1*A1 - T1*B1
    d2 = T2*A2 - T2*B2
    if d1 + d2 == 0:
        print('infinity')
    elif d1 < 0 > d2 or d1 > 0 < d2:
        print(0)
    else:
        if d1 < 0:
            d1 *= -1
            d2 *= -1
        if d1 % (d1+d2) == 0:
            print('wari')
            print(d1 // abs(d1+d2) * 2)
        else:
            print(d1 // abs(d1+d2) * 2 + 1)




main()

Submission status: Wrong Answer

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]
suma = a1+a2
sumb = b1+b2
if suma==sumb:
    print("infinity")
elif ((a1-b1)*(a2-b2))>0:
    print(0)
else:
    if sumb>suma:
        a1,b1 = b1,a1
        a2,b2 = b2,a2
        suma,sumb = sumb,suma
    k = (b1-a1)//(suma-sumb)
    ans = 2*k+1
    if (b1-a1)%(suma-sumb)==0:
        ans-=1
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
d1 = (b1 - a1) * t1
d2 = (b2 - a2) * t2
if d1 + d2 == 0:
    print('infinity')
    exit()
if (d1 > 0) ^ (d2 < 0):
    print(0)
    exit()

ok, ng = 0, 10**10
if d1 > 0:
    d1 = -d1
    d2 = -d2
while ng - ok > 1:
    m = (ok + ng) // 2
    if m * d2 + (m + 1) * d1 <= 0:
        ok = m
    else:
        ng = m
print(2 * ok + 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())

P=(B1-A1)*T1
Q=(B2-A2)*T2

if P+Q==0:
    print("infinity")

else:
    #print(P,Q)
    print(-P//(P+Q)*2+1)

Submission status: Wrong Answer

Solution code:
import numpy as np
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
c1 = b1-a1
c2 = b2-a2
s = t1*c1+t2*c2
if s==0:
    print("infinity")
    exit()
mx = max([np.abs(t1*c1),np.abs(t2*c2)])
n = np.abs(int(mx/s))

print(2*n-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())

if (a1<b1 and a2<b2) or (a1>b1 and a2>b2):
    print(0)
    exit()

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

_b1 = b1 - a1
_a2 = a2 - b2

v1 = t1*_b1
v2 = t2*_a2

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

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

d = v2 - v1
k = v1 // d

ans = k*2 + 1
if v2*k == v1*(k+1):
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy

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

v1=b1-a1
v2=b2-a2
#print(v1,v2)
x1=v1*t1
x2=v2*t2
#print(x1,x2)
if numpy.sign(x1)==numpy.sign(x2):
    #print("one")
    print(0)
elif abs(x2)-abs(x1)<0:
    #print("two")
    print(0)
elif -x1==x2:
    print("infinity")
else:
    #print("hello")
    #print(x1,2*(abs(x2)-abs(x1)))
    print(2*(x1//(abs(x2)-abs(x1)))+1)

Submission status: Wrong Answer

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

# st = Counter(raw_input())

MOD=1000000007

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


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


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

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


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

    # print('T: %d + %d = %d' % (T1,T2,T))
    # print('  a: %d + %d = %d' % (T1*A1, T2*A2, az))
    # print('  b: %d + %d = %d' % (T1*B1, T2*B2, bz))
    # # az < bz
    # print('  %d k + %d > %d k + %d' % (az, T1*A1, bz, T1*B1))
    # print('  %d > %d k ' % ( T1*(A1-B1), (bz-az)))

    ans = 0

    # print('[[ %d / %d ]] %g' % (az, bz, az/bz))

    numer = T1*(A1-B1)
    denom = (bz-az)
    # print('[[ %d / %d ]] %g' % (numer, denom, numer/denom))
    # if (numer % denom == 0):
    #     met = numer / denom
    #     if met >= 0:
    #         # print('met at %d' % met)
    #         ans += 1

    if A1 > B1:
        # print('A1 > B1')
        # numer = T1*(A1-B1)
        # denom = T*(bz-az)
        cross = int(numer / denom)
        if cross > 0:
            # print('cross %d times' % cross)
            ans += cross

    # if A2 < B2:
    if A1 > B1:
        cross = int(numer / denom) + 1
        if cross > 0:
            # print('cross %d times' % cross)
            ans += cross

    if numer/denom == int(numer/denom):
        ans -= 1

    # kmax = calc_kmax(az,bz)
    # print('kmax=',kmax)
    # print('%d/%d <= %d/%d' % (az,bz, kmax,kmax+1))
    # print('%g <= %g' % (az / bz, kmax/(kmax+1)))

    # def f(x,y):
    #     if (x==y): return '='
    #     elif (x > y): return '>'
    #     else: return '<'

    # ai = bi = 0
    # for k in range(kmax+1):
    #     print('%d' % k)
    #     ai += T1*A1
    #     bi += T1*B1
    #     print('  %d %s %d' % (ai,f(ai,bi),bi))
    #     ai += T2*A2
    #     bi += T2*B2
    #     print('  %d %s %d' % (ai,f(ai,bi),bi))

    print(ans)


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

Submission status: Wrong Answer

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

    dist1 = a1*t1 + a2*t2
    dist2 = b1*t1 + b2*t2
    if (a1 < b1 and a2 < b2) or (b1 < a1 and b2 < a2): 
        return 0
    elif dist1 == dist2:
        return "infinity"

    if dist1 > dist2:
        a = a1*t1
        b = a2*t2
        c = b1*t1
        d = b2*t2
    else:
        a = b1*t1
        b = b2*t2
        c = a1*t1
        d = a2*t2
    
    m = (c-a) // (a+b-c-d)
    out = 2 * m + 1

    return out

out = main()
print(out)

Submission status: Wrong Answer

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

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  T1,T2=LI()
  A1,A2=LI()
  B1,B2=LI()
  x=A1*T1
  y=B1*T1
  
  if x<=y:
    f=0
  else:
    f=1

  ans=0
  for i in range(300010):
    if i%2==0:
      x+=A2*T2
      y+=B2*T2
    else:
      x+=A1*T1
      y+=B1*T1

    # print(f,x,y)
    if f==0 and (x>=y):
      ans+=1
      f=1
    elif f==1 and (x<=y):
      ans+=1
      f=0

  if ans==300010:
    return 'infinity'
  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
t1,t2 = list(map(int, input().split()))
a1,a2 = list(map(int, input().split()))
b1,b2 = list(map(int, input().split()))
x=(a1-b1)*t1 + (a2-b2)*t2
#print(x)
#print((a1-b1)*t1)
if x*(a1-b1) < 0:
    r = abs((a1-b1)*t1)//abs(x)
else:
    r = 0

#print((a2-b2)*t2)
if x*(a2-b2) > 0:
    r += abs((a2-b2)*t2)//abs(x)
else:
    r += 0
x=(a1-b1)*t1 + (a2-b2)*t2
if x == 0:
    print("infinity")
else:
    print(r)

Submission status: Wrong Answer

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

X_up = (A1 - B1) * T1
X_dn = (A2 - B2) * T2

if X_up + X_dn == 0:
    if X_up == 0:
        print(0)
    else:
        print("infinity")
else:
    if X_up + X_dn < 0 < X_up:
        grad = -(X_up + X_dn)
        ans = 2 * ((X_up + (grad - 1)) // grad) - 1
    elif X_up < 0 < X_up + X_dn:
        grad = X_up + X_dn
        ans = 2 * ((-X_up + (grad - 1)) // grad) - 1
    else:
        ans = 0
    #
    print(max(0, ans))

Submission status: Wrong Answer

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

def getEnd(mid, f):
    c = mid % 2
    if f == 1:
        return (a1 * t1) * ((mid / 2) + c) + (a2 * t2) * (mid / 2)
    else:
        return (b1 * t1) * ((mid / 2) + c) + (b2 * t2) * (mid / 2)

def get(mid):
    c = mid % 2
    Y1 = getEnd(mid, 1)
    X1 = getEnd(mid - 1, 1)

    Y2 = getEnd(mid, 2)
    X2 = getEnd(mid - 1, 2)

    if X1 < X2:
        if Y1 >= Y2:
            return True
    elif X1 == X2:
        return True
    else:
        if Y1 <= Y2:
            return True
    return False


def check(mid):
    if mid == 1:
        return True
    if get(mid):
        return True
    return False

s = 1
e = 10**18
it = 100
while it:
    mid = (s + e) / 2
    it -= 1
    if check(mid):
        s = mid
    else:
        e = mid
res = -1
for i in range(mid - 5, mid + 5):
    if i >= 1:
        if check(i):
            res = i
if res >= 10**15:
    print 'infinity'
else:
    print res - 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())

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

if A2>B2:
  print(0)
  exit()

x1,x2=(A1-B1)*T1,(B2-A2)*T2
#print(x1,x2)
if x1-x2>0:
  print(0)
elif x1-x2==0:
  print("infinity")
else:
  d=x1-x2
  print(-(x1//d)*2-1)

Submission status: Wrong Answer

Solution code:
import numpy

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

v1=b1-a1
v2=b2-a2
print(v1,v2)
x1=v1*t1
x2=v2*t2
print(x1,x2)
if numpy.sign(x1)==numpy.sign(x2):
    #print("one")
    print(0)
elif abs(x2)-abs(x1)<0:
    #print("two")
    print(0)
elif -x1==x2:
    print("infinity")
else:
    #print("hello")
    print(x1,2*(abs(x2)-abs(x1)))
    print(2*(x1//(abs(x2)-abs(x1)))+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())

if a1 < b1 and a2 < b2:
    print(0)
elif a2 > b2 and a1 > b1:
    print(0)
elif t1*a1+t2*a2-t1*b1-t2*b2 == 0:
    print('infinity')
else:
    sa = abs(t1*a1+t2*a2-t1*b1-t2*b2)
    if t1*a1+t2*a2-t1*b1-t2*b2 > 0:
        a1,a2,b1,b2 = b1,b2,a1,a2
    kijyun = 0
    if t1*a1 - t1*b1 > t2*a2 - t2 * b2:
        kijyun = t1*a1 - t1*b1
    vecsa = max(t1*a1 - t1*b1,t2*a2 - t2 * b2)
    ans = 0
    l = -1;r = 3*10**10
    cost = -1;n = -1
    while l + 1 < r:
        n = (l + r)//2
        cost = vecsa - n*sa#+kijyun
        if cost >= 0:
            l = n
        elif cost == 0:
            break
        else:r = n
    l *= 2
    if kijyun != 0:
        l += 1
    print(l)
    """
    sa1 = max(0,t1*a1+t2*a2 - t1*b1)
    sa2 = max(0,t1*b1+t2*b2-t1*a1)
    print(sa1//sa + sa2//sa)
    """

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import itertools
import sys

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

taka = t1*a1
aoki = t1*b1
ans = 0
if(t1*a1+t2*a2==t1*b1+t2*b2):
    print("infinity")
    quit()

top = 0
if(taka>aoki):
    top = 0
else:
    top = 1
for i in range(2,500000):
#    prev_taka = taka
#    prev_aoki = aoki
    prev_top = top
    if(i%2==1):
        taka += t1*a1
        aoki += t1*b1
    else:
        taka += t2*a2
        aoki += t2*b2
    if(taka>aoki):
        top = 0
    else:
        top = 1
    if(prev_top != top):
        ans += 1
    
#    print("taka",taka)
#    print("aoki",aoki)

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

v1, v2 = b1 - a1, b2 - a2
x1, x2 = t1 * v1, t2 * v2
if x1 + x2 == 0:
  print('infinity')
else:
  if x1 < 0:
    x1, x2 = [x1 * -1, x2 * -1]

  if x1 + x2 > 0:
    print(0)
  else:
    dx = abs(x1 + x2)
    c = x1 // dx
    print(c * 2 + 1)

Submission status: Wrong Answer

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

mugen = "infinity"
a1 = A1*T1
a2 = A2*T2
b1 = B1*T1
b2 = B2*T2

if a1+a2 > b1+b2:
    if a1 > b1:
        print(0)
    elif a1 < b1:
        n = (a2-b2)//((a1+a2)-(b1+b2))
        if (a2-b2)%((a1+a2)-(b1+b2)) == 0:
            print(2*n-2)
        else:
            print(2*n-1)
    else:
        print(1)
            
elif a1 + a2 < b1 + b2:
    if a1 < b1:
        print(0)
    elif a1 > b1:
        n = -(a2-b2)//((a1+a2)-(b1+b2))
        if -(a2-b2)%((a1+a2)-(b1+b2)) == 0:
            print(2*n-2)
        else:
            print(2*n-1)
    else:
        print(1)
            
else:
    print(mugen)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import itertools
import sys

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

taka = t1*a1
aoki = t1*b1
ans = 0
if(t1*a1+t2*a2==t1*b1+t2*b2):
    print("infinity")
    sys.exit()

top = 0
if(taka>aoki):
    top = 0
else:
    top = 1
for i in range(2,500000):
#    prev_taka = taka
#    prev_aoki = aoki
    prev_top = top
    if(i%2==1):
        taka += t1*a1
        aoki += t1*b1
    else:
        taka += t2*a2
        aoki += t2*b2
    if(taka>aoki):
        top = 0
    else:
        top = 1
    if(prev_top != top):
        ans += 1
    
#    print("taka",taka)
#    print("aoki",aoki)

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

def solve() :
    ret = -float('inf')
    
    if T1 * A1 + T2 * A2 == T1 * B1 + T2 * B2 :
        return 'infinity'
        
    else :
        a = T1 * A1
        b = T1 * B1
        d1 = a - b
        
        a += T2 * A2
        b += T2 * B2
        d2 = b - a
        if d2 < 0 :
            return 0
            
        a += T1 * A1
        b += T1 * B1
        d3 = a - b
        if d3 < 0 :
            return 1
            
        a += T2 * A2
        b += T2 * B2
        d4 = b - a
        if d4 < 0 :
            return 2
        
        if d3 < d1 :
            d31 = d1 - d3
            x = d1 // d31
            if d1 - x * d31 == 0 :
                x -= 1
            return x * 2 + 1
        else :
            d42 = d4 - d2
            x = d2 // d42
            if d2 - x * d42 == 0 :
                x -= 1
            return x * 2 + 2

print(solve())

Submission status: Wrong Answer

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

if (A_1==B_1 and A_2==B_2) or (T_1==T_2 and A_1==B_2 and A_2==B_1):
    print('infinity')
elif (A_1>B_1 and A_2>B_2) or (A_1<B_1 and A_2<B_2):
    print(str(0))
else:
    C=0
    T=T_1
    A=A_1*T
    B=B_1*T
    L_A=A
    L_B=B
    for i in range(2,100001):
        if i%2==0:
            T=T_2
            A=A+A_2*T
            B=B+B_2*T
        else:
            T=T_1
            A=A+A_1*T
            B=B+B_1*T
        if L_A>L_B and A<=B:
            C=C+1
            L_A=A
            L_B=B
        elif L_A<L_B and A>=B:
            C=C+1
            L_A=A
            L_B=B
        else:
            L_A=A
            L_B=B
    if C>=50000:
        print('infinity')
    else:
        print(C)

Submission status: Wrong Answer

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

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  T1,T2=LI()
  A1,A2=LI()
  B1,B2=LI()
  x=A1*T1
  y=B1*T1
  
  if x<=y:
    f=0
  else:
    f=1

  ans=0
  for i in range(100010):
    if i%2==0:
      x+=A2*T2
      y+=B2*T2
    else:
      x+=A1*T1
      y+=B1*T1

    # print(f,x,y)
    if f==0 and (x>=y):
      ans+=1
      f=1
    elif f==1 and (x<=y):
      ans+=1
      f=0

  if ans==100010:
    return 'infinity'
  return ans

# main()
print(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())

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

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

if L % d == 0:
    print(L//d*2)
    exit()
else:
    if A1 < B1:
        A1, A2, B1, B2 = B1, B2, A1, A2
    if (A1-B1)*T1 > (B2-A2)*T2:
        print(0)
        exit()
    else:
        ans = L//d*2+1
    # if d*L//d <= L:
    #     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())
t_ave = (T1*A1+T2*A2)/(T1+T2)
a_ave = (T1*B1+T2*B2)/(T1+T2)
t_D1 = T1*A1
t_D2 = T2*A2
a_D1 = T1*B1
a_D2 = T2*B2
if t_D1+t_D2==a_D1+a_D2:
    print("infinity")
else:
    if (t_D1>a_D1 and t_D2>a_D2) or (t_D1<a_D2 and t_D2 <=a_D2 ):
        print(0)
    else:
        print(int(abs((t_D1-a_D1)/(t_ave-a_ave))))

Submission status: Wrong Answer

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

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

if(((A1<B1)and(T1*A1+T2*A2>T1*B1+T2*B2)) or ((A1>B1)and(T1*A1+T2*A2<T1*B1+T2*B2))):
    
    sa = abs(T1*A1-T1*B1)
    sa2 = abs(T1*A1+T2*A2-T1*B1-T2*B2)
    count = (sa//sa2)*2+1
    
elif(T1*A1+T2*A2==T1*B1+T2*B2):
    count = "infinity"
else:
    count = 0
    
print(count)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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+A2 > B1+B2: # Aを遅く
    A1,B1 ,A2,B2 = B1,A1 ,B2,A2

if A1+A2 == B1+B2:
    print("infinity")
    exit()

def nibu(l,r):
    if l >= r:
        return r
    c = (l+r+1)//2
    if (A1+A2)*(c-1)+A1 >= (B1+B2)*(c-1)+B1:
        return nibu(c,r)
    else:
        return(nibu(l,c-1))

def nibu2(l,r):
    if l >= r:
        return r
    c = (l+r+1)//2

ans = nibu(0,10**100)
if (A1+A2)*(ans-1)+A1 == (B1+B2)*(ans-1)+B1:
    print(ans*2-2)
else:
    print(ans*2-1)

Submission status: Wrong Answer

Solution code:
import sys

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("intfinity")
    sys.exit()

a, b = 0, 0
key = 0
ans = 0
while True:
    key = ans
    while True:
        a_past, b_past = a, b
        a += a1*t1
        b += b1*t1
        if a_past == b_past:
            break
        if (a_past-b_past)*(a-b) <= 0:
            ans += 1
        break
    while True:
        a_past, b_past = a, b
        a += a2*t2
        b += b2*t2
        if a_past == b_past:
            break
        if (a_past-b_past)*(a-b) <= 0:
            ans += 1
        break
    if key == ans:
        break
print(ans)

Submission status: Wrong Answer

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

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

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

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

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

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

if L % d == 0:
    print(L//d*2)
    exit()
else:
    if A1 < B1:
        A1, A2, B1, B2 = B1, B2, A1, A2
    if (A1-B1)*T1 > (B2-A2)*T2:
        print(0)
        exit()
    else:
        ans = L//d*2+1
    # if d*L//d <= L:
    #     ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import math


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

# T1,T2以内に出会えるか, 出会えないなら一生無理
x = (T1*B1 - T1*A1) / (A2 - B2)
if x < 0 or x > T2:
    print(0)
    exit()

times = [T1+x]
dis_a = T1*A1 + T2*A2
dis_b = T1*B1 + T2*B2
T = T1 + T2
# T3で出会うか
x = (dis_b - dis_a) / (A1 - B1)
if 0 < x <= T1:
    times.append(T + x)

# T4で出会うか
x = (dis_b + T1*B1 - dis_a - T1*A1) / (A2 - B2)
if 0 < x <= T2:
    times.append(T + T1 + x)

# T5で出会うか
x = (dis_b*2 - dis_a*2) / (A1 - B1)
if 0 < x <= T1:
    times.append(T*2 + x)

# T6で出会うか
x = (dis_b*2 + T1*B1 - dis_a*2 - T1*A1) / (A2 - B2)
if 0 < x <= T2:
    times.append(T*2 + T1 + x)

## T7で出会うか
#x = (dis_b*3 - dis_a*3) / (A1 - B1)
#if 0 < x <= T1:
#    times.append(T*3 + x)
#
## T8で出会うか
#x = (dis_b*3 + T1*B1 - dis_a*3 - T1*A1) / (A2 - B2)
#if 0 < x <= T2:
#    times.append(T*3 + T1 + x)

dis_times = []
for i in range(len(times)-1):
    dis_times.append(times[i+1] - times[i])

if len(dis_times) == 0:
    print(len(times))
    exit()
if dis_times[0] == dis_times[1]:
    print('infinity')
    exit()
if dis_times[1] > dis_times[3]:
    x = dis_times[1] / (dis_times[1] - dis_times[3])
    print(math.ceil(x*2)+1)
    exit()
else:
    x = dis_times[0] / (dis_times[0] - dis_times[2])
    print(math.ceil(x*2))
    exit()

Submission status: Wrong Answer

Solution code:
def main():
    # input
    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
    diff = x+y

    # print(x)
    # print(y)
    # print(diff)

    if x == -y:
        print("infinity")
        return
    elif x*y > 0:
        print(0)
        return

    if diff > 0: # aが先行
        if x<0:
            ans = x//diff
        else:
            ans = y//diff

    else: # bが先行
        if x>0:
            ans = x//diff
        else:
            ans = y//diff

    print(-ans*2)



    a = a1*t1
    b = b1*t1


    # calc


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from math import floor

def solve():
    T1, T2 = map(int, input().split())
    A1, A2 = map(int, input().split())
    B1, B2 = map(int, input().split())
    C1 = T1*(A1-B1)
    C = C1 + T2*(A2-B2)
    if C == 0:
        return 'infinity'
    elif C1/C >= 0:
        return '0'
    elif C1/C >= -1:
        return '1'
    else:
        res =  2*(floor(-C1/C)+1)-1
        return str(res)



    
    return str(sum(L3))

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

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

Submission status: Wrong Answer

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+A2 > B1+B2: # Aを遅く
    A1,B1 ,A2,B2 = B1,A1 ,B2,A2

if A1+A2 == B1+B2:
    print("Infinity")
    exit()

def nibu(l,r):
    if l >= r:
        return r
    c = (l+r+1)//2
    if (A1+A2)*(c-1)+A1 >= (B1+B2)*(c-1)+B1:
        return nibu(c,r)
    else:
        return(nibu(l,c-1))

def nibu2(l,r):
    if l >= r:
        return r
    c = (l+r+1)//2

ans = nibu(0,10**20)
if (A1+A2)*(ans-1)+A1 == (B1+B2)*(ans-1)+B1:
    print(ans*2-2)
else:
    print(ans*2-1)

Submission status: Wrong Answer

Solution code:
t1,t2,a1,a2,b1,b2=map(int,open(0).read().split())
w,x,y,z=t1*a1,t2*a2,t1*b1,t2*b2
if w+x==y+z:
	print("infinity")
	exit()
if (w-y)*(w+x-y-z)>0:
	print(0)
	exit()
a1=min(w,y)
b1=max(w,y)
c1=max(w+x,y+z)
c2=min(w+x,y+z)
print(int(min((b1-a1)/(c1-c2),(b1-c1)/(c2-c1)))*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())
t_ave = (T1*A1+T2*A2)/(T1+T2)
a_ave = (T1*B1+T2*B2)/(T1+T2)
t_D1 = T1*A1
t_D2 = T2*A2
a_D1 = T1*B1
a_D2 = T2*B2
if t_D1+t_D2==a_D1+a_D2:
    print("infinity")
else:
    if (t_D1>a_D1 and t_D2>a_D2) or (t_D1<a_D2 and t_D2 <=a_D2 ):
        print(0)
    else:
        print((t_D1-a_D1)//(t_ave-a_ave))