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;
}
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值