比赛 20151019 评测结果 AAAAAAAAAA
题目名称 爬山 最终得分 100
用户昵称 mikumikumi 运行时间 0.003 s
代码语言 C++ 内存使用 0.31 MiB
提交时间 2015-10-19 21:08:32
显示代码纯文本
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long LL;
LL n,d,a,b;
void read()
{
	scanf("%lld%lld%lld%lld",&n,&d,&a,&b);//lld
}
bool check(LL x)
{
	LL to=(x-a);
	if(to>0) to=(to-1)/d+1;
	else to=(to+1)/d-1;
	LL r=(x-b);
	if(r>0) r=(r-1)/d+1;
	else r=(r+1)/d-1;
	//cout<<x<<" "<<b<<" "<<d<<" "<<to<<" "<<r<<" "<<(x-b-1)<<endl;
	if(to+r<n) return 1;
	return 0;
}
void work()
{
	LL l=a,r=n*d+a;
	while(l<r)
	{
		LL mid=(r+l)/2;
		if(check(mid)) l=mid+1;
		else r=mid;
		//cout<<mid<<" "<<check(mid)<<endl;
	}
	while(!check(r)) r--;
	printf("%lld",r);
}
int main()
{
	freopen("mountain.in","r",stdin);
	freopen("mountain.out","w",stdout);
	read();
	work();
	return 0;
}