codeforces—MUH and House of Cards补11.16training
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:
- The house consists of some non-zero number of floors.
- 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.
- 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 using exactly n cards.
圣彼得堡动物园的北极熊 Menshykov 和 Uslada 以及基辅动物园的大象 Horace 决定建造一座纸牌屋。为此,它们已经找到了一副厚厚的 n 扑克牌。让我们来描述一下他们想要建造的房子:
- 房子由若干非零层数的楼层组成。
- 每层由数量不为零的房间和天花板组成。一个房间是两张互相靠在一起的牌。房间排成一排,每两个相邻的房间共享由另一张牌组成的天花板。
- 除最低层外,每一层的房间数都应少于下面一层。
请注意,房子的尽头可能是有多个房间的楼层,在这种情况下,它们也必须被天花板覆盖。此外,相邻楼层的房间数不一定相差一个,可能相差更多。
在小熊练习放卡片的同时,霍勒斯也在计算他们的房子应该有几层。房子的高度就是楼层数。用 n 张扑克牌可以做出很多不同高度的房子。大象似乎无法解决这个问题,他让你算一算他们用 完全 n 张扑克牌可以做出多少不同高度的房子。
Input
The single line contains integer n (1 ≤ n ≤ 1012) — the number of cards.
输入
单行包含整数 n ( 1 ≤ n ≤ 1012 ) - 纸牌数量。
Output
Print the number of distinct heights that the houses made of exactly n cards can have.
输出
打印完全由 n 张卡片组成的房屋的不同高度数。
Examples
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.
注意
在第一个示例中,您只能建造这两座房子(记住,您必须使用所有卡片):

因此,13 张牌只够建造两层房屋,所以答案是 1。
第二个样本中的 6 张牌不足以建造任何房屋。
题解
因为我们只需要输出高度的种类数,所以我们可以尽量去构造高度,我们发现高度多一层到达h层时所需最少的额外纸牌数为3 * h + 2,而剩下的纸牌直接在底层进行搭建,因为一定要有天花板所以剩下的纸牌一定能被3整除,这时就能达到这一高度。
代码如下
#include<bits/stdc++.h>
using namespace std;
int main() {
long long n;cin>>n;
long long h = 0;
long long res = 0;
long long ans = 0;
while(1){
res += 3 * h + 2;
h ++;
if(n - res < 0)
break;
if((n - res)%3!=0)
continue;
ans ++;
}
cout<<ans<<endl;
return 0;
}

浙公网安备 33010602011771号