有道破题~~

很郁闷的一道“有道难题”:

这个是在线初赛我抽到的500题目,意思很简单,描述如下:

Problem Statement
   
双倍超立方数是指一个正整数可以正好被拆分为两种不同的a^3+b^3的方式,其中a,b均为整数且0<a<=b。对于任何一个指定的 int n, 返回所有的小于等于n的双倍超立方数的个数。
Definition
   
Class:
TwiceSuperCubic
Method:
count
Parameters:
int
Returns:
int
Method signature:
int count(int n)
(be sure your method is public)
   

Constraints
-
n取值范围为1到1,000,000,000(含)
Examples
0)

   
1
Returns: 0

1)

   
1729
Returns: 1
1729=1^3+12^3 1729=9^3+10^3
2)

   
475574
Returns: 27

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

    首先声明,为了不泄题,我等到所有比赛结束后才发,题目意思看上去很简单,不过却是要仔细看要“求什么”再写代码。我开始就把题目意思理解成对于一个数n,返回他有多少种情况(不同a,b)存在a^3+b^3=n了...(topcoder做多了太想当然了...)就是这个破原因费了我好多的时间,后来test之后发现题意理解重大偏差..然后飞速修改代码,可以通过所有test了,可是我发现对于example[2]我就耗时1.8s了,那么对于1,000,000,000的数据量,完全就算不出来,我写的是一个接近O(n^3)的垃圾算法,虽然做了很多优化。。。一般topcoder的题目的数据量是很小的,又想当然了。。
    这该怎么办呢,显然要想个全新的算法,可是时间剩下不到10分钟了(一共60分钟2题),就在这是我肚子好痛,唉!只好先上个厕所想一想了,正蹲着突然想到了一个算法,这种算法是一种逆向思维,先把对于所有数对的立方和小于n的数全部求出来,存在一个map中,然后再针对n求解,可惜正当我调试的时候,唉...时间到了,垃圾算法也没提交,最后就考第一个题目得了229分...太遗憾了!!(祈祷第一题不要有什么差错啊...)

  反正要全心准备考研,诅咒自己不要进复赛!!

 1 #include  < iostream >
 2 #include  < sstream >
 3 #include  < algorithm >
 4 #include  < string >
 5 #include  < utility >
 6 #include  < map >
 7 #include  < set >
 8 #include  < list >
 9 #include  < stack >
10 #include  < queue >
11 #include  < cctype >
12 #include  < vector >
13 #include  < bitset >
14 #include  < cmath >
15 #include  < functional >
16 // #include <hash_map>
17 // #include <fstream>
18 // #include <cstdlib>
19 // #include <ctime>
20
21 #define  FOR(i, a, b) for(int i = (a); i < (b); ++i)
22 #define  FORR(i, a, b) for(int i = (a); i <= (b); ++i)
23 #define  BE(x) (x).begin(), (x).end()
24
25 using   namespace  std;
26 // using namespace stdex;
27
28 typedef vector < string >  VS;
29 typedef vector < int >  VI;
30 typedef  long   long  LL;
31
32 class  TwiceSuperCubic
33 ExpandedBlockStart.gifContractedBlock.gif {
34public:
35    int count(int n)
36ExpandedSubBlockStart.gifContractedSubBlock.gif    {
37        int result = 0, max = (int)pow((double)n, 1.0/3), temp;
38        VI cubic;
39        FORR(i, 0, max)
40ExpandedSubBlockStart.gifContractedSubBlock.gif        {
41            temp = i*i*i;
42            cubic.push_back(temp);
43        }

44        //res[i]表示
45        map<intint> res;
46        FORR(i, 1, max)
47ExpandedSubBlockStart.gifContractedSubBlock.gif        {
48            FORR(j, i, max)
49ExpandedSubBlockStart.gifContractedSubBlock.gif            {
50                temp = cubic[i] + cubic[j];
51                if (temp <= n)
52                    res[temp]++;
53            }

54        }

55        map<intint>::const_iterator it = res.begin();
56        while(it != res.end())
57ExpandedSubBlockStart.gifContractedSubBlock.gif        {
58            if(it->second == 2)
59                result++;
60            it++;
61        }

62        return result;
63    }

64}
;
65
66 int  main()
67 ExpandedBlockStart.gifContractedBlock.gif {
68    TwiceSuperCubic x;
69    //return 1546 with less than 2s
70    cout << x.count(1000000000);
71    return 0;
72}

 

转载于:https://www.cnblogs.com/CCBB/archive/2009/05/31/1493212.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值