C. MUH and House of Cards (题目类型很有特点,存一下以便学习)

http://codeforces.com/contest/471/problem/C

首先看数据范围: n高达10^12,首先第一反应就是肯定有公式,而且随着层数的增高,木棍数会急速增大。

所以此题很常规的一个思路就是:

  从一层开始枚举,直到木棍数大于给出的n。

   构成最高层时用的最少数量的card数目:  n*(3n+1) / 2(因为每层数目为3n-1)

 

AC Code:

 1 //
 2 //  main.cpp
 3 //  C++_text01
 4 //
 5 //  Created by songjs on 14-10-11.
 6 //  Copyright (c) 2014年 songjs. All rights reserved.
 7 //
 8 
 9 #include <stdio.h>
10 #include <string.h>
11 #include <math.h>
12 #include <stdlib.h>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 #define LL long long
17 
18 int main()
19 {
20     //freopen("in.txt","r",stdin);
21     LL n;
22     while(scanf("%I64d",&n)!=EOF){
23         int num = 0;
24         for(LL i=1;i*(3*i+1)/2 <=n ;i++){
25             if((n-i*(3*i+1)/2 )% 3 == 0)num++;
26         }
27         printf("%d\n",num);
28     }
29     return 0;
30 }

   总结:

    遇到题目先看懂题,从小数据进行逐步分析。(一般ACM题目很少要写if else,一般都是有比较通用一点的公式的)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值