ZJU 1331 解题报告

Perfect Cubes

Time limit: 10 Seconds   Memory limit: 32768K  
Total Submit: 2409   Accepted Submit: 1045  

For hundreds of years Fermat's Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 such that a^n = b^n + c^n, has remained elusively unproven. (A recent proof is believed to be correct, though it is still undergoing scrutiny.) It is possible, however, to find integers greater than 1 that satisfy the ``perfect cube'' equation a^3 = b^3 + c^3 + d^3 (e.g. a quick calculation will show that the equation 12^3 = 6^3 + 8^3 + 10^3 is indeed true). This problem requires that you write a program to find all sets of numbers {a, b, c, d} which satisfy this equation for a <= 200.


Output

The output should be listed as shown below, one perfect cube per line, in non-decreasing order of a (i.e. the lines should be sorted by their a values). The values of b, c, and d should also be listed in non-decreasing order on the line itself. There do exist several values of a which can be produced from multiple distinct sets of b, c, and d triples. In these cases, the triples with the smaller b values should be listed first.

The first part of the output is shown here:

Cube = 6, Triple = (3,4,5)
Cube = 12, Triple = (6,8,10)
Cube = 18, Triple = (2,12,16)
Cube = 18, Triple = (9,12,15)
Cube = 19, Triple = (3,10,18)
Cube = 20, Triple = (7,14,17)
Cube = 24, Triple = (12,16,20)

Note: The programmer will need to be concerned with an efficient implementation. The official time limit for this problem is 2 minutes, and it is indeed possible to write a solution to this problem which executes in under 2 minutes on a 33 MHz 80386 machine. Due to the distributed nature of the contest in this region, judges have been instructed to make the official time limit at their site the greater of 2 minutes or twice the time taken by the judge's solution on the machine being used to judge this problem.

 


Problem Source: Mid-Central USA 1995 

 

题目地址:http://acm.zju.edu.cn/show_problem.php?pid=1331

初看题目,首先想到的就是暴力解决,四个for循环,代码如下:

#include  < stdio.h >

int  main( int  argc,  char   * argv)
{
    
int a, b, c, d;
    
    
for (a = 2; a <= 200; a ++
        
for (b = 2; b < a; b ++
            
for (c = b; c < a; c ++
                
for (d = c; d < a; d ++)
                    
if (a * a * a == b * b * b + c * c * c +* d * d) {
                        printf(
"Cube = %d, Triple = (%d,%d,%d) ", a, b, c, d);
                        
break;
                    }


    
return 0;
}

 

但这样做,显然效率太低,每个数的立方都要重复计算很多次,可以定义一个数组,以空间换时间,代码如下:

#include  < stdio.h >

int  main( int  argc,  char   * argv)
{
    
int a, b, c, d, i, cube[201];
    
    
for (i = 0; i < 201; i ++) cube[i] = i * i * i;
    
for (a = 2; a <= 200; a ++
        
for (b = 2; b < a; b ++
            
for (c = b; c < a; c ++
                
for (d = c; d < a; d ++)
                    
if (cube[a] == cube[b] + cube[c] + cube[d]) {
                        printf(
"Cube = %d, Triple = (%d,%d,%d) ", a, b, c, d);
                        
break;
                    }


    
return 0;
}

 

再优化,程序中的加法有很多是重复的,一些循环控制的初值也可改变以减少循环次数。如下:

#include  < stdio.h >

int  main( int  argc,  char   * argv)
{
    
int a, b, c, d, i, x, y, cube[201];
    
    
for (i = 1; i < 201; i ++) cube[i] = i * i * i;
    
for (a = 6; a <= 200; a ++
        
for (b = 2; b < a; b ++{
            x 
= cube[a] - cube[b];
            
for (c = b + 1; c < a; c ++{
                y 
= x - cube[c]; 
                
for (d = c + 1; d < a; d ++)
                    
if (y == cube[d]) {
                        printf(
"Cube = %d, Triple = (%d,%d,%d) ", a, b, c, d);
                        
break;
                    }

            }

        }


    
return 0;
}

 

最后一个for循环的if判断语句为顺序查找,对于一个已经有序的数组来说效率是比较低的,可以改为二分查找。如下:

#include  < stdio.h >

int  BinarySearch( int  s[],  int  high,  int  low,  int  key);

int  main( int  argc,  char   * argv)
{
    
int a, b, c, d, i, cube[201];
    
    
for (i = 1; i < 201; i ++) cube[i] = i * i * i;
    
for (a = 6; a <= 200; a ++
        
for (b = 2; b < a; b ++{
            i 
= cube[a] - cube[b];
            
for (c = b + 1; c < a; c ++{
                d 
= BinarySearch(cube, a - 1, c + 1, i - cube[c]);
                
if (d) printf("Cube = %d, Triple = (%d,%d,%d) ", a, b, c, d);
            }

        }


    
return 0;
}


int  BinarySearch( int  s[],  int  high,  int  low,  int  key)
{
    
int mid;

    
while (low <= high) {
        mid 
= (high + low) / 2;
        
if (s[mid] == key) return mid;
        
else if (s[mid] < key) low = mid + 1;
        
else high = mid - 1;
    }


    
return 0;
}

 

 

至此,已想不出可以如何再优化,提交:

Judge Status
Problem ID
Language
Run time
Run memory
Accepted
C
00:00.10
388K

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值