【CS Round #44 (Div. 2 only) D】Count Squares

【链接】点击打开链接


【题意】


给你一个0..n和0..m的区域.
你可以选定其中的4个点,然后组成一个正方形.
问你可以圈出多少个正方形.
(正方形的边不一定和坐标轴平行)

【题解】


首先,考虑只和坐标轴平行的情况
长度为L的正方形有(N-L+1)*(M-L+1)个.
然后引入一个bounding box的概念。
一个正方形的bounding box,指的是一个最小的包括了正方形的4个点的一个矩形.
矩形的边都是和坐标轴平行的
正方形的bounding box都是正方形!
这样,我们刚才枚举的和坐标轴平行的正方形就有用了。
相当于我们在枚举所有正方形的bouding box.
那么一个长为L的boungding box正方形,里面包含多少个小正方形呢?
答案是L个,因为我们能在一条边里面的L-1个点中任选一个点,一旦这个一个点选定之后,boungding box对应的正方形的其他3个点就确定了.
可以保证其他3个点是在这个boungding box的另外3条边上的
然后还有这个bounding box本身一个正方形.
这样我们只要枚举boungding box的长度L,算出它的个数有多少个,乘上L加到答案里就好。

【错的次数】


0

【反思】


用bounding box的概念,来算出所有的正方形。
正方形的boungding box还是一个正方形.
整点上,枚举出了bouding box,则bounding box为这个的正方形个数就是 boundingbox的长度.

【代码】

/*

*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <cstdlib>
#include <cmath>
#include <bitset>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb emplace_back
#define fi first
#define se second
#define ld long double
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define rf(x) scnaf("%lf",&x)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
#define sz(x) ((int) x.size())
#define ld long double

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

//mt19937 myrand(time(0));
//int get_rand(int n){return myrand()%n + 1;}
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;
const LL MOD = 1e9 + 7;
LL ans = 0;

int n, m;

int main() {
	//Open();
	//Close();
	ri(n), ri(m);
	rep1(i, 1, min(n, m)) {
		ans += (LL)i*(n - i + 1)*(m - i + 1);
		ans %= MOD;
	}
	ol(ans); puts("");
	return 0;
}


原文地址:https://www.cnblogs.com/AWCXV/p/7626063.html