MUH and House of Card
2016-12-08 12:27
411 查看
**
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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:
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.
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张卡牌,要求按图样搭出层数尽量高的房子。且底层一定要大于上层。
思路:这题需要多画图,从画图中找到规律,即第k层至少需要的卡牌数量为3*k-1;(如3层高的房子,第二层需要的卡牌数为3*2-1==5;第三层需要3*3-1==8;)而达到第i层需要的卡牌数为(3*i+1)*i/2;(三层高的房子需要卡牌的总数为15;)
根据上述规律,可以得到(3*i+1)*i/2<=n;以此可以作为条件。而3*k-1这个条件的作用,就是用来判断是否层数+1。在搭房子的过程中会发现,每一层会跟3的倍数少1,即有k层则跟3的倍数少k。
代码实现:
引用
http://www.2cto.com/kf/201503/383428.html
MUH and House of Card
**stime limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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:
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.
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张卡牌,要求按图样搭出层数尽量高的房子。且底层一定要大于上层。
思路:这题需要多画图,从画图中找到规律,即第k层至少需要的卡牌数量为3*k-1;(如3层高的房子,第二层需要的卡牌数为3*2-1==5;第三层需要3*3-1==8;)而达到第i层需要的卡牌数为(3*i+1)*i/2;(三层高的房子需要卡牌的总数为15;)
根据上述规律,可以得到(3*i+1)*i/2<=n;以此可以作为条件。而3*k-1这个条件的作用,就是用来判断是否层数+1。在搭房子的过程中会发现,每一层会跟3的倍数少1,即有k层则跟3的倍数少k。
代码实现:
#include <cstdio> #include <cmath> #include <algorithm> using namespace std; int main() { long long n; scanf("%lld",&n); long long i,cnt=0;; for (i=1;(3*i+1)*i/2<=n;i++) { if ((n+i)%3==0) cnt++; } printf ("%lld\n",cnt); }
引用
http://www.2cto.com/kf/201503/383428.html
相关文章推荐
- CodeForces 471C - MUH and House of Cards(推导)
- Codeforces 471C MUH and House of Cards【找规律+二分】
- C. MUH and House of Cards(数论规律题)
- Codeforces #269 (Div. 2)C. MUH and House of Cards(数学:通项公式)
- C. MUH and House of Cards(Codeforces Round #269)
- codeforces 471C MUH and House of Cards 数论
- C. MUH and House of Cards (题目类型很有特点,存一下以便学习)
- Can not find Storage Card of Mediaplay 11, and resolve of solution.
- 【CF471E】MUH and Lots and Lots of Segments 扫描线+并查集+线段树+set
- Understanding Windows CardSpace: An Introduction to the Concepts and Challenges of Digital Identitie
- WLCardViewLayout: Card and swipe out layout of CollectView.(可以滑动删除的卡片视图布局)
- Tales of Mystery and Imagination——1、The Fall of the house of Usher
- ethtool speed HowTo : Change Speed and Duplex of Ethernet card in Linux
- Linux change the speed and duplex settings of an Ethernet card
- Analyzing billions of credit card transactions and serving low-latency insights in the cloud
- Unable to find Definition of MD5_Init, MD5_Update and MD5_Final!
- [论文笔记] Quality of service for workflows and web service processes (Journal of Web Semantics, 2004)
- Action prediction and identification of tempral user behavior
- mongo cluster of replicate set and shard
- Oracle Bills of Material and Engineering Application Program Interface (APIs)