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 ...
随机推荐
- yii2系统定义的常用路径别名
@yii 表示Yii框架所在的目录,也是 yii\BaseYii 类文件所在的位置: @app 表示正在运行的应用的根目录,一般是 digpage.com/frontend :物理路径 @vendor ...
- Android中的Uri.parse()
1,调web浏览器 Uri myBlogUri = Uri.parse("http://www.baidu.com"); returnIt = new Intent(Intent. ...
- BZOJ-1002 轮状病毒 高精度加减+Kirchhoff矩阵数定理+递推
1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3543 Solved: 1953 [Submit][Statu ...
- html中设置锚点定位的几种常见方法(#号定位)
在html中设置锚点定位我知道的有几种方法,在此和大家分享一下: 1.使用id定位: <a href="#1F">锚点1</a> <div id=&q ...
- Sublime Text 3 笔记
Nearly all of the interesting files for users live under the data directory. The data directory is ~ ...
- CSS3系列二(媒体支持、文字与字体相关样式、盒相关样式)
CSS3媒体支持 在css3中允许我们在不改变内容的情况下,在样式中选择一种页面的布局以精确的适应不同的设备,从而改善用户体验 可以利用meta标签在页面中指定浏览器在处理本页面时按照多少像素的窗口宽 ...
- ci默认控制器
默认控制器 前面提到,如果在请求中没有指明具体的控制器,CI将会把页面重定向到一个系统默认的页面.这个默认页面可以自己设定,它存放在如下地址:/system/application/config/ro ...
- hdu 2199 Can you solve this equation?
#include<stdio.h> #include<math.h> double f(double x) { return 8*x*x*x*x+7*x*x*x+2*x*x+3 ...
- CSS 2D转换 matrix() 详解
2D转换 IE10.Firefox.Opera 支持 transform 属性 Chrome.Safari 需要前缀 -webkit- . IE9 需要前缀 -ms- . translate():接收 ...
- 一个令人蛋疼的 Microsoft.AspNet.FriendlyUrls
我一个项目都基本上做完了,结果部署到我服务器的时候结果一直报404 找不到 一看global.asax有个路由注册的代码 public static void RegisterRoutes(Route ...