Project Euler 85:counting Rectangles number Rectangle

Source: Internet
Author: User

Counting rectangles

By counting carefully it can be seen, a rectangular grid measuring 3 by 2 contains eighteen rectangles:

Although there exists no rectangular grid that contains exactly both million rectangles, find the area of the grid with the Nearest solution.

Number of rectangles

If you are careful enough, you can see that a 3-by-2 rectangular grid contains 18 rectangles of different sizes, as shown in:

Although none of the rectangular meshes contains exactly 2 million rectangles, there are many rectangular meshes that contain nearly 2 million rectangles, which are nearest to the area of this number of rectangular meshes.

Solving

Reference Blog

Has the following content:

For any rectangle m*n

Where the 1*1 matrix has a m*n

The 1*2 matrix has m* (N-1)

The 2*1 matrix has a (M-1) *n

In fact, the position of this matrix is determined only by determining the position of the top left vertex of the small matrix in the large rectangle.

All in any rectangular m*n, the Matrix I*j has (m-i+1) * (n-j+1)

So the total number of matrices for the M*n matrix is:

        int num = 0;          for (int i =1;i<= m;i++) {            for (int J =1;j<= n;j++) {                + = (m-i + 1) * (n-j+1);            }        }

More surprisingly, the number of matrices is calculated directly:

num = (m+1) *m* (n+1) *N/4

Java

 PackageLevel3;ImportJava.util.Random; Public classpe085{Static voidrun () {intLimit = 100; intClose =Integer.max_value; intArea = 0;  for(intM =1;m< limit; m++){             for(intn = 1;n< limit; n++){                intnum =Grid_num (m,n); if(num>2000000)                       Break; if(Math.Abs (num-2000000) < Math.Abs (close-2000000) ) {Close=num; Area= Nm;    }}} System.out.println (area); }     Public Static intGRID_NUM2 (intMintN) {        intnum = 0; Num= (m+1) *m* (n+1) *N/4; returnnum; }//2772//running time=0s0ms     Public Static intGrid_num (intMintN) {        intnum = 0;  for(intI =1;i<= m;i++){             for(intJ =1;j<= n;j++) {num+ = (m-i + 1) * (n-j+1); }        }        returnnum; }//2772//running time=0s20ms     Public Static voidMain (string[] args) {LongT0 =System.currenttimemillis ();        Run (); LongT1 =System.currenttimemillis (); Longt = T1-t0; System.out.println ("Running Time=" +t/1000+ "s" +t%1000+ "MS");    }}

You say is not very rascal, this rule, how can I be so intelligent to find?

Project Euler 85:counting Rectangles number Rectangle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.