您的位置:首页 > 其它

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

2014-12-25 19:37 477 查看
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,一般都是有比较通用一点的公式的)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: