UVALive 6602 Counting Lattice Squares
给定一个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
//Date: 20140211
#include <iostream>
#include <sstream>
#include <cstdio>
#include <climits>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <string>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-3
#define pi acos(-1.0)
#define inf 0x0f0f0f0f
#define pb push_back
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mp(a, b) make_pair((a), (b))
#define in freopen("test_in.txt", "r", stdin);
#define out freopen("test_out.txt", "w", stdout);
#define bug puts("********))))))");
#define inout in out
#define stop system("pause");
#define PRD(a) printf("%d\n",(a))
#define PRLD(a) printf("%lld\n", (a))
#define PRID(a) printf("%I64d\n", (a))
#define PRU(a) printf("%u\n", (a))
#define PRLU(a) printf("%llu\n", (a))
#define PRIU(a) printf("%I64u\n", (a))
#define SET(a, v) memset(a, (v), sizeof(a))
#define READ(a, n) {REP(i, n) cin>>a[i];}
#define REP(i, n) for(int i = 0; i < (n); i++)
#define Rep(i, base, n) for(int i = base; i < n; i++)
#define REPS(s) for(int i = 0; s[i]; i++)
#define pf(x) ((x)*(x))
#define Log(a, b) (log((double)b)/log((double)a))
#define Srand() srand((int)time(0))
#define random(number) (rand()%number)
#define random_range(a, b) (int)(((double)rand()/RAND_MAX)*(b-a) + a) /*
1. 点分治,atan和atan2返回弧度值,atan值域为(-pi/2, pi/2), atan2值域为(-pi, pi) ;
2. exp(x)用于计算e^x ;
3. log2和log10分别用来计算对数, log默认以e为底 ;
4. sort中比较函数的原型:bool cmp(const Type &a, const Type &b);
5. lower_bound和upper_bound返回的是相应下标对应的指针 ;
6. dfs时注意利用强剪枝和避免重复状态进行优化 ;
7. 尽量减少不必要的状态表示的维度 ;
8. greater<T> () less<T> () ;
9. 尽量少用strlen,尤其是在递归深度较大,字符串较长的时候,容易超时;少用memset ;
10.不要在函数里面开数组,易暴栈 ;
*/ using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int,int> pii;
typedef vector<pii> VII;
typedef vector<pii, int> VIII;
typedef VI:: iterator IT;
typedef map<string, int> Mps;
typedef map<int, int> Mpi;
typedef map<int, pii> Mpii;
typedef map<pii, int> Mpiii; template<class T> inline const T& Max(const T& a, const T& b)
{
return a < b ? b : a;
}
template<class T> inline const T& Min(const T& a, const T& b)
{
return a < b ? a : b;
}
template<class T> inline void checkMax(T& a, const T& b)
{
if(a < b) a = b;
}
template<class T> inline void checkMin(T& a, const T& b)
{
if(a > b) a = b;
} const int maxn = + ;
LL f[maxn], g[maxn], h[maxn];
void pre()
{
f[] = g[] = h[] = ;
for(int i = ; i < maxn; i += ) {
f[i] = f[i-] + i;
g[i] = g[i-] + (LL)i*i;
h[i] = h[i-] + (LL)i*i*i;
}
}
int main()
{ LL n, m;
pre();
while(scanf("%lld%lld", &n, &m), n||m) {
LL k = min(n, m);
if((k&) == )
k--;
LL ans = (n*m+n+m+)*f[k]-(n+m+)*g[k]+h[k];
printf("%lld\n", ans);
}
return ;
}
UVALive 6602 Counting Lattice Squares的更多相关文章
- UVaLive 6602 Counting Lattice Squares (找规律)
题意:给定一个n*m的矩阵,问你里面有几面积为奇数的正方形. 析:首先能知道的是,大的矩阵是包括小的矩阵的,而且面积为奇数,我们只要考虑恰好在边界上的正方形即可,画几个看看就知道了,如果是3*3的有3 ...
- UVALive 5058 Counting BST 数学
B - Counting BST Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit S ...
- UVALive 5058 Counting BST --组合数
题意:排序二叉树按照数插入的顺序不同会出现不同的结构,现在要在1~m选n个数,使按顺序插入形成的结构与给出的结构相同,有多少种选法. 解法:先将给出的结构插入,构造出一棵排序二叉树,再dfs统计,首先 ...
- UVALive 3295 Counting Triangles
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALIVE 3571 Visible Lattice Points
就欧拉函数然后地推一下. #include <map> #include <set> #include <list> #include <cmath> ...
- UVALive 6527 Counting ones dfs(水
题目链接:点击打开链接 #include <cstdio> #include <vector> using namespace std; typedef long long l ...
- Individual Contest #1 and Private Training #1
第一次的增补赛,也是第一场个人排位赛,讲道理打的和屎一样,手速题卡了好久还WA了好多发,难题又切不出来,这种情况是最尴尬的吧! Individual Contest #1: Ploblem D: 题意 ...
- Rocky(dfs)
题目描述 Sylvester Stallion is an old horse who likes nothing better than to wander around in the fields ...
- The 2013 South America/Brazil Regional Contest 题解
A: UVALive 6525 cid=61196#problem/A" style="color:blue; text-decoration:none">Atta ...
随机推荐
- PS定位
一.定位:按标准看谁离得近来进行覆盖:什么都没有(最远).浮动(远).定位(近) 二.position absolute 绝对定位,能堆叠在上一层下面,脱离文档流 relative 相对定位,按 ...
- 接口和抽象类:Interface、abstract _【转】
一.接口 接口是C#中很常见的工具,概念什么的就不说了,这里讲几个值得注意的小地方: 1.接口内部只能有函数.属性和事件的声明: interface IParent { void Show(); st ...
- Beyond Compare 相同文件对比结果仍显示红色 解决方案
转载:http://blog.sina.com.cn/s/blog_4d4bc1110100zj7x.html 1. 问题详细描述如下. 下图显示对比结果中,两侧的aaa.xml是一模一样,会话中 ...
- Azure Redis Cache作为ASP.NET Session状态提供程序
从上一篇博客<使用Azure Redis Cache>我们已经可以创建并使用Redis Cache为我们服务了. 作为Web开发者,我们都知道Session状态默认是保存在内存中的,它的优 ...
- HW--字符串加解密
package t0817; import java.util.Scanner; public class StringEncrypt { public static void main(String ...
- 前台页面Josn 数组在后台.cs代码中的解析
后台代码: //解析jObject字符串 json_wang 2015/01/27 //var passengerListStr = BookingTicket_PassengerList_Hidde ...
- 3D Touch ? 木有6s,也阔以玩!!!
3D Touch 之 Peek & Pop 3D Touch 是iOS9之后专为 iPhone6s 机型加入的新特性,这一新技术移植于 Mac Book 上的 ForceTouch 更准确地说 ...
- Web前端新人之CSS样式选择器
最近在学习css样式.那么我就想先整理一下css样式的选择器 规则结构: 每个规则都有两个基本部分:选择器和声明块.声明块由一个或者多个声明组成,每个声明则是一个属性—值对(property-valu ...
- Application.StartupPath同System.Environment.CurrentDirectory区别
System.Windows.Forms.Application.StartupPath:获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称. System.Environment.Curr ...
- 51nod1079中国剩余定理
/** *中国剩余定理 */ #include<iostream> #include<cstdio> #include<map> #include<cstri ...