2016-09-15 9 views
-1

私はTopcoderの問題を解決しようとしています。Topcoderでは、幅と長さが指定されているときに四角形の数(四角形を除く)を見つける必要があります。私のコードは、すべてのテストケースで完璧に動作しますが、最終的なテストケースの1つである592X964は、81508708664を返す必要があります。これは長時間保持できる値よりも大きいですが、 long。問題文と以下のコードを追加して、親切に行って、Javaで長い間使って上記の値を返すことができるかどうかを教えてください。戻り値がlongより大きい場合にメソッドからlongを返す方法はありますか?

/* 
Problem Statement 
     
Given the width and height of a rectangular grid, return the total number of rectangles (NOT counting squares) that can be found on this grid. 
For example, width = 3, height = 3 (see diagram below): 
__ __ __ 
|__|__|__| 
|__|__|__| 
|__|__|__| 
In this grid, there are 4 2x3 rectangles, 6 1x3 rectangles and 12 1x2 rectangles. Thus there is a total of 4 + 6 + 12 = 22 rectangles. Note we don't count 1x1, 2x2 and 3x3 rectangles because they are squares. 
Definition 
     
Class: 
RectangularGrid 
Method: 
countRectangles 
Parameters: 
int, int 
Returns: 
long 
Method signature: 
long countRectangles(int width, int height) 
(be sure your method is public) 
Limits 
     
Time limit (s): 
2.000 
Memory limit (MB): 
64 
Notes 
- 
rectangles with equals sides (squares) should not be counted. 
Constraints 
- 
width and height will be between 1 and 1000 inclusive. 
Examples 
0) 

     
3 
3 
Returns: 22 
See above 
1) 

     
5 
2 
Returns: 31 
__ __ __ __ __ 
|__|__|__|__|__| 
|__|__|__|__|__| 
In this grid, there is one 2x5 rectangle, 2 2x4 rectangles, 2 1x5 rectangles, 3 2x3 rectangles, 4 1x4 rectangles, 6 1x3 rectangles and 13 1x2 rectangles. Thus there is a total of 1 + 2 + 2 + 3 + 4 + 6 + 13 = 31 rectangles. 
2) 

     
10 
10 
Returns: 2640 

3) 

     
1 
1 
Returns: 0 

4) 

     
592 
964 
Returns: 81508708664 

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. 
*/ 
public class RectangularGrid 
{ 
    public static void main(String[] args) 
    { 
     System.out.println(countRectangles(592,964)); 
    } 

    public static long countRectangles(int m, int n) 
    { 
     long tot_rect = (long)(((m*m)+m)*((n*n)+n))/4; 
     long tot_square = 0; 
     boolean status = true; 

     while(status) 
     { 
      if(m>0 && n>0) 
      { 
       tot_square+=(long)m*n; 
       --m; 
       --n; 
      } 
      else 
      { 
       status = false; 
      } 

     } 


     return tot_rect-tot_square; 
    } 
} 
+9

「long」は2^63-1です。 – chrylis

+0

'BigDecimal'を使わないのはなぜでしょうか?あなたは本当に' long'(これは可能です)に値を格納できないと仮定していますか? –

+3

'long'は64ビットなので、-2^63と2^63-1の間の値を保持できます。 2^32-1より大きい値を保持できないというあなたの考えは間違っています。 – Jesper

答えて

5

がガベージ値を返しますがint乗算をやっているので、それは価値が長い

でホールドすることができることをしなければ、なぜ、私が見カントそれらにlongを使用したい場合は、キャストが必要です:

public static long countRectangles(int m, int n) 
{ 
    long tot_rect = ((((long)m*m)+m)*(((long)n*n)+n))/4; 
    // Note -----------^^^^^^----------^^^^^^ 

最終結果をキャストすることは、中間結果の損失に対しては何も行いません。

+0

問題を解決してくれてありがとうございました。それがどのように解決されたか説明すると多くの役に立ちます。ありがとう:) – OBX

-1

ロングその範囲があることを意味する8バイト-2^63 63^2である - 1。したがって、2^32-1よりはるかに大きいです。 4バイトの整数型で、範囲は-2^31〜2^31-1です。あなたが範囲内にいれば、あなたは完璧に上手です。

public static long countRectangles(int m, int n) 
// Note ---------------------------^^^----^^^ 
{ 
    long tot_rect = (long)(((m*m)+m)*((n*n)+n))/4; 
    // Note -----------------^^^-------^^^ 

の場合:このコードを実行している

関連する問題