Counting rectangles

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

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


数长方形

如果数得足够仔细,能看出在一个3乘2的长方形网格中包含有18个不同大小的长方形,如下图所示:

尽管没有一个长方形网格中包含有恰好两百万个长方形,但有许多长方形网格中包含的长方形数目接近两百万,求其中最接近这一数目的长方形网格的面积

解题

参考博客

有下面内容:

对于任意矩形M*N

其中1*1的矩阵有M*N个

1*2的矩阵有M*(N-1)个

2*1的矩阵有(M-1)*N个

实际上只要确定小矩阵左上角顶点在大矩形中的位置,这个矩阵的位置就唯一确定了

所有在任意矩形M*N中,矩阵i*j有(M-i+1)*(N-j+1)个

所以对于M*N的矩阵总的矩阵数量是:

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

更让人想不到是是直接计算矩阵的数量:

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

Java

package Level3;
import java.util.Random; public class PE085{ static void run() {
int limit = 100;
int close = Integer.MAX_VALUE;
int area = 0;
for(int m =1;m< limit ;m++){
for(int n = 1;n< limit ;n++){
int num = grid_num(m,n);
if (num>2000000)
break;
if( Math.abs(num - 2000000 ) < Math.abs(close - 2000000)){
close = num;
area = n*m;
}
}
}
System.out.println(area);
}
public static int grid_num2(int m , int n){
int num = 0;
num = (m+1)*m*(n+1)*n/4;
return num;
}
// 2772
// running time=0s0ms
public static int grid_num(int m , int n){
int num = 0;
for(int i =1;i<= m;i++){
for(int j =1;j<= n;j++){
num += (m-i + 1)*(n - j+1);
}
}
return num;
}
// 2772
// running time=0s20ms public static void main(String[] args){
long t0 = System.currentTimeMillis();
run();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms");
}
}

你说是不是很流氓,这个规律,我怎么那么聪慧的会发现?

Python

# coding=gbk
import time as time t0 = time.time() def run():
limit = 100
close = 0
area = 0
for m in range(1,limit):
for n in range(1,limit):
num = grid_num(m,n)
if num>2000000:break
if abs(num - 2000000) < abs(close -2000000):
close = num
area = n*m
print area def grid_num(m ,n):
count = 0
for i in range(1,m+1):
for j in range(1,n+1):
count += (m-i+1)*(n-j+1)
return count run()
t1 = time.time()
print "running time=",(t1-t0),"s" #
# running time= 1.19499993324 s

Project Euler 85 :Counting rectangles 数长方形的更多相关文章

  1. Project Euler 19 Counting Sundays( 蔡勒公式计算星期数 )

    题意:在二十世纪(1901年1月1日到2000年12月31日)中,有多少个月的1号是星期天? 蔡勒公式:计算 ( year , month , day ) 是星期几 以下图片仅供学习! /****** ...

  2. project euler 19: Counting Sundays

    import datetime count = 0 for y in range(1901,2001): for m in range(1,13): if datetime.datetime(y,m, ...

  3. Python练习题 040:Project Euler 012:有超过500个因子的三角形数

    本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...

  4. Python练习题 045:Project Euler 017:数字英文表达的字符数累加

    本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...

  5. Python练习题 030:Project Euler 002:偶数斐波那契数之和

    本题来自 Project Euler 第2题:https://projecteuler.net/problem=2 # Each new term in the Fibonacci sequence ...

  6. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  7. Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.

    In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...

  8. 【Project Euler 8】Largest product in a series

    题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...

  9. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

随机推荐

  1. C#项目连接数据库的配置

    一:C# 连接SQL数据库    1.用SqlServer数据库,windows身份验证模式<add name="TestSqlSqever" providerName=&q ...

  2. 虚拟局域网VLAN

    6.5.1配置路由器广域网端口的PPP封装 (1)配置路由器A: Router>enable Router#config Router_config#hostname Router-A Rout ...

  3. C# 发邮件类可发送附件

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ne ...

  4. 编译linux内核问题

    1: openssl/opensslv.h: No such file or directory sudo apt-get install libssl-dev 2:一般配置内核树,需要先make o ...

  5. spring mvc的excel报表文件下载时流的冲突解决

    在jsp或者在servlet中有时要用到 response.getOutputStream(),但是此时会在后台报这个错误java.lang.IllegalStateException: getOut ...

  6. CentOS 6.5系统上安装MySQL数据库

    1.查看系统是否安装了MySQL      使用命令:      #rpm -qa | grep mysql 2.卸载已安装的MySQL       卸载mysql命令如下:        #rpm ...

  7. WPF 多线程处理(5)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 项目的目录: 以下是FileStroage的 ...

  8. 复习linq

    复习linq linq的英文是language integrated query.其中query的意思就是疑问或者计算机用语就是从资料库中提取信息的要求,可以理解为查询的意思.那么它翻译过来的话就是集 ...

  9. android studio 完整安装教程,已完全实践过

    直接去官方下载包含android sdk的安装包(约813M),之前就是没有包含android sdk (约214M)所以需要另外从dl-google下载android sdk,太麻烦了.下面就一步步 ...

  10. 3640: JC的小苹果 - BZOJ

    让我们继续JC和DZY的故事.“你是我的小丫小苹果,怎么爱你都不嫌多!”“点亮我生命的火,火火火火火!”话说JC历经艰辛来到了城市B,但是由于他的疏忽DZY偷走了他的小苹果!没有小苹果怎么听歌!他发现 ...