• CQUOJ 10819 MUH and House of Cards


    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 using exactly 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.

     1 /*
     2 2016年4月24日14:55:30
     3 
     4 这道题目要看清啊  给n个卡牌, 求所能搭建的 不同楼层房子 的数 
     5 
     6 看题目的Hint 图形就知道题意了,
     7 对着图形观察一下就会发现, 
     8 (这个图形不是题目所给的那个图形,而是自己画的 第一层2个卡牌, 第二层5个卡牌 ....),
     9 第 n 层(从上往下)需要的卡牌数目为 2 * n + (n - 1)个,
    10 可以化简一下 公式就是 3 * n - 1,这样就会发现 
    11 
    12 每层所需卡牌的数差1就是3的倍数了,
    13 然后每一层都差1,如果有i层的话,那么其实就是差了i
    14 
    15 所以, 假设共有卡牌 x张,其实 就是枚举 i ,有多少个i 使得 (x + i)%3 == 0,
    16 ( 这里的意思是  如果 i 符合(x + i)% 3 == 0 , 
    17     则可以搭建 i 层的房子(因为每层楼的卡牌数都是差1 就是3的倍数 ) 
    18         再想想你就懂 )
    19 
    20 但是还有个限制的,因为 搭建i层 至少需要的牌数要知道,
    21 不能超过所给的卡牌数目x张, 
    22 由等差公式 (2 + (3*i-1)) * i / 2 = (3 * i + 1) * i / 2 可得
    23 搭建i层  至少需要 (3 * i + 1)* i/2张卡牌,这样 就很容易确定枚举范围了,
    24 而且 答案不大,所以直接枚举答案没事
    25 
    26 */
    27 
    28 # include <iostream>
    29 # include <cstdio>
    30 # include <cstring>
    31 # include <algorithm>
    32 # include <queue>
    33 # include <vector>
    34 # include <cmath>
    35 # define LL long long 
    36 # define INF 0x3f3f3f3f
    37 using namespace std;
    38 
    39 int main(void)
    40 {
    41      LL n, i, ans;
    42      while (~scanf("%I64d", &n)){
    43          ans = 0;
    44          for (i = 1; ; i++){
    45              if (n < (3 * i + 1)* i / 2)
    46                  break;
    47              if ((n + i) % 3 == 0)
    48                  ans++;
    49          }
    50          printf("%I64d
    ", ans);
    51      }
    52     
    53     return 0;    
    54 }
  • 相关阅读:
    剑指offer-删除链表中重复的节点
    剑指offer——二叉树中和为某一值的路径
    动态规划之140 Word Break2
    动态规划之139 Word Break
    动态规划之132 Palindrome Partitioning II
    动态规划之115 Distinct Subsequences
    动态规划之97 Interleaving String
    动态规划之91 decode ways
    vector
    第一篇
  • 原文地址:https://www.cnblogs.com/hyq123456/p/5427664.html
Copyright © 2020-2023  润新知