当前位置: 首页 > news >正文

Codeforces Testing Round 1 B. Right Triangles 题解 组合数学

Right Triangles

题目描述

You are given a n × m n×m n×m field consisting only of periods (‘.’) and asterisks (‘*’). Your task is to count all right triangles with two sides parallel to the square sides, whose vertices are in the centers of ‘*’-cells. A right triangle is a triangle in which one angle is a right angle (that is, a 90 degree angle).

输入格式

The first line contains two positive integer numbers n n n and m m m ( 1 < = n , m < = 1000 1<=n,m<=1000 1<=n,m<=1000 ). The following n n n lines consist of m m m characters each, describing the field. Only ‘.’ and ‘*’ are allowed.

输出格式

Output a single number — total number of square triangles in the field. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).

样例 #1

样例输入 #1

2 2
**
*.

样例输出 #1

1

样例 #2

样例输入 #2

3 4
*..*
.**.
*.**

样例输出 #2

9

原题

Codeforces——传送门
洛谷——传送门

思路

题目要求是求图中三个顶点均为 ′ ∗ ′ '*' 字符的直角三角形个数。我们发现,直角三角形有两条直角边和一个拐点,而两条直角边和一个拐点唯一确定了一个直角三角形。所以如果我们知道了以任意一点为拐点的行直角边个数和列直角边个数,便可以确定以该点为拐点的直角三角形个数,从而计算出答案。那么思路便是先存储每行的 ′ ∗ ′ '*' 点个数和每列的 ′ ∗ ′ '*' 点个数,再遍历图,找到所有能构成直角三角形的点,接着以其为拐点,计算行直角边个数与列直角边个数的组合数并统计进 a n s ans ans 中即可。

代码

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;int main()
{ios::sync_with_stdio(0);cin.tie(0);int n, m;cin >> n >> m;vector<vector<char>> g(n, vector<char>(m)); // 存图vector<int> hor(n, 0), ver(m, 0);           // 统计每行的'*'点数和每列的'*'点数for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){cin >> g[i][j];if (g[i][j] == '*'){hor[i]++;ver[j]++;}}}i64 ans = 0; // 直角三角形个数// 遍历图,找到所有能构成直角三角形的点for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (g[i][j] == '*') // 让其成为拐点,则其与该行的其他'*'构成一条直角边,与该列的其他'*'构成另一条直角边{ans += (hor[i] - 1) * (ver[j] - 1); // 行上直角边与列上直角边的组合个数即为以该点为拐点的直角三角形个数}}}cout << ans << '\n';return 0;
}
http://www.lryc.cn/news/388562.html

相关文章:

  • 怎样将word默认Microsoft Office,而不是WPS
  • C语言之进程的学习2
  • web使用cordova打包Andriod
  • 内卷情况下,工程师也应该了解的项目管理
  • 【解锁未来:深入了解机器学习的核心技术与实际应用】
  • 1-3.文本数据建模流程范例
  • 【FFmpeg】avformat_alloc_output_context2函数
  • Flask 缓存和信号
  • 基于weixin小程序农场驿站系统的设计
  • JAVA将List转成Tree树形结构数据和深度优先遍历
  • 设计模式——开闭、单一职责及里氏替换原则
  • 代码随想录算法训练营第59天:动态[1]
  • jvm性能监控常用工具
  • ISP IC/FPGA设计-第一部分-SC130GS摄像头分析-IIC通信(1)
  • HTTP协议头中X-Forwarded-For是能做什么?
  • Linux高并发服务器开发(八)Socket和TCP
  • 力扣第220题“存在重复元素 III”
  • Qt实战项目——贪吃蛇
  • Windows 10,11 Server 2022 Install Docker-Desktop
  • C++中的RAII(资源获取即初始化)原则
  • 【机器学习】Whisper:开源语音转文本(speech-to-text)大模型实战
  • ubuntu22.04 编译安装openssl C++ library
  • 百度Agent初体验(制作步骤+感想)
  • 7-491 3名同学5门课程成绩,输出最好成绩及所在的行和列(二维数组作为函数的参数)
  • OpenCloudOS开源的操作系统
  • 排序题目:多数元素 II
  • <电力行业> - 《第1课:电力行业的五大四小》
  • 数据库定义语言(DDL)
  • mybatis实现多表查询
  • 数据结构:队列详解 c++信息学奥赛基础知识讲解