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. MySql的一些操作

    我们安装mysql时一开始root用户如果没设置的话是可以没有密码的,所以,如果需要设置密码,则 格式:mysql> set password for 用户名@localhost = passw ...

  2. C#获取外网IP

    思路是通过WebRequest连接一些网上提供IP查询服务的网站,下载到含有你的IP的网页,然后用正则表达式提取出IP来 class Program { static void Main(string ...

  3. 屠蛟之路_重登数据库大山_SecondDay

    重登数据库大山 屠蛟少年们痛定思痛,(2.0正式改名,咳咳),整顿之后,开启新的屠蛟之路. 然而现实摆在他们面前的是,如果想要往东追击beta怪蛟,就要重新攀登上绵亘数千里.有万丈高的数据库大山脉.不 ...

  4. HD2767Proving Equivalences(有向图强连通分量+缩点)

    题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...

  5. gcc编译与gdb调试简要步骤

    http://blog.chinaunix.net/uid-24103300-id-108248.html 一.Linux程序gcc编译步骤: Gcc编译过程主要的4个阶段: l 预处理阶段,完成宏定 ...

  6. wpf arcgis engine 当前没有或未启用Spatial Analyst许可解决办法

    用wpf 在做叠加分析时 遇到了一个错误:“ERROR 010096:当前没有或未启用Spatial Analyst许可”:在环境中把这个Spatial Analyst扩展功能给勾了也不能解决,现在把 ...

  7. php 设计模式 例子

    加载类:include("./Ren.class.php");include "./Ren.class.php"; require("./Ren.cl ...

  8. MongoDb学习1

    目标框架必须是 4.5以上,最新MongoDb.Driver 是 2.2.4(与MongoDb.Driver 的1.x版本差别较大) 官方文档 http://mongodb.github.io/mon ...

  9. kail2 linux 安装vmware tools

    kali进去后,安装vmtools有点蛋疼,中途会问你要编译内核模块所需要的内核头文件,但是没有默认安装的.安装headers时又因为没有源下载不了,所以我们要做一些准备工作. 首先打开shell,我 ...

  10. Python基本运算符

    Python基本运算符 什么是操作符? 简单的回答可以使用表达式4 + 5等于9,在这里4和5被称为操作数,+被称为操符. Python语言支持操作者有以下几种类型. 算术运算符 比较(即关系)运算符 ...