洛谷 - p2241 统计方形 (数学)

题目传送
题意:
在这里插入图片描述

思路:
一个长为2,宽为3的方形中:
长为2,宽为1的长方形的数量为:(2 - 0) * (3 - 1) + (2 - 1) * (3 - 0)

正方形边长为1的数量为:
(2-0) * (3-0)
边长为2的数量为:
(2-1) * (3-1)

子矩形的数量为原矩形的的长和宽减去一定长度来获得的,且长和宽减去的长度不能相同的为长方形,相同的为正方形,

AC代码

#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 1e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const double EEE = exp(1);
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    //    freopen("input.txt","r",stdin);
    //    freopen("output.txt","w",stdout);
    int n,m;
    cin >> n >> m;
    ll a = 0,b = 0;
    for(int i = 0;i < n;i++)
        for(int j = 0;j < m;j++)
            i == j ? a += (n-i)*(m-j) : b += (n-i)*(m-j);
    cout << a << " " << b << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷 P1681 最大正方形II 是一个动态规划问题,要求给定一个由 '0' 和 '1' 组成的矩阵,找出其中最大的正方形,并输出其边长。 以下是一个 C++ 编写的解答示例: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int maximalSquare(vector<vector<char>>& matrix) { int rows = matrix.size(); if (rows == 0) return 0; int cols = matrix[0].size(); vector<vector<int>> dp(rows + 1, vector<int>(cols + 1, 0)); int maxSide = 0; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= cols; j++) { if (matrix[i-1][j-1] == '1') { dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1; maxSide = max(maxSide, dp[i][j]); } } } return maxSide * maxSide; } int main() { int n, m; cin >> n >> m; vector<vector<char>> matrix(n, vector<char>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> matrix[i][j]; } } cout << maximalSquare(matrix) << endl; return 0; } ``` 在上述代码中,我们首先定义了一个名为 `maximalSquare` 的函数,该函数接受一个二维字符矩阵 `matrix` 作为参数,返回最大正方形的边长。 在 `main` 函数中,我们首先从标准输入读取矩阵的行数和列数,并创建一个大小为 `n x m` 的二维字符矩阵。然后,我们按行读取矩阵的数据,并调用 `maximalSquare` 函数进行求解。最后,输出最大正方形的边长。 在动态规划的解法中,我们使用一个二维数组 `dp` 来记录以当前位置为右下角的最大正方形的边长。遍历矩阵中的每个元素,如果当前元素为 '1',则根据其左方、上方和左上方的最大正方形边长计算出当前位置的最大正方形边长,并更新 `dp` 数组和最大边长变量。 请注意,以上代码仅为示例,可能需要根据具体题目要求进行适当修改。同时,为了简化示例,未进行输入验证,请确保输入的矩阵符合题目要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值