【Codeforces Round 269 (Div 2)C】【暴力】MUH and Sticks 纸牌搭楼n张牌恰好搭楼数

C. MUH and House of Cards
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to build a house of cards. For that they've already found a hefty deck of n playing cards. Let's describe the house they want to make:

  1. The house consists of some non-zero number of floors.
  2. Each floor consists of a non-zero number of rooms and the ceiling. A room is two cards that are leaned towards each other. The rooms are made in a row, each two adjoining rooms share a ceiling made by another card.
  3. Each floor besides for the lowest one should contain less rooms than the floor below.

Please note that the house may end by the floor with more than one room, and in this case they also must be covered by the ceiling. Also, the number of rooms on the adjoining floors doesn't have to differ by one, the difference may be more.

While bears are practicing to put cards, Horace tries to figure out how many floors their house should consist of. The height of the house is the number of floors in it. It is possible that you can make a lot of different houses of different heights out of n cards. It seems that the elephant cannot solve this problem and he asks you to count the number of the distinct heights of the houses that they can make usingexactly n cards.

Input

The single line contains integer n (1 ≤ n ≤ 1012) — the number of cards.

Output

Print the number of distinct heights that the houses made of exactly n cards can have.

Sample test(s)
input
13
output
1
input
6
output
0
Note

In the first sample you can build only these two houses (remember, you must use all the cards):

Thus, 13 cards are enough only for two floor houses, so the answer is 1.

The six cards in the second sample are not enough to build any house.


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
LL n;
int main()
{
	while(~scanf("%lld",&n))
	{
		int ans=0;
		LL need=0;
		for(int i=1;i<=3-n%3;++i)need+=i;
		for(int m=3-n%3;;m+=3)
		{
			LL room=(n+m)/3;
			if(room>=need)++ans;
			else break;
			need+=m+m+1+m+2;
		}
		printf("%d\n",ans);
	}
	return 0;
}
/*
【trick&&吐槽】
暴力大法好哇,大力出奇迹哇

【题意】
我们有n(1<=n<=1e12)张卡片。
我们要用这些卡片搭房子,而且恰好用完。
对于一层楼,如果房间数为x,那么总共需要的卡片数为3x-1
对于每层楼的房间数,应该要随着楼层变高严格递减。
问你,我们可以搭建多少种不同高度的楼层。

【类型】
暴力

【分析】
就算我们最大程度利用卡片,所能搭建的楼层数也不会多。
假设最高能搭建的楼层数为m,那么至少需要的卡片也是(m+1)m/2级别的,所以m最大也不过1e6。

于是我们可以暴力枚举其所能搭建的楼层。

枚举层数m,每层楼所用的卡片数都是 3*房间数-1 。
那么我们总共有的房间数,就是(n+m)/3
房间数就知道了。然后要查看是否数量够即可。
即(层数+1)/(层数)/2 <= 房间数是否成立。

这道题的时间复杂度可以是暴力的sqrt(n)或者二分复杂度log(n)
二分的话,需要用到平方和求和公式——
1^2+2^2+...+n^2==n(n+1)(2n+1)/6

*/


你可能感兴趣的:(codeforces,暴力,题库-CF)