码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 10170 - The Hotel with Infinite Rooms

时间:2014-09-29 20:56:21      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   sp   2014   c   on   log   r   

题目:求从s开始的递增序列(每次加1),求出他们加和不小于D的那个最后的加数。

分析:数学题,分治。s + s+1 + ... + n = n*(n+1)/2 - s*(s-1)/2 = (n+s)*(n-s+1)/2。

             直接二分答案即可(二分范围0~10^8)。

说明:(⊙_⊙)。

#include <iostream>
#include <cstdlib> 

using namespace std;

long long sum(long long s, long long n)
{
	return (n-s+1LL)*(n+s)/2LL;
}

long long bs(int S, long long D)
{
	long long mid,l = 1LL,r = 100000000LL;
	while (l < r) {
		mid = l+(r-l)/2LL;
		if (sum(S, mid) >= D)
            r = mid;  
        else l = mid+1LL;  
	} 
	return r;
}

int main()
{
	long long s,D;
	while (cin >> s >> D)
		cout << bs(s, D) << endl;
	
	return 0;
}

UVa 10170 - The Hotel with Infinite Rooms

标签:blog   io   os   sp   2014   c   on   log   r   

原文地址:http://blog.csdn.net/mobius_strip/article/details/39673443

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!