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. MVC-12 ActionMethodSelectorAttribute

    ActionMethodSelectorAttribute 其实微软对方法的起名都比较规范和通俗易懂的,从名字上来看就知道这是方法选择器 我们在action上加上 HttpGet.HttpPost . ...

  2. cookie的实例

    使得Cookie简化用户登陆,要求如下: 1.用户第一次登陆时需要输入用户名和密码 2.当登陆成功后,在Cookie中保存用户的登陆信息 3.设置Cookie有效期为5分钟 4.在有效期内用户再次登陆 ...

  3. Alpha版本十天冲刺——Day 9

    站立式会议 会议总结 队员 今日完成 问题 明日要做 感想 对学长说的话的感受 鲍亮 无 获取图片未解决 获取图片,发帖接口,争取完成此版本预期功能 不知不觉只剩两天时间了,这两天接连遇到发图片,获取 ...

  4. 【Alpha阶段】第三次Scrum例会

    会议信息 时间:2016.10.19 21:00 时长:0.5h 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 个人任务报告 姓名 今日已完成Issue 明日计划Issue 今日已做事务 工 ...

  5. Docker入门教程(七)Docker API

    Docker入门教程(七)Docker API [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第七篇,重点介绍了Docker Registry API和 ...

  6. setInterval js

    $('#start_scan').on('click',function(){ if(timer == undefined){ timer = setInterval(scan,1000) start ...

  7. CentOS7安装Nginx并部署

    服务器IP是192.168.36.136 1.直接yum install nginx即可 2.主配置文件是/etc/nginx/下的nginx.conf,另外一个是/etc/nginx/conf.d/ ...

  8. 编译安装chkrootkit出现的问题

    tar xf chkrootkit.tar.gz cd chkrootkit-* make sense的时候出现make: *** [strings-static] Error 1,解决办法:yum ...

  9. nginx的简单操作

    1.今天在nginx下访问403,发现图片访问出现403,其他没有问题,看来下图片的文件夹没有读写的权限,修改下权限就OK了! 2.重启nginx 在env/nginx/sbin目录下输入:nginx ...

  10. JavaScript中原型和原型链

    原型[prototype]: 为其他对象提供共享属性的对象. 每个函数都有一个原型(prototype)属性,这个属性是一个指针,指向一个对象,这个对象包含特定实例共享的一些属性和方法. 以例服人: ...