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:
T = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))


da, db = 0, 0
for i in range(2):
    da += T[i] * A[i]
    db += T[i] * B[i]

if da == db:
    print("infinity")
    exit()

print(max(0, (B[0] - A[0]) * T[0] // (da - db) * 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())

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 ev:
    print((ok-1) * 2)
else:
    print(ok * 2 - 1)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
t=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
at1=a[0]*t[0]
at2=a[1]*t[1]
bt1=b[0]*t[0]
bt2=b[1]*t[1]
if at1+at2==bt1+bt2:
    print("infinity")
    quit()

if at1>bt1:
    if at2>bt2:
        print(0)
        quit()
    sa1=at1-bt1
    sa2=bt1+bt2-at1-at2
    # print(sa1//sa2*2+1)
else:
    if bt2>at2:
        print(0)
        quit()
    sa1=bt1-at1
    sa2=-(bt1+bt2-at1-at2)
    # print(sa1//sa2*2+1)
if sa1%sa2==0:
    print(sa1//sa2*2)
else:
    print(sa1//sa2*2+1)

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



    a = a1*t1
    b = b1*t1


    # calc


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

x = (a1-b1) * t1
y = x + (a2-b2) * t2
#print(x, y)

def p(x, y):
    if x%y == 0:
        return abs(x//y)
    else:
        return abs(x//y) + 1

if y == 0:
    ans = 'infinity'
else:
    if x < 0 and y > 0 :
        ans = p(x, y) * 2 - 3
    elif x > 0 and y < 0 :
        ans = p(x, y) * 2 - 2
    else:
        ans = 0

print(ans)

Submission status: Wrong Answer

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
T_1, T_2 = map(int, input().split())
A_1, A_2 = map(int, input().split())
B_1, B_2 = map(int, input().split())
flag = 0
ans = 0
if T_1 * A_1 + T_2 * A_2 == T_1 * B_1 + T_2 * B_2:
    print("infinity")
else:
    # 速いほうをＡと仮定
    if T_1 * A_1 + T_2 * A_2 < T_1 * B_1 + T_2 * B_2:
        A_1, A_2, B_1, B_2 = B_1, B_2, A_1, A_2
    if A_1 < B_1:
        sa_12 = T_1 * A_1 + T_2 * A_2 - (T_1 * B_1 + T_2 * B_2)  # 1サイクルでできる差
        sa_1 = -T_1 * A_1 + T_1 * B_1  # T_1でできる差
        kari = math.ceil(sa_1 / sa_12)
        ans = (kari-1)*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 < 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
    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:
from math import ceil
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
a1 -= b1
a2 -= b2

if t1*a1 + t2*a2 == 0:
    print("infinity")
    exit()
if a1*a2 > 0:
    print(0)
    exit()
a1 *= t1
a2 *= t2
if a1 > 0:
    x = -a1/(a1+a2)
    n = ceil(x)
    if n == int(x):
        print(2*n)
    else:
        print(2*n-1)
if a2 > 0:
    x = -a1 / (a1 + a2)
    n = ceil(x)
    if n == int(x):
        print(2 * n)
    else:
        print(2 * n - 1)

Submission status: Wrong Answer

Solution code:
def main():
    t1, t2 = map(int, raw_input().split())
    a1, a2 = map(int, raw_input().split())
    b1, b2 = map(int, raw_input().split())
    ax1 = t1 * a1
    bx1 = t1 * b1
    ax2 = t2 * a2
    bx2 = t2 * b2
    if ax1 + ax2 == bx1 + bx2:
        print 'infinity'
        return
    if ax1 + ax2 < bx1 + bx2:
        ax1, bx1 = bx1, ax1
        ax2, bx2 = bx2, ax2
    init_d = bx1 - ax1
    if init_d < 0:
        print 0
        return
    total_delta = ax1 + ax2 - bx1 - bx2
    ans = 1 + (init_d / total_delta) * 2
    print ans

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 > 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**40)
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:
def main():
    import sys
    import math
    s,t=map(int,input().split())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    if a[0]<b[0]:
        a,b=b,a
    p=a[0]-b[0]
    q=a[1]-b[1]
    x,y=p*s,q*t

    if q>0 or x+y>0:
        print(0)
        sys.exit()
    elif x+y==0:
        print('infinity')
        sys.exit()


    y=abs(y)
    ans=math.floor(x/(y-x))*2+1
    print(ans)


 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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)
    ans=2*((y//x)+1)-1
    if y%x==0:
        ans-=1
    print(ans)
    

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

pos1, pos2 = 0,0
dis = 0
count=-1
if a1<b1:
    a1,a2,b1,b2 = b1,b2,a1,a2

sub = abs(t1*a1+t2*a2 - t1*b1+t2*b2)

if sub == 0:
    print("infinity")
elif a1>b1 and a2>b2:
    print(0)
else:
    while(-1*dis < (a1-b1)*t1):
        pos1+=t1*a1+t2*a2
        pos2+=t1*b1+t2*b2
        dis = pos1-pos2
        count+=2
    print(count)

Submission status: Wrong Answer

Solution code:
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
l = 0
r = 10**36
while l < r - 1:
	m = (l + r) // 2
	val1 = m // 2 * a2 * t2 + (m + 1) // 2 * a1 * t1
	val2 = (m + 1) // 2 * b1* t1 + m // 2 * b2 * t2
	val3 = (m + 1) // 2 * a2 * t2 + (m + 2) // 2 * a1 * t1
	val4 = (m + 2) // 2 * b1 * t1 + (m + 1) // 2 * b2 * t2
	if val1 <= val2 and val3 >= val4 or val1 >= val2 and val3 <= val4:
		l = m
	else:
		r = m
if r == 10 ** 36:
    print("infinity")
else:
    print(l)

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    t1, t2 = LI()
    a1, a2 = LI()
    b1, b2 = LI()
    if t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
        print("infinity")
        return
    if t1 * a1 > t1 * b1:
        if t1 * a1 + t2 * a2 > t1 * b1 + t2 * b2:
            print(0)
            return
        else:
            print(2 * (t1 * (a1 - b1) // (-t1 * (a1 - b1) + t2 * (b2 - a2))) + 1)
            return
    elif t1 * a1 < t1 * b1:
        if t1 * a1 + t2 * a2 < t1 * b1 + t2 * b2:
            print(0)
            return
        else:
            print(2 * (t1 * (b1 - a1) // (-t1 * (b1 - a1) + t2 * (a2 - b2))) + 1)
            return
    return


#main
if __name__ == '__main__':
    solve()

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

sa = A1 * T1
sb = B1 * T1
ea = A2 * T2
eb = B2 * T2
fa = sa + ea
fb = sb + eb

if fa < fb:
  if sa < sb:
    print(0)
  elif sa == sb:
    print(1)
  elif sa > sb:
    d1 = sa - sb
    di = fb - fa
    c = int(d1 / di) * 2 + 1
    print(c)
elif fa == fb:
  print("infinity")
elif fa > fb:
  if sa < sb:
    d1 = sb - sa
    di = fa - fb
    c = int(d1 / di) * 2 + 1
    print(c)
  elif sa == sb:
    print(1)
  elif sa > sb:
    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())

X1 = T1 * (A1 - B1)
X2 = T2 * (A2 - B2)

D = X1 + X2
if D == 0:
	print("infinity")
else:
	if X1 < 0:
		X1, X2 = [X2, X1]
	else:
		D = -D
	
	if D < 0:
		print("0")
	else:
		if X1 % D == 0:
			print((X1 // D) * 2)
		else:
			print((X1 // D) * 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())

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

# 最初から同じ速度の場合
if Adist1==Bdist1:
  print("infinity")
# Aが先に行ってしまってBが絶対おいつけない場合
elif Adist1>Bdist1 and Adist2>=Bdist2:
  print(0)
# Bが先に行ってしまってAが絶対おいつけない場合
elif Adist1<Bdist1 and Adist2<=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:
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())

pos1, pos2 = 0,0
dis = 0
count=-1
if a1<b1:
    a1,a2,b1,b2 = b1,b2,a1,a2

sub = abs(t1*a1+t2*a2 - t1*b1+t2*b2)

if sub == 0:
    print("infinity")
elif a1>b1 and a2>b2:
    print(0)
else:
    while(-1*dis < (a1-b1)*t1):
        pos1+=t1*a1+t2*a2
        pos2+=t1*b1+t2*b2
        dis = pos1-pos2
        count+=2
        
print(count)

Submission status: Wrong Answer

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


da, db = 0, 0
for i in range(2):
    da += T[i] * A[i]
    db += T[i] * B[i]

if da == db:
    print("infinity")
    exit()

print((B[0] - A[0]) * T[0] // (da - db) * 2 + 1)

Submission status: Wrong Answer

Solution code:
t1, t2 = [int(i) for i in input().split()]
a1, a2 = [int(i) for i in input().split()]
b1, b2 = [int(i) for i in input().split()]
c = a1 * t1 + a2 * t2 - b1 * t1 - b2 * t2


def waru(a, b):
    a = max(0, a)
    if b < 0:
        return 0
    if a % b == 0:
        return a // b+1
    else:
        return a//b


first1 = a1 * t1
first2 = b1 * t1
last1 = a2 * t2
last2 = b2 + t2


if c == 0:
    print("infinity")
    exit()

if first1 > first2:
    if c > 0:
        print(0)
        exit()
    if (first1 - first2) % c == 0:
        print((first1 - first2) // (-c) * 2)
    else:
        print(waru(first1 - first2, -c) * 2 + 1)
else:
    if c < 0:
        print(0)
        exit()
    if (first1 - first2) % c == 0:
        print(waru(first2 - first1, c) * 2)
    else:
        print(waru(first2 - first1, c) * 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())
if t1*a1+t2*a2==t1*b1+t2*b2:print("infinity");exit()
a1,a2=t1*a1,t2*a2
b1,b2=t1*b1,t2*b2
if a1+a2>b1+b2:a1,a2,b1,b2=b1,b2,a1,a2
d=b1+b2-a1-a2
print(max(int(0--(a1-b1+0.5)//d)*2-1,0))

Submission status: Wrong Answer

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

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

delta1 = A1 - B1
delta2 = A2 - B2

d = delta1 * T1 + delta2 * T2
T = T1 + T2

if d == 0:
    print("infinity")
    exit()

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

if delta1 * d > 0:
    print(0)
    exit()

#a = T1 * (T * delta1 - d) / T
Ta = T1 * (T * delta1 - d)
# t0 = a * (-1) * T / d
t0 = Ta * (-1) / d

Tnum = int((t0 - T1) // T)
ans = 2 * Tnum + 1

dt0 = Ta * (-1)

#if t0 == Tnum * T + T1:
if dt0 == d * (Tnum * T + T1):
    ans -= 1

print(max(ans, 0))

Submission status: Wrong Answer

Solution code:
import time
#n = int(input())
#a = list(map(int, input().split()))
#ans = 1
#for c in range(0, n):
#    i = n-c-1
#    p = i - a[i] + 1
#    ans*=p
#print(ans%1000000007
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())

if((t1*a1+t2*a2) == (t1*b1+t2*b2)):
    print("infinity")
    exit()
ta = 0
ao = 0
count = 0
flag1 = 0
flag2 = 0
while (flag2 < 2 and flag1 < 2):

    ta += t1*a1
    ao += t1*b1
    if (ta > ao):
        #print(str(ta) + " > " + str(ao))
        flag2 += 1
        flag1 = 0
    else:
        #print(str(ta) + " <= " + str(ao))
        flag1 += 1
        flag2 = 0
    ta += t2*a2
    ao += t2*b2
    if(ta > ao):
        #print(str(ta) + " > " +str(ao))
        flag2 += 1
        flag1 = 0
    else:
        #print(str(ta) + " <= " +str(ao))
        flag1 += 1
        flag2 = 0
    #time.sleep(0.5)
    count += 1
print(count*2-3)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
t=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
at1=a[0]*t[0]
at2=a[1]*t[1]
bt1=b[0]*t[0]
bt2=b[1]*t[1]
if at1+at2==bt1+bt2:
    print("infinity")
    quit()

if at1>bt1:
    if at2>bt2:
        print(0)
        quit()
    sa1=at1-bt1
    sa2=bt1+bt2-at1-at2
    print(sa1//sa2*2+1)
else:
    if bt2>at2:
        print(0)
        quit()
    sa1=bt1-at1
    sa2=-(bt1+bt2-at1-at2)
    print(sa1//sa2*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())

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

  if x1 > (x2 * -1):
    print(0)
  else:
    dx = abs(x1 + x2)
    c = x1 // dx
    print(c * 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())

c1 = a1 * t1
c2 = a2 * t2
d1 = b1 * t1
d2 = b2 * t2

if c1 + c2 == d1 + d2:
    print("infinity")
    exit(0)

if c1 + c2 < d1 + d2:
    c1, d1 = d1, c1
    c2, d2 = d2, c2

def check(mid):
    if mid % 2 == 0:
        a = (c1 + c2) * (mid // 2)
        b = (d1 + d2) * (mid // 2)
        f1 = a < b
        a += c1
        b += d1
        f2 = a < b
        return f1 != f2
    else:
        a = (c1 + c2) * (mid // 2) + c1
        b = (d1 + d2) * (mid // 2) + d1
        f1 = a < b
        a += c2
        b += d2
        f2 = a < b
        return f1 != f2

ok, ng = 0, 10 ** 20
while ng - ok > 1:
    mid = (ok + ng) // 2
    if check(mid):
        ok = mid
    else:
        ng = mid

print(ok)

Submission status: Wrong Answer

Solution code:
import math

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

c = [t[0]*a[0],t[1]*a[1]]
d = [t[0]*b[0],t[1]*b[1]]

if sum(d)>sum(c):
    if d[0]>=c[0]:
        print(0)
        exit()
    elif d[0]<c[0]:
        n = (c[0]-d[0])/(sum(d)-sum(c))
        if n==int(n):
            print(2*n+1)
        else:
            print(2*math.ceil(n)-1)
elif sum(c)>sum(d):
    if c[0]>=d[0]:
        print(0)
        exit()
    elif c[0]<d[0]:
        n = (c[0]-d[0])/(sum(d)-sum(c))
        if n==int(n):
            print(2*n+1)
        else:
            print(2*math.ceil(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:
    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
    ans = 0
    c = 0
    if cost == 0:c = 1
    if kijyun != 0:ans += (n - 1 - c)*2 + 1
    else:ans += (n - c) * 2
    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())
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
    while ok + 1 < ng:
        mid = (ok + ng) // 2
        s = mid * (d1 + d2)
        if s + d1 > 0 and s + d1 + d2 < 0:
            ok = mid
        else:
            ng = mid
    print(ng * 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())

L1 = t1*a1+t2*a2
L2 = t1*b1+t2*b2
if L1 == L2:
    print('infinity')
    exit()
flg = 1 if L1 < L2 else 0
ok = 0
ng = 10**15+1
mid = (ok + ng)//2
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
        break
    else:
        if flg:
            ng = mid
        else:
            ok = mid
    mid = (ok + ng)//2

print(ok * 2 - 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")
    sys.exit()

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

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    t1, t2 = LI()
    a1, a2 = LI()
    b1, b2 = LI()
    if t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
        print("infinity")
        return
    if t1 * a1 > t1 * b1:
        if t1 * a1 + t2 * a2 > t1 * b1 + t2 * b2:
            print(0)
            return
        else:
            print(2 * (t1 * (a1 - b1) // (t1 * (a1 - b1) - t2 * (b2 - a2))) + 1)
            return
    elif t1 * a1 < t1 * b1:
        if t1 * a1 + t2 * a2 < t1 * b1 + t2 * b2:
            print(0)
            return
        else:
            print(2 * (t1 * (b1 - a1) // (-t1 * (b1 - a1) + t2 * (a2 - b2))) + 1)
            return
    return


#main
if __name__ == '__main__':
    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())

d1=t1*a1+t2*a2
d2=t1*b1+t2*b2

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

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

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]
D1 = A1-B1
D2 = A2-B2
if D1*D2>0:
    output = "infinity"
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: Wrong Answer

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

delta1 = A1 - B1
delta2 = A2 - B2

d = delta1 * T1 + delta2 * T2
T = T1 + T2

if d == 0:
    print("infinity")
    exit()

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

if delta1 * d > 0:
    print(0)
    exit()

#a = T1 * (T * delta1 - d) / T
Ta = T1 * (T * delta1 - d)
# t0 = a * (-1) * T / d
t0 = Ta * (-1) / d

Tnum = int((t0 - T1) // T)
ans = 2 * Tnum + 1

dt0 = Ta * (-1)

#if t0 == Tnum * T + T1:
if dt0 == d * (Tnum * T + T1):
    ans -= 1

print(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
    X1 = getEnd(mid, 1)
    Y1 = getEnd(mid - 1, 1)

    X2 = getEnd(mid, 2)
    Y2 = 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())
d1 = (A1 - B1) * T1
d2 = d1 + (A2 - B2) * T2
if d1 * d2 > 0:
    print(0)
    exit()
if d2 == 0:
    print('infinity')
    exit()
d1 = abs(d1)
d2 = abs(d2)
if d1 / d2 == d1 // d2:
    print(d1, d2)
    print(d1 // d2 * 2)
else:
    print(1 + d1 // d2 * 2)

Submission status: Wrong Answer

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
	print(2*ans + 1)