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 ...
随机推荐
- BZOJ-1854 游戏 二分图匹配 (并查集)
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 3372 Solved: 1244 [Submit][Status] ...
- BZOJ2460 [BeiJing2011]元素
Description 相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力取决于使用的矿石. 一般地,矿石越多则法力越 ...
- SQL Server中,Numric,Decimal,Money三种字段类型的区别
都是精确数据类型, 前两个可以自己定义长度和小数位数, Money的定义相当于Numric(19,4) numeric(10,2) 表示最大可以放10位数,但这10位数里有2位是小数如: 123456 ...
- 洛谷P2279 [HNOI2003]消防局的设立
题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状 ...
- 从注册流程 分析如何安全退出多个Activity 多种方式(附DEMO)
退出Activity注册Android遍历 目录(?)[+] 前言 知识结构 具体方案 方案1 方法采用FLAG_ACTIVITY_CLEAR_TOP退出整个程序多activity 方案2 方 ...
- org.springframework.orm.hibernate3.LocalSessionFactoryBean的疑惑解决办法
在项目中使用了SSH框架(Struts2 + Spring3+ Hibernate3),applicationContext中配置了sessionFactory <bean id="s ...
- c# 闭包 小例
class Program { static void fnnn() { string[] k = new string[] { "x", "y", " ...
- 升级centos6.5系统的gcc为4.8.5的简易步骤
Centos6.5_64位升级gcc为4.8.2的简易步骤 一.安装依赖包 yum install texinfo-tex flex zip mpfr-devel libgcc.i686 glibc- ...
- pom.xml
使用intelJ idea 导入maven包管理文件是,使用Import的方式导入,会自动导入pom.xml来导入包. pom.xml会指定父子关系. 例如,总模块的pom.xml中有一下内容: &l ...
- C语言or和and运算
#include <stdio.h> void main () { printf( | ); printf( | ); printf( | ); printf( | ); printf( ...