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 ...
随机推荐
- Oracle查看锁表
查看锁表进程SQL语句1: select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, a ...
- Codevs2157 配对
题目描述 Description 给出2个序列A={a[1],a[2],…,a[n]},B={b[1],b[2],…,b[n]},从A.B中各选出n个元素进行一一配对(可以不按照原来在序列中的顺序), ...
- CodeReview Learning
目录 . 引言 . 代码检视的指导思想 . 代码检视的内容 . 回归测试 0. 引言 代码检视(Code Review)是指软件开发人员在完成代码设计.编写.调试后展开的个人或群体性的代码阅读过程,代 ...
- java将一维数组拆分成二维数组
package staticFactory; public class Array { public static void main(String[] args) { String[] a=new ...
- 两个大的整数的运算(java)
import java.math.BigInteger; public class BigInt { BigInteger m1; BigInteger m2; BigInteger m3; BigI ...
- latin1
Latin1是ISO-8859-1的别名,有些环境下写作Latin-1.ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII ...
- Developing a plugin framework in ASP.NET MVC with medium trust
http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust.aspx January 7, ...
- shell的内建命令和外部命令
shell的内建命令和外部命令 Shell执行的命令可以分为内建命令(built-in)和外部命令(external),前者是构建在shell内部:后者是一个独立的文件(可以是二进制文件,也可以是一个 ...
- Nginx和PHP-FPM的启动、重启、停止脚本分享(转)
服务器上的Nginx和PHP都是源码编译安装的,不像ubuntu一样有自带service启动脚本,所以不支持类似以前的nginx (start|restart|stop|reload)了.自己动手丰衣 ...
- Cannot find class [org.apache.commons.dbcp.BasicDataSource]
错误:Cannot find class [org.apache.commons.dbcp.BasicDataSource] 原因:缺少commons-dbcp.jar