给定一个n*m的网格,求面积为奇数的正方形有多少个.

首先是n*m个面积为1的,然后剩下的要么是边长为奇数,要么被这样一个奇数边长所包围。

原因如下:

对于一个边长不平行于坐标抽的正方形,其边长一定是某个长方形的对角线,而且长方形长宽a,b一定是一奇数,一偶数,这样area = a^2+b^2才是奇数。

所以可以对任何奇数i <= min(n, m) 求出这样的边长正方形以及被包围的正方形个数。

注意对于一个奇数例如5,被包围的正方形可以是以1和4的对角线,2和3的对角线为边,这样对任何一个奇数i,被包围的正方形有i/2个,根据对称性还应该*2。

w-i+1表示宽方向能够移动的次数,l-i+1表示长度方向能够移动的次数,例如图5,长和宽方向均能移动2次。

ans = n*m(单位正方形) +

{2*(w-i+1)*(l-i+1)*(i/2) + (w-i+1)*(l-i+1)}    i为 3<=i<=min(n, m)的奇数。

后来在最后完成这篇博客的时候想到能不能把式子简化一下呢,然后化简就得到这个:ans = ∑{ (w*l+w+l+1)*i – (w+l+2)*i*i + i*i*i} i为1到min(l,w)之间的奇数。

甚至可以推广到面积为偶数的情况:

公式:ans = ∑{ (w*l+w+l+1)*i – (w+l+2)*i*i + i*i*i} i为1到min(l,w)之间的偶数。

如果是求所有的正方形个数的话,只要把i从1取到min(w,l)就行了。

注意程序中预处理一下i,i*i,i*i*i的和。

下面只上化简后公式实现的代码好了。

time:29ms

  1. //Date: 20140211
  2. #include <iostream>
  3. #include <sstream>
  4. #include <cstdio>
  5. #include <climits>
  6. #include <ctime>
  7. #include <cstring>
  8. #include <cstdlib>
  9. #include <string>
  10. #include <stack>
  11. #include <map>
  12. #include <cmath>
  13. #include <vector>
  14. #include <queue>
  15. #include <algorithm>
  16. #define esp 1e-3
  17. #define pi acos(-1.0)
  18. #define inf 0x0f0f0f0f
  19. #define pb push_back
  20. #define lson l, m, rt<<1
  21. #define rson m+1, r, rt<<1|1
  22. #define mp(a, b) make_pair((a), (b))
  23. #define in freopen("test_in.txt", "r", stdin);
  24. #define out freopen("test_out.txt", "w", stdout);
  25. #define bug puts("********))))))");
  26. #define inout in out
  27. #define stop system("pause");
  28. #define PRD(a) printf("%d\n",(a))
  29. #define PRLD(a) printf("%lld\n", (a))
  30. #define PRID(a) printf("%I64d\n", (a))
  31. #define PRU(a) printf("%u\n", (a))
  32. #define PRLU(a) printf("%llu\n", (a))
  33. #define PRIU(a) printf("%I64u\n", (a))
  34. #define SET(a, v) memset(a, (v), sizeof(a))
  35. #define READ(a, n) {REP(i, n) cin>>a[i];}
  36. #define REP(i, n) for(int i = 0; i < (n); i++)
  37. #define Rep(i, base, n) for(int i = base; i < n; i++)
  38. #define REPS(s) for(int i = 0; s[i]; i++)
  39. #define pf(x) ((x)*(x))
  40. #define Log(a, b) (log((double)b)/log((double)a))
  41. #define Srand() srand((int)time(0))
  42. #define random(number) (rand()%number)
  43. #define random_range(a, b) (int)(((double)rand()/RAND_MAX)*(b-a) + a)
  44.  
  45. /*
  46. 1. 点分治,atan和atan2返回弧度值,atan值域为(-pi/2, pi/2), atan2值域为(-pi, pi) ;
  47. 2. exp(x)用于计算e^x ;
  48. 3. log2和log10分别用来计算对数, log默认以e为底 ;
  49. 4. sort中比较函数的原型:bool cmp(const Type &a, const Type &b);
  50. 5. lower_bound和upper_bound返回的是相应下标对应的指针 ;
  51. 6. dfs时注意利用强剪枝和避免重复状态进行优化 ;
  52. 7. 尽量减少不必要的状态表示的维度 ;
  53. 8. greater<T> () less<T> () ;
  54. 9. 尽量少用strlen,尤其是在递归深度较大,字符串较长的时候,容易超时;少用memset ;
  55. 10.不要在函数里面开数组,易暴栈 ;
  56. */
  57.  
  58. using namespace std;
  59. typedef long long LL;
  60. typedef unsigned long long ULL;
  61. typedef vector<int> VI;
  62. typedef pair<int,int> pii;
  63. typedef vector<pii> VII;
  64. typedef vector<pii, int> VIII;
  65. typedef VI:: iterator IT;
  66. typedef map<string, int> Mps;
  67. typedef map<int, int> Mpi;
  68. typedef map<int, pii> Mpii;
  69. typedef map<pii, int> Mpiii;
  70.  
  71. template<class T> inline const T& Max(const T& a, const T& b)
  72. {
  73. return a < b ? b : a;
  74. }
  75. template<class T> inline const T& Min(const T& a, const T& b)
  76. {
  77. return a < b ? a : b;
  78. }
  79. template<class T> inline void checkMax(T& a, const T& b)
  80. {
  81. if(a < b) a = b;
  82. }
  83. template<class T> inline void checkMin(T& a, const T& b)
  84. {
  85. if(a > b) a = b;
  86. }
  87.  
  88. const int maxn = + ;
  89. LL f[maxn], g[maxn], h[maxn];
  90. void pre()
  91. {
  92. f[] = g[] = h[] = ;
  93. for(int i = ; i < maxn; i += ) {
  94. f[i] = f[i-] + i;
  95. g[i] = g[i-] + (LL)i*i;
  96. h[i] = h[i-] + (LL)i*i*i;
  97. }
  98. }
  99. int main()
  100. {
  101.  
  102. LL n, m;
  103. pre();
  104. while(scanf("%lld%lld", &n, &m), n||m) {
  105. LL k = min(n, m);
  106. if((k&) == )
  107. k--;
  108. LL ans = (n*m+n+m+)*f[k]-(n+m+)*g[k]+h[k];
  109. printf("%lld\n", ans);
  110. }
  111. return ;
  112. }

UVALive 6602 Counting Lattice Squares的更多相关文章

  1. UVaLive 6602 Counting Lattice Squares (找规律)

    题意:给定一个n*m的矩阵,问你里面有几面积为奇数的正方形. 析:首先能知道的是,大的矩阵是包括小的矩阵的,而且面积为奇数,我们只要考虑恰好在边界上的正方形即可,画几个看看就知道了,如果是3*3的有3 ...

  2. UVALive 5058 Counting BST 数学

    B - Counting BST Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  3. UVALive 5058 Counting BST --组合数

    题意:排序二叉树按照数插入的顺序不同会出现不同的结构,现在要在1~m选n个数,使按顺序插入形成的结构与给出的结构相同,有多少种选法. 解法:先将给出的结构插入,构造出一棵排序二叉树,再dfs统计,首先 ...

  4. UVALive 3295 Counting Triangles

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  5. UVALIVE 3571 Visible Lattice Points

    就欧拉函数然后地推一下. #include <map> #include <set> #include <list> #include <cmath> ...

  6. UVALive 6527 Counting ones dfs(水

    题目链接:点击打开链接 #include <cstdio> #include <vector> using namespace std; typedef long long l ...

  7. Individual Contest #1 and Private Training #1

    第一次的增补赛,也是第一场个人排位赛,讲道理打的和屎一样,手速题卡了好久还WA了好多发,难题又切不出来,这种情况是最尴尬的吧! Individual Contest #1: Ploblem D: 题意 ...

  8. Rocky(dfs)

    题目描述 Sylvester Stallion is an old horse who likes nothing better than to wander around in the fields ...

  9. The 2013 South America/Brazil Regional Contest 题解

    A: UVALive 6525 cid=61196#problem/A" style="color:blue; text-decoration:none">Atta ...

随机推荐

  1. c++Builder 下的文件及目录操作

    转自 http://blog.csdn.net/ktcserver/article/details/936329 一.判断目录是否存在:           C++   Builder中提供了检查文件 ...

  2. js 金额格式化

    //格式化金额 function fmoney(s, n) { n = n > 0 && n <= 20 ? n : 2; s = parseFloat((s + &quo ...

  3. java集合框架示例图

  4. HDOJ 1024 Max Sum Plus Plus -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...

  5. TreeSet集合

    TreeSet集合 TreeSet集合是一个依靠TreeMap实现的有序集合,内部存储元素是自动按照自然排序进行排列,所以如果想要保留存储时的顺序,那么就不建议使用TreeSet. TreeSet继承 ...

  6. zookeeper_笔记

    Zookeeper:(没看懂) http://cailin.iteye.com/blog/2014486/ http://agapple.iteye.com/blog/1184023 http://b ...

  7. Less使用——让老司机带你飞

    为什么我要使用Less less的作为编写css的工具插件,省时.方便.检测,具体的安装,请参考我的一篇文章<sublime text3 个人使用心得>,里面我讲解了安装方法,使用webs ...

  8. ECMAScript 6十大特性

    ES6入门 http://es6.ruanyifeng.com/ ES6排名前十的最佳特性列表 Default Parameters(默认参数) in ES6 Template Literals (模 ...

  9. prefixfree.js介绍

    假如你现在正学习着强大的Css3,你知道Css3的很多数属性为实验属性,使用他们的时候得加上各式各样的浏览器前缀.可能你默默忍受了,因为还没到统一的时间.有没想过给自己减下负,偶然间在网上看到一个js ...

  10. LNMP1.2一键安装教程

    系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian Linux系统 需要2GB以上硬盘剩余空间 128M以上内存,Xen的需要有SWAP,OpenVZ的另外 ...