Codeforces 471C MUH and House of Cards【找规律+二分】

来源:互联网 发布:动态代理java 编辑:程序博客网 时间:2024/06/07 21:30

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 ofn 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.

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.


题目大意:
房子由一些非零数目的楼层组成。
每个楼层包括一个非零数量的房间和天花板。 一个房间是两张彼此靠在一起的牌。 房间排成一排,每两个相邻的房间共享由另一张卡制成的天花板。
上边楼层的房间数应少于下边楼层的房间数。
给你一个数N,表示一共有多少个卡片,问一共能够摆出几种高度的楼房。

思路:

1、首先我们观察到,如果我们按照最紧凑的方式来搭建起来的话:
那么很明显:
①搭建一层,最少需要2个卡片。
②搭建两层,最少需要7个卡片。
③搭建三层,最少需要15个卡片。
④搭建四层,最少需要26个卡片。
⑤搭建五层,最少需要40个卡片。
............................依次类推,我们发现,每层最少需要的卡片的差值是有规律递增的:
7-2=5
15-7=8
26-15=11
40-26=14
每次递增3个。
那么我们设定数组f【i】,表示搭建i层最少需要的卡片数,预处理出来。
根据测试,数组大小为816497.那么约为10^6大小的一个数组。

2、接下来考虑,对应一个高度的楼房,我们可以对其进行拓展,很显然我们要从最底层开始拓展,才能拓展上边层数的房间数。每次每层拓展需要3个卡片,又因为对其形状无所关心,所以那么我们只要是f【i】+3*X==n(X是一个整数),那么有n个卡片的话,就一定能够建筑出来一种高度为i的楼房。

3、那么我们接下来对于这个X进行二分,我们枚举每一种高度的楼房,约为10^6的一个枚举操作量。接下来二分这个X,总时间复杂度约为10^7...

Ac代码:

#include<stdio.h>#include<string.h>using namespace std;#define ll __int64ll f[10000060];void init(){    ll base=2;    ll tmp=5;    f[1]=2;    for(int i=2;;i++)    {        f[i]=f[i-1]+tmp;        tmp+=3;        if(f[i]>1000000000000)        {            // printf("%d\n",i);//816497            // 816496            break;        }    }}int main(){    init();    ll n;    while(~scanf("%I64d",&n))    {        int output=0;        /*        for(ll i=1;i<=816;i++)        {            for(ll j=0;;j++)            {                if(f[i]+j*3==n)                {                    output++;                    break;                }                else if(f[i]+j*3>n)break;            }        }*/        for(ll i=1;i<=816496;i++)        {            ll l=0;            ll r=1000000000000;            while(r-l>=0)            {                ll mid=(l+r)/2;                if(f[i]+mid*3>n)                {                    r=mid-1;                }                else if(f[i]+mid*3<n)                {                    l=mid+1;                }                else                {                    output++;break;                }            }        }        printf("%d\n",output);    }}

















0 0
原创粉丝点击