算法:部分网格计数问题
部分网格数问题 每个点都标记为 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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
公式
N*(N-1)/2
的含义是从1到N的所有数字的总和。如果您查看一行,例如
0001110000000000
,那么有将是子集1
、11
、111
,它们的和是 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 sets1
,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.