codeforces 471C.MUH and House of Cards 解题报告
题目链接: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 解题报告的更多相关文章
- CodeForces 471C MUH and House of Cards
MUH and House of Cards Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & % ...
- [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 ...
- codeforces 140B.New Year Cards 解题报告
题目链接:http://codeforces.com/problemset/problem/140/B 题目意思:给出 Alexander 和他的 n 个朋友的 preference lists:数字 ...
- codeforces 814B.An express train to reveries 解题报告
题目链接:http://codeforces.com/problemset/problem/814/B 题目意思:分别给定一个长度为 n 的不相同序列 a 和 b.这两个序列至少有 i 个位置(1 ≤ ...
- 【LeetCode】914. X of a Kind in a Deck of Cards 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 最大公约数 日期 题目地址: https:// ...
- codeforces 558B. Amr and The Large Array 解题报告
题目链接:http://codeforces.com/problemset/problem/558/B 题目意思:给出一个序列,然后找出出现次数最多,但区间占用长度最短的区间左右值. 由于是边读入边比 ...
- codeforces 515B. Drazil and His Happy Friends 解题报告
题目链接:http://codeforces.com/problemset/problem/515/B 题目意思:有 n 个 boy 和 m 个 girl,有 b 个 boy 和 g 个 girl ( ...
- codeforces 514B. Han Solo and Lazer Gun 解题报告
题目链接:http://codeforces.com/problemset/problem/514/B 题目意思:给出双头枪的位置(x0, y0),以及 n 个突击队成员的坐标.双头枪射击一次,可以把 ...
- codeforces C. Vasily the Bear and Sequence 解题报告
题目链接:http://codeforces.com/problemset/problem/336/C 题目意思:给出一个递增的正整数序列 a1, a2, ..., an,要求从中选出一堆数b1, b ...
随机推荐
- 5.Android之image控件学习
Android中经常用到图片,比如图片浏览.图标等等,今天学习下image控件,image控件主要有ImageButton和ImageView两种. (1)ImageButton 在布局文件增加: & ...
- poj 1006 中国剩余定理解同余方程
其实画个图就明白了, 该问题就是求同余方程组的解: n+d≡p (mod 23) n+d≡e (mod 28) n+d≡i (mod 33) #include "iostream" ...
- css常用技巧
去点号 list-style:none; 字体居中 text-align:center; 链接去下划线 text-decoration:none; 鼠标禁止右键 <body oncontextm ...
- groovy-脚本和类
在groovy中定义类和java中是一样的.类的方法可以是static,也可以是非static的. groovy中的方法可以是public, protected, private,同时也支持java中 ...
- HD2157How many wasy??(十大矩阵问题之八 + 邻接矩阵的应用)
How many ways?? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 初学structs2,结果类型简单示例
一.自定义结果处理类,structs.xml中package节点下加result-types节点,在result-types节点下配置result-type的属性.然后在配置的action中的resu ...
- 也谈闭包--小白的JS进阶之路
JavaScript当然是会用的,不过没有深入系统的学习罢了.平常还是用JQuery比较多,原生的JS用到的很少. 不过前端时间学习Ruby,被动态语言的强大和魔幻给震惊了一把.了解Ruby后,我把目 ...
- linux 下安装memcached与php的memcache扩展
1. 在线安装 yum install memcached: 源代码安装 wget http://memcached.org/latest 下载最新版本 tar -zxvf memcached-1.x ...
- derby的三大缺陷
derby的三大缺陷 derby数据库的嵌入式特性让人很流口水.但是,我刚打算将其用进我的项目中,却发现它没有好的分页查询方式,每次都返回所有符合条件的记录.oracle有rownum,mysql有l ...
- fp = fopen(s, "at") 中at 是啥意思,a 是append 追加的意思
打开一个s的stream, a表示append,就是说写入处理的时候是接着原来文件已有内容写入,不是从头写入覆盖掉, t表示打开文件的类型是文本文件, "+号表示对文件既可以读也可以写.&q ...