結果

問題 No.281 門松と魔法(1)
ユーザー ciel
提出日時 2020-02-09 01:03:21
言語 Crystal
(1.14.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 662 bytes
コンパイル時間 11,850 ms
コンパイル使用メモリ 297,688 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-30 20:02:55
合計ジャッジ時間 13,509 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve_short(d,a,b,c)
	begin
		r=0_i64
		if a==c
			c=[0,c-d].max
			r+=1
		end
		t=b-[a,c].min+1
		if t>0
			cnt=(t+d-1)//d
			r+=cnt
			b=[0,b-r*d].max
		end
		a==b || b==c ? -1 : r
	rescue DivisionByZeroError
		-1
	end
end
def solve_tall(d,a,b,c)
	begin
		r=0_i64
		t=a-b+1
		if t>0
			cnt=(t+d-1)//d
			r+=cnt
			a=[0,a-cnt*d].max
		end
		t=c-b+1
		if t>0
			cnt=(t+d-1)//d
			r+=cnt
			c=[0,c-cnt*d].max
		end
		if a==c
			c=[0,c-d].max
			r+=1
		end
		a==b || b==c || c==a ? -1 : r
	rescue DivisionByZeroError
		-1
	end
end
d,a,b,c=4.times.map{gets.not_nil!.to_i}.to_a
x=solve_short(d,a,b,c)
y=solve_tall(d,a,b,c)
puts x>=0&&y>=0 ? [x,y].min : [x,y].max
0