SRM 146 2 500RectangularGrid


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

  • ClassRectangularGrid
  • MethodcountRectangles
  • Parametersint , int
  • Returnslong long
  • Method signaturelong 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.

Test cases

    1.  
      • width3
      • height3
       

      Returns22

       
      See above
    2.  
      • width5
      • height2
       

      Returns31

       
       __ __ __ __ __
      |__|__|__|__|__|
      |__|__|__|__|__|

      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.

    3.  
      • width10
      • height10
       

      Returns2640

    4.  
      • width1
      • height1
       

      Returns0

    5.  
      • width592
      • height964
       

      Returns81508708664

       #include <cstdio>
      #include <cmath>
      #include <cstring>
      #include <ctime>
      #include <iostream>
      #include <algorithm>
      #include <set>
      #include <vector>
      #include <sstream>
      #include <typeinfo>
      #include <fstream> using namespace std;
      typedef long long ll ;
      class RectangularGrid {
      public:
      long long countRectangles(int l , int r) {
      if (l > r) swap (l , r ) ;
      // printf ("l = %d , r = %d\n" , l , r ) ;
      ll all = (1ll *l*l*r*r + 1ll *l*r*r + 1ll *r*l*l + 1ll *r*l) / ;
      ll sum = 1ll * l * (l - ) * (l + ) / + 1ll * (r - l + ) * l * (l + ) / ;
      // printf ("all = %lld , sum = %lld\n" , all , sum) ;
      return all - sum ;
      }
      }; // CUT begin
      ifstream data("RectangularGrid.sample"); string next_line() {
      string s;
      getline(data, s);
      return s;
      } template <typename T> void from_stream(T &t) {
      stringstream ss(next_line());
      ss >> t;
      } void from_stream(string &s) {
      s = next_line();
      } template <typename T>
      string to_string(T t) {
      stringstream s;
      s << t;
      return s.str();
      } string to_string(string t) {
      return "\"" + t + "\"";
      } bool do_test(int width, int height, long long __expected) {
      time_t startClock = clock();
      RectangularGrid *instance = new RectangularGrid();
      long long __result = instance->countRectangles(width, height);
      double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
      delete instance; if (__result == __expected) {
      cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
      return true;
      }
      else {
      cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
      cout << " Expected: " << to_string(__expected) << endl;
      cout << " Received: " << to_string(__result) << endl;
      return false;
      }
      } int run_test(bool mainProcess, const set<int> &case_set, const string command) {
      int cases = , passed = ;
      while (true) {
      if (next_line().find("--") != )
      break;
      int width;
      from_stream(width);
      int height;
      from_stream(height);
      next_line();
      long long __answer;
      from_stream(__answer); cases++;
      if (case_set.size() > && case_set.find(cases - ) == case_set.end())
      continue; cout << " Testcase #" << cases - << " ... ";
      if ( do_test(width, height, __answer)) {
      passed++;
      }
      }
      if (mainProcess) {
      cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
      int T = time(NULL) - ;
      double PT = T / 60.0, TT = 75.0;
      cout << "Time : " << T / << " minutes " << T % << " secs" << endl;
      cout << "Score : " << * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl;
      }
      return ;
      } int main(int argc, char *argv[]) {
      cout.setf(ios::fixed, ios::floatfield);
      cout.precision();
      set<int> cases;
      bool mainProcess = true;
      for (int i = ; i < argc; ++i) {
      if ( string(argv[i]) == "-") {
      mainProcess = false;
      } else {
      cases.insert(atoi(argv[i]));
      }
      }
      if (mainProcess) {
      cout << "RectangularGrid (500 Points)" << endl << endl;
      }
      return run_test(mainProcess, cases, argv[]);
      }
      // CUT end

      已知一个长 l , 宽 r 的由1×1的单位矩阵构成的矩阵,问其中有多少个长方形?

      那么我们只要求出其中矩阵的总个数n , 和正方形数m,然后n - m就是答案。

      n= l*r + l×(r×(r-1))/2 + r×(l×(l-1))/2 + l×r×(l-1)×(r-1)/4

       =(l×l×r×r+l×r×r+r×l×l+r×l)/4;

      m=2×(1+2×3/2+4×3/2+……+l×(l-1)/2) + (r-l+1)×l×(l+1)/2;

       =l×(l+1)×(2×l+1)/6 + (r-l)×l×(l+1)/2;

      PS:

      1^2 + 2^2 + 3^2 + 4^2 + 5^2 +……+n^2 = n*(n + 1)*(2*n+1)/6;

      证明:(来自百度文库)

      想像一个有圆圈构成的正三角形,

      第一行1个圈,圈内的数字为1

      第二行2个圈,圈内的数字都为2,

      以此类推

      第n行n个圈,圈内的数字都为n,

      我们要求的平方和,

      就转化为了求这个三角形所有圈内数字的和。

      设这个数为r

      下面将这个三角形顺时针旋转120度,

      得到第二个三角形

      再将第二个三角形顺时针旋转120度,得到第三个三角形.

      然后,将这三个三角形对应的圆圈内的数字相加,

      我们神奇的发现所有圈内的数字都变成了

      2n+1

      而总共有几个圈呢,这是一个简单的等差数列求和

      1+2+……+n=n(n+1)/2

      于是

      3r=[n(n+1)/2]*(2n+1)

      r=n(n+1)(2n+1)/6

tc 146 2 RectangularGrid(数学推导)的更多相关文章

  1. 借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5

    上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.p ...

  2. 关于不同进制数之间转换的数学推导【Written By KillerLegend】

    关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...

  3. UVA - 10014 - Simple calculations (经典的数学推导题!!)

    UVA - 10014 Simple calculations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  4. 『sumdiv 数学推导 分治』

    sumdiv(POJ 1845) Description 给定两个自然数A和B,S为A^B的所有正整数约数和,编程输出S mod 9901的结果. Input Format 只有一行,两个用空格隔开的 ...

  5. LDA-线性判别分析(二)Two-classes 情形的数学推导

    本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...

  6. leetcode 343. Integer Break(dp或数学推导)

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  7. [hdu5307] He is Flying [FFT+数学推导]

    题面 传送门 思路 看到这道题,我的第一想法是前缀和瞎搞,说不定能$O\left(n\right)$? 事实证明我的确是瞎扯...... 题目中的提示 这道题的数据中告诉了我们: $sum\left( ...

  8. ZOJ3329(数学推导+期望递推)

    要点: 1.期望的套路,要求n以上的期望,则设dp[i]为i分距离终点的期望步数,则终点dp值为0,答案是dp[0]. 2.此题主要在于数学推导,一方面是要写出dp[i] = 什么,虽然一大串但是思维 ...

  9. [国家集训队]整数的lqp拆分 数学推导 打表找规律

    题解: 考场上靠打表找规律切的题,不过严谨的数学推导才是本题精妙所在:求:$\sum\prod_{i=1}^{m}F_{a{i}}$ 设 $f(i)$ 为 $N=i$ 时的答案,$F_{i}$ 为斐波 ...

随机推荐

  1. MVC5-4 ViewResult

    ViewResult 之前已经分析了很多个Result,但是并没有分析我们最常用的ViewResult.因为它牵扯到了Razor引擎,所以需要单独的拿出来去讲. 之前在学习的时候,老师总会和我们说当你 ...

  2. Unity3d5.0 新UI之2048

    因为汽车系统没写出来所以,纠结之中,弄了弄新版本的UI. 做了个2048. 新版本的unity的UI必须以Canvas为基底来呈现,如果没有加画布的话可是显示不出来东西的哦. 而且作为UI上的所有组件 ...

  3. UML用例图

  4. 全栈必备 Linux 基础

    Linux 几乎无处不在,不论是服务器构建,还是客户端开发,操作系统的基础技能对全栈来说都是必备的.系统的选择Linux发行版本可以大体分为两类,一类是商业公司维护的发行版本,一类是社区组织维护的发行 ...

  5. UVA11136Hoax or what( multiset的应用)

    题目链接 题意:n天,每天往一个箱子里放m个数,放完之后取最大的Max和最小的min做差,并把这两个数去掉,求n天之后的和 multiset 和 set的原理是相似的,multiset可以存多个相同的 ...

  6. IPsec 学习笔记

    工作中需要,参考网上的资料对IPSecVPN进行学习,并通过博客记录下一些知识点作为学习记录和后续复习的材料. Transport Layer (TLS) 其中主要参考了以下文档: http://ww ...

  7. 使用canvas绘制时钟

    使用canvas绘制时钟  什么使canvas呢?HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.<canvas> 标签只是图 ...

  8. linux安装ftp组件

    1   安装vsftpd组件 linux系统安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件. [root@bogon ~]# yum -y install v ...

  9. CentOS7+hadoop2.6.4+spark-1.6.1

    环境: CentOS7 hadoop2.6.4已安装两个节点:master.slave1 过程: 把下载的scala.spark压缩包拷贝到/usr/hadoop-2.6.4/thirdparty目录 ...

  10. Unity3D Multi-Compile Shader

    http://www.martinpalko.com/muli-compile-unity/ http://forum.unity3d.com/threads/tutorial-shade-more- ...