UVa 10170 - The Hotel with Infinite Rooms

题目:求从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;
}
时间: 2024-08-01 22:46:45

UVa 10170 - The Hotel with Infinite Rooms的相关文章

简单的酒店订房系统

简单的酒店订房系统 一个不能算作项目的项目,就是一个简单的酒店的订房系统,周日会看原来的内容突然觉得这个很不错,就整理了一下. 酒店订房系统其实总体的来看就是分为三个大部分,一个就是客户端,一个酒店的规模(就是酒店的有几层,多少房间),还有一个就是房间的的类型:      酒店            酒店规模            几层            每层几个房间           对外提供一个酒店房间列表打印方法           预订房间方法 实现的代码是 1 public cl

uva 10627 - Infinite Race(数论)

题目链接:uva 10627 - Infinite Race 题目大意:一段路.两个人在这条路上来回走,求相遇次数 解题思路:相遇有两种,一种是追击,一种是相对 追击:t?(u?v)=(2?k+1)?L 相对:t?(u+v)=(2?k+1)?L 可是有一种特殊情况.就是在边界相遇的时候,会被考虑两次,所以要减掉一次. 那么怎样考虑边界相遇的时间:找到一篇具体解释 #include <cstdio> #include <cstring> #include <cmath>

P2894 [USACO08FEB]酒店Hotel

P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on fam

POJ 3667 Hotel

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16782   Accepted: 7303 Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie

UVA LIVE-3263 - That Nice Euler Circuit

画一个顶点为偶数的封闭的二维图,当然.这个图能够自交,给出画的过程中的一些轨迹点.求出这个图把二次元分成了几部分,比如三角形把二次元分成了两部分. 这个的话,有图中顶点数+部分数-棱数=2的定律,这是核心思想.也就是所谓的欧拉定律拓扑版,好吧,事实上细致想想也是可以想出这个规律来的. 做出这题纯属意外,因为给的点的坐标全是用整数表示,为了不用考虑精度问题,一開始.我就想仅仅用这些点.就是说不再算出其他交点之类的,就把答案算出, 由于当前轨迹与之前轨迹无非三种情况:规范与不规范相交,不相交 不相交

(简单) POJ 3667 Hotel,线段树+区间合并。

Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Stree

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

487--3279 UVA 755 其实有三种解法

说说:这道题初看挺简单的,题意无非就是将一串字符转化成一个用'-'隔开的电话号码,然后把出现超过一次的号码按照字典升序输出即可.但是这样样做是会超时的....其实把电话号码中间的'-'去掉,电话号码其实就是一个整数,有了这个想法那就简单啦.只要设立一个超大的数组包含所有可能的电话号码,然后数组的值是该号码出现的次数,统计完后遍历一遍输出即可.但是第三种相当于把前两种方法法结合起来了.把号码当成一个整数,但是号码存储在一个数组中,号码出现的次数存储在另一个数组中.这样在插入新号码的时候就排序,然后

Hotel poj 3667

Language: Default Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18020   Accepted: 7823 Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lak