简体   繁体   中英

Algorithm : partial grid count problme

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;

在此处输入图像描述

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 eg 0001110000000000 , then there will be sub sets 1 , 11 , 111 , the sum of those is 1+2+3, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM