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. 【Beta版本】冲刺计划及安排

    目录 一.Beta的初步完善 二.团队分工的改进 三.工具流程的改进 四.冲刺阶段的计划与安排 五.关于组长是否重选 六.附录 队伍:606notconnected 成员:031401433 张斯巍  ...

  2. hdu 2007 - 平方和与立方和

    题目大意: 给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和. 解答: 坑你没商量!要考虑输入数a,b的大小.如果a>b,需要交换a,b的值. 1: #include<s ...

  3. 用css进行布局

     用css进行布局 一,开始布局的注意事项 1.作为最佳实践,应把html(内容)和css(显示)分离: 2.网站设计主要有两大类型:固定宽度(基于像素)和响应式(也称流式,使用百分数定义) 二,构建 ...

  4. WinForm------TreeListLookUpEdit控件的使用

    1.数据库添加表dbo.Graduation 2.从工具栏拖出TreeListLookUpEdit控件,修改部分属性 Display Name:选中后显示在控件的值 Value Member:C#代码 ...

  5. JavaWeb学习笔记——第一个JSP文件

    必须加上第一句以用来指定编码,否则会出现乱码 <%@ page language="java" import="java.util.*" contentT ...

  6. 【C++11】新特性——auto的使用

    [C++11]新特性——auto的使用 C++11中引入的auto主要有两种用途:自动类型推断和返回值占位.auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除.前后 ...

  7. 兼容IE的写法收集||bug修复

    这篇文章实时更新 属于IE的专属写法 其中,S表示Standards Mode即标准模式,Q表示Quirks Mode,即兼容模式 hack 示例 IE6(S) IE6(Q) IE7(S) IE7(Q ...

  8. 修改 页面中默认的select样式

    select样式定制: select { /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ border: solid 1px #000; /*很关键:将默认的select选择 ...

  9. yourphp问题中心

    1.前台读不出字段.数据库列表有的,但前台就适读不出来. 2.浏览次数+1与+2的问题 浏览+ <a href="{:URL('User-Register/index')}" ...

  10. python 函数基础介绍

    函数是对程序逻辑进行结构化或过程化的一种编程方法.能将整块代码巧妙地隔离成易于管理的小块,把重复代码放在函数中而不是进行大量的拷贝. 一.函数创建 def 函数创建格式如下: def function ...