题目链接: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)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. typedef __int64 LL;
  8.  
  9. inline LL cal(LL x)
  10. {
  11. return x*(*x+) / ;
  12. }
  13.  
  14. int main()
  15. {
  16. LL n;
  17. // freopen("input.txt", "r", stdin);
  18. while (scanf("%I64d", &n) != EOF)
  19. {
  20. LL ans = ;
  21. for (LL i = ; cal(i) <= n; i++)
  22. {
  23. if ((n-cal(i)) % == )
  24. ans++;
  25. }
  26. printf("%I64d\n", ans);
  27. }
  28. return ;
  29. }

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

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. typedef __int64 LL;
  8.  
  9. int main()
  10. {
  11. LL n;
  12. while (scanf("%I64d", &n) != EOF)
  13. {
  14. LL cal, sum, ans;
  15. cal = ;
  16. ans = , sum = ;
  17.  
  18. for ( ; sum + cal <= n; )
  19. {
  20. sum += cal;
  21. LL remain = n - sum;
  22. if (remain % == )
  23. ans++;
  24. cal += ;
  25. }
  26. printf("%I64d\n", ans);
  27. }
  28. return ;
  29. }

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. 【CodeForces 622A】Infinite Sequence

    题意 一个序列是, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5....这样排的,求第n个是什么数字. 分析 第n个位置属于1到k,求出k,然后n-i*(i-1)/ ...

  2. python 类型之 set

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...

  3. OracleOraDb10g_home1TNSListener无法启动

    1:“本地计算机上的OracleOraDb10g_home1TNSListener服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止.” 解决办法:动态ip,服务 OracleOraDb10 ...

  4. 《驾驭Core Data》 第二章 Core Data入门

    本文由海水的味道编译整理,请勿转载,请勿用于商业用途.    当前版本号:0.4.0 第二章 Core Data入门 本章将讲解Core Data框架中涉及的基本概念,以及一个简单的Core Data ...

  5. display & visibility区别

    http://www.cnblogs.com/zhangran/archive/2012/08/29/2662459.html 说明:在学习css的过程中由于其中包含太多的元素.属性等等,总有许多是自 ...

  6. PHP实现发红包程序(helloweba网站经典小案例)

    我们先来分析下规律. 设定总金额为10元,有N个人随机领取: N=1 第一个 则红包金额=X元: N=2 第二个 为保证第二个红包可以正常发出,第一个红包金额=0.01至9.99之间的某个随机数. 第 ...

  7. 深入理解 Javascript 面向对象编程

    一:理解构造函数原型(prototype)机制 prototype是javascript实现与管理继承的一种机制,也是面向对象的设计思想.构造函数的原型存储着引用对象的一个指针,该指针指向与一个原型对 ...

  8. 兼容amd,commonjs和browser的模块写法

    从uuid.js中抽出来的写法. (function() { var _global = this; // Export public API var obj = {}; obj.attr = fun ...

  9. C语言中strstr函数

    头文件:#include <string.h> strstr()函数用来检索子串在字符串中首次出现的位置,其原型为:    char *strstr( char *str, char * ...

  10. webexam项目杂记2

    strstr,stristr是返回匹配到的字符串,常规的字符串操作尽量避免使用正则, strstr是返回从匹配字符(串)开始(包括该匹配字符串)到结束的(或开头的)字符串 而如果仅仅只是判断是否包含匹 ...