当前位置:网站首页>洛谷P2241统计方形
洛谷P2241统计方形
2022-04-23 00:29:00 【Soraku7】
题目背景
1997年普及组第一题
题目描述
有一个n×m 方格的棋盘,求其方格包含多少正方形、长方形(不包含正方形)。
输入格式
一行,两个正整数 n,m(5000n≤5000,m≤5000)。
输出格式
一行,两个正整数,分别表示方格包含多少正方形、长方形(不包含正方形)。
输入输出样例
输入
2 3
输出
8 10
题目简单易懂,可是想要做这道题目还是需要一些小技巧的。
这里就拿题目的例子进行举例,有一个2 * 3的棋盘,求他的正方形个数和长方形个数

先看1 * 1的正方形个数 ,显然有6个 , 2 * 2的正方形总共有2个
再看长方形1 * 2的长方形有3个 , 2 * 1 的长方形个数有4个,1 * 3的长方形个数有2个,最后仅剩下一个大的长方形是2 * 3。
如果光是这样看,那么我们好像并不能找到什么规律 。现在我们需要找到一个能求出正方形长方形的规律 。这里是这样想的,我们带入一个坐标轴,求出每个点和正方形长方形的个数关系

现在从(1 , 1)开始看起 , 正方形个数为1 , 长方形个数为0,总和为1
(2 , 1),此时相较于(1 , 1)比起来,正方形个数多了1 , 长方形个数多了1 , 总和多了2
(3 , 1),此时相较于(2 , 1)比起来,正方形个数多了1 , 长方形个数多了2 ,总和多了3
现在可以来总结一下规律 , 此规律如果有不了解的,或者是不太懂的可以自己根据我所推出的规律进行测试
正方形增加的个数应该为(x , y)坐标中较小的一个,而正方形和长方形所总共所增加的数应该为x * y。
看来有这样简单方便的规律 , 我们好像就可以算出棋盘中正方形的个数了。长方形的个数当然我们可以使用所有矩形的个数减去正方形的个数,自然就是长方形的个数了。
这里代码比较简单
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
long long n, m;
long long squ = 0, sum = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
squ += min(i, j);
sum = sum + i * j;
}
}
cout << squ << " " << sum - squ;
return 0;
}
, 就不过多的进行注释了
版权声明
本文为[Soraku7]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Solaku7/article/details/124307526
边栏推荐
- ArcGIS 计算地块容积率
- (to) excel 2016 does not have enough memory or disk space to open excel
- 倍福TwinCAT两台CX控制器之间做ADS通信
- 程序员怎么找工作?程序员找工作越来越难怎么办?
- API post, the interface document generation tool, is amazing
- EXCEL IF函数的简单使用
- 我和TiDB的故事 | 毫无准备地不期而遇,却想说与你相遇好幸运
- Learning Android 8 from scratch -- threads and services
- js -对连续月份数据做处理拆分
- Difference between Beifu twincat1260 and tf5010 authorization
猜你喜欢

Two methods are used to realize mongodb self increment ID, and the second one is recommended

scanpy find resolution

ArcGIS 制作3D遥感影像图

JVM performance tuning 1

ArcGIS urban living area land suitability evaluation (V)
(turn) how is word2016 compatible with MathType

Utiliser dottrace 6. 0 performance, analyse de mémoire

【图像分类】 一文读懂AlexNet

ArcMap spatial connection and intersection tool

EXCEL IF函数的简单使用
随机推荐
将 AWS S3 数据迁移至 TiDB Cloud 集群
A tikv hard disk usage problem caused by GC not working caused by ticdc exception
Generation and mutual conversion of ArcGIS tin ground surface and grid ground surface
网线IP配置
MySQL installation and basic use tutorial
El2124 module wiring and pin meaning
(transfer) Aspose Working with document in words Programming Guide
[essay contest] the first essay contest of tidb community column. Come and gather all the surrounding areas at one time!
Ads communication between two CX controllers of Beifu TwinCAT
Analytic hierarchy process software operation steps (Yaahp)
(转)SSH 登录配置与文件传输
Communication error between Willem touch screen and Beifu PLC adsparsesymbol invalid array index
MySQL -- table operation
ArcGIS 城市生活区用地适宜性评价(三)
对Indexlookup的理解误区
接口文档生成工具ApiPost 绝了
Beifu scope view oscilloscope reads the signal of configuration IO
repeat_ String delete some characters
倍福EL6631和西门子通信profinet出现报错解决方案
ArcGIS urban living area land suitability evaluation (IV)