結果

問題 No.281 門松と魔法(1)
ユーザー furafura
提出日時 2020-06-09 19:24:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 979 bytes
コンパイル時間 2,768 ms
コンパイル使用メモリ 191,928 KB
最終ジャッジ日時 2025-01-11 00:32:52
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 48 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

const long long INF=1LL<<61;

bool is_kadomatsu(lint a,lint b,lint c){
	if(a==b || a==c || b==c) return false;
	if(a<b && b>c) return true;
	if(a>b && b<c) return true;
	return false;
}

int main(){
	lint d,a,b,c; cin>>d>>a>>b>>c;

	if(d==0){
		puts(is_kadomatsu(a,b,c)?"0":"-1");
		return 0;
	}

	lint ans=INF;
	// b が最大値になるケース
	{
		lint ta=a,tb=b,tc=c;

		lint res=0;
		if(a>=b){
			lint k=(a-b)/d+1;
			res+=k;
			a-=k*d;
		}
		if(c>=b){
			lint k=(c-b)/d+1;
			res+=k;
			c-=k*d;
		}
		if(a==c) a-=d, res++;
		if(a>=0 && b>=0 && c>=0){
			ans=min(ans,res);
		}

		a=ta; b=tb; c=tc;
	}
	// b が最小値になるケース
	{
		lint res=0;
		if(a==c) a-=d, res++;
		if(b>=min(a,c)){
			lint k=(b-min(a,c))/d+1;
			res+=k;
			b-=k*d;
		}
		if(a>=0 && b>=0 && c>=0){
			ans=min(ans,res);
		}
	}
	printf("%lld\n",ans<INT_MAX?ans:-1);

	return 0;
}
0