算法:部分网格计数问题

发布于 2025-01-11 18:32:15 字数 806 浏览 4 评论 0原文

部分网格数问题 每个点都标记为 1 或 0。 本例中,求四个角都为 1 的子网格个数的问题

每行以位集形式表示,在搜索每一行时,当公共列为 时,将计数相加通过与操作的比较来画出。 最后,count(count-1)/2 子格,其中第一行是 a,最后一行是 b。

我不明白如何使用公式 count(count-1)/2 获取子晶格的数量。

  bitset<5> row[5];
  row[0] = (1 << 3) + (1 << 0);
  row[1] = (1 << 3) + (1 << 2);
  row[2] = (1 << 4);
  row[3] = (1 << 3) + (1 << 2) + (1 << 0);
  row[4] = 0;
  int count = 0;
  for (int a = 0; a < 4; a++) {
    for (int b = a + 1; b < 5; b++) {
      int count_row = (row[a] & row[b]).count();
      count += count_row;
    }
  }

  count = count * (count - 1) / 2;

在此处输入图像描述

In partial grid count problem
Each point is marked with 1 or 0. In this case, the problem of finding the number of subgrid with 1 in all four corners

Each row is expressed in bitset form, and while searching each row, the count is added when the common column is painted by comparison with the and operation.
Finally,count(count-1)/2 the sublattice where the first row is a and the last row is b.

I don't understand how to get the number of sublattices with the formula count(count-1)/2.

  bitset<5> row[5];
  row[0] = (1 << 3) + (1 << 0);
  row[1] = (1 << 3) + (1 << 2);
  row[2] = (1 << 4);
  row[3] = (1 << 3) + (1 << 2) + (1 << 0);
  row[4] = 0;
  int count = 0;
  for (int a = 0; a < 4; a++) {
    for (int b = a + 1; b < 5; b++) {
      int count_row = (row[a] & row[b]).count();
      count += count_row;
    }
  }

  count = count * (count - 1) / 2;

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮凉 2025-01-18 18:32:15

公式N*(N-1)/2的含义是从1到N的所有数字的总和。

如果您查看一行,例如0001110000000000,那么有将是子集
111111,它们的和是 1+2+3,即所有数字 1 到 3 的和。
我想这就是文本的意思。

但是,这只计算从左侧开始的子集,而不计算中间的单个 1 和右侧的单个 1 以及右侧的 11。

所以我想我知道文本的含义 - 但认为这是错误的。

The meaning of the formula N*(N-1)/2 is the sum of all numbers from 1 to N.

If you look at a row e.g. 0001110000000000, then there will be sub sets
1, 11, 111, the sum of those is 1+2+3, i.e. the sum of all numbers 1 to 3.
I think that is what the text means.

However, that only counts the subsets which start on the left, not counting the middle single 1 and the right single 1 and the right 11.

So I think I know what the text means - but think it is wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文