Thursday 29 October 2015

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.

First of make us few observations

suppose we fix r rooms in a floor then we will see that the number of cards needed for the same are 2*R+(R-1)

2 cards for each rooms and R-1 cards for the ceiling

Suppose we construct F floors with the same then number of cards N required for the same are 

2*r1+(r1-1)+ 2*r2+(r2-1) +2*r3+(r3-1)........=N
  
therfore  if the number of floors is equal to F then the rooms needed are 

          3*r1+3*r2+3*r3+......+3*rf+.....=N+F
implies
               3(r1+r2+r3+.....+rf)=N+F

First observation is that to be a valid frame the factor N+F should be totally divisble by 3 else we cannot for a 
valid house of cards. 

Moving to the second step, for a house with height f since number of floors should be monotonically increasing therefore
our greedy to of assignment is 1 room on first floor 2 room on second and so on.....

Lets resolve the for any height we need atleast 3 *F*(F+1)/2 cards if this is applicable (meaning if for any 
floor F we have more than this number of cards) and if (N+F) is divisible by 3 then we can construct the house.

The code goes below 


#include "assert.h"
#include <iostream>

typedef long long int LL;

using namespace std;

LL getNumberOfCardsInFullHouse(LL floors) {
  return 3 * floors * (floors + 1) / 2 - floors;
}
 
LL solve(LL n) {
  LL res = 0;
  for (LL floors = 1 ; getNumberOfCardsInFullHouse(floors) <= n ; floors++)
    if ((n + floors) % 3 == 0)
      res++;
  return res;
}
 
int main() {
  LL n; cin >> n;
  cout << solve(n); 
}




No comments:

Post a Comment