题目链接:http://codeforces.com/problemset/problem/471/C

题目意思:有 n 张卡,问能做成多少种不同楼层(floor)的 house,注意这 n 张卡都要用光。每层 floor 都由一定的 room 构成,每两个相邻 room 规定要有一个公共的ceiling。规定从上到下看,每层 floor 的 room 的数量呈递增的形式排布。

好容易想到构成最高层时用的最少数量的card需要多少。可以发现,每层的需要用到的card 可以用这个表达式表示: 3n-1 (第一层是最高层)。例如要构成 3 层,那么最少需要用到的card 为 15,从上到下依次为 2, 5, 8。求和是有条式子的,对于前 k 层需要用到的最少card是: n*(3n+1) / 2。

具体推法可以参考:http://codeforces.com/blog/entry/13986

(作者写了这么详细的题解,确实要给一个大大的赞啊~~~)

不过,知道最少的 card 之后还未得到答案,因为 n 有可能大于构成最高层的最少card数。那么此时就有剩余了。其实问题最关键的地方,就是如何利用这个剩余数来统计结果。

对于排到第 k 层的时候,求出当前剩余数remain =  n - sum_cardk(表示第k层需要的最少card),如果remain % 3 == 0 就表示该层是可以构建的,即可以够建高度为 k 的house。因为除了最高的一层(只需要两张card),下面每层如果要增加一个room,就需要3张card,ceiling需要一张嘛~~

版本 1 (利用求和公式,时间为 31ms)

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; typedef __int64 LL; inline LL cal(LL x)
{
return x*(*x+) / ;
} int main()
{
LL n;
// freopen("input.txt", "r", stdin);
while (scanf("%I64d", &n) != EOF)
{
LL ans = ;
for (LL i = ; cal(i) <= n; i++)
{
if ((n-cal(i)) % == )
ans++;
}
printf("%I64d\n", ans);
}
return ;
}

版本2(暴力求和,不用公式,时间是 30ms)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; typedef __int64 LL; int main()
{
LL n;
while (scanf("%I64d", &n) != EOF)
{
LL cal, sum, ans;
cal = ;
ans = , sum = ; for ( ; sum + cal <= n; )
{
sum += cal;
LL remain = n - sum;
if (remain % == )
ans++;
cal += ;
}
printf("%I64d\n", ans);
}
return ;
}

codeforces 471C.MUH and House of Cards 解题报告的更多相关文章

  1. CodeForces 471C MUH and House of Cards

    MUH and House of Cards Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  2. [CF 471C] MUH and House of Cards

    C. MUH and House of Cards   Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elep ...

  3. codeforces 140B.New Year Cards 解题报告

    题目链接:http://codeforces.com/problemset/problem/140/B 题目意思:给出 Alexander 和他的 n 个朋友的 preference lists:数字 ...

  4. codeforces 814B.An express train to reveries 解题报告

    题目链接:http://codeforces.com/problemset/problem/814/B 题目意思:分别给定一个长度为 n 的不相同序列 a 和 b.这两个序列至少有 i 个位置(1 ≤ ...

  5. 【LeetCode】914. X of a Kind in a Deck of Cards 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 最大公约数 日期 题目地址: https:// ...

  6. codeforces 558B. Amr and The Large Array 解题报告

    题目链接:http://codeforces.com/problemset/problem/558/B 题目意思:给出一个序列,然后找出出现次数最多,但区间占用长度最短的区间左右值. 由于是边读入边比 ...

  7. codeforces 515B. Drazil and His Happy Friends 解题报告

    题目链接:http://codeforces.com/problemset/problem/515/B 题目意思:有 n 个 boy 和 m 个 girl,有 b 个 boy 和 g 个 girl ( ...

  8. codeforces 514B. Han Solo and Lazer Gun 解题报告

    题目链接:http://codeforces.com/problemset/problem/514/B 题目意思:给出双头枪的位置(x0, y0),以及 n 个突击队成员的坐标.双头枪射击一次,可以把 ...

  9. codeforces C. Vasily the Bear and Sequence 解题报告

    题目链接:http://codeforces.com/problemset/problem/336/C 题目意思:给出一个递增的正整数序列 a1, a2, ..., an,要求从中选出一堆数b1, b ...

随机推荐

  1. 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...

  2. 用requests库实现登录遇到的问题

    想登录zhihu,然后总是得到403 foribidden的错误,各种谷歌百度,得到结论说是输入错误或者是url错误,用fldder发现的确是url错了,post的地址是错误的 ==. 开始以为是#s ...

  3. MS-sqlserver数据库2008如何转换成2000

    http://bbs.csdn.net/topics/390438560?page=1#post-394316973 MS-sqlserver数据库2008如何转换成2000 回你这个贴等于我写个博客 ...

  4. MySQL 存储过程传参之in, out, inout 参数用法

    存储过程传参:存储过程的括号里,可以声明参数. 语法是 create procedure p([in/out/inout] 参数名  参数类型 ..) in :给参数传入值,定义的参数就得到了值 ou ...

  5. 初学structs2,简单配置

    一.structs2-demo1项目下新建structs.xml文件,文件名必须是structs 二.package节点配置及其子节点配置 <!--name:单纯给包起个名字,保证和其他包不重名 ...

  6. 关于Java单例

    参考资料:http://blog.csdn.net/haoel/article/details/4028232 public class SingletonTest implements Runnab ...

  7. centos安装gitlab

    原文链接: http://www.centoscn.com/image-text/install/2015/0320/4929.html http://www.01happy.com/centos-6 ...

  8. js实现鼠标点击input框后里面的内容就消失代码

    <!--# <a href="http://www.mianfeimoban.com/texiao_mb/" target="_blank" cla ...

  9. Shell脚本获得变量值作为新变量一部分的值

    最近使用shell写一个发布脚本时,由于shell编程是边学便用的缘故,经验不足,中间遇到一个问题,一个变量的值作为一个新变量的一部分,而我要根据变量获得新变量的值,以前没有遇到过.网络搜索一番,最后 ...

  10. afasf

    http://www.cnblogs.com/ttzhang/archive/2008/11/02/1324601.html project server 2007 sn :W2JJW-4KYDP-2 ...