CodeForces 471C MUH and House of Cards

来源:互联网 发布:java执行shell脚本 编辑:程序博客网 时间:2024/05/22 12:34

题目:

Description

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 Input

Input
13
Output
1
Input
6
Output
0

Hint

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.


看图应该就能理解大意了。

输入card的数量,输出摆的层数可能会有多少种情况。

比如上面的图片,只能摆2层,层数只有1种情况,那么答案为1。

如果根本没法摆,答案为0。


然后就是找规律了。

首先按行分。

对于左图,一楼有4个房子,有3个天花板,card有4*3-1个

二楼有1个房子,0个天花板,card有1*3-1个

对于右图,一楼有3个房子,2个天花板,card有3*3-1个

二楼有2个房子,1个天花板,card有2*3-1个。

然后,假设有h层,从上往下各有x1、x2、x3。。。。。。xh个房子。

现在就是要求方程n=(x1*3-1)+(x2*3-1)+......+(xh*3-1)是否有解,而且x1<x2<x3......<xh

方程可以化简为n+h=3*(x1+x2+x3......+xh)

如果n+h不是3的倍数,那么无解,如果n+h是3的倍数,

那么,n+h=3*(x1+x2+x3......+xh)有解等价于n+h>=3*(1+2+3+......+h)


问题的完整表述:

输入n,求有多少个正整数h,使得n+h=3k,且k>=1+2+3+......+h

代码:

#include<iostream>using namespace std;int main(){long long n;cin >> n;int sum = 0;for (long long h = 3 - (n % 3);; h += 3){if (n * 2 < h*h * 3 + h)break;sum++;}cout << sum;return 0;}

1 0
原创粉丝点击