BZOJ_2460_[BeiJing2011] Element_Linear basis

BZOJ_2460_[BeiJing2011] Element_Linear basis

Description

  According to legend, in ancient times, on the Magic Land in the western continent, people have mastered the
technology of refining the staff with magic ore. It was then recognized that the mana of a staff depends on the ore used.
In general, the more ores, the stronger the mana, but the opposite is true: sometimes, people
use a lot of ores in order to obtain stronger mana, but in the refining process, they find that all the magic ores have disappeared, so
the staff cannot be refined. This phenomenon is called "magic cancellation". In particular, if more than
one piece of the same ore is used in the refining process, "magic cancellation" must occur.
  Later, with the improvement of people's cognitive level, this phenomenon has been well explained. After a lot
of experiments, the famous mage Dmitri found that if each ore found now is reasonably numbered
(the number is a positive integer, called the element number of the ore), then a combination of ore will produce "magic".
"Cancellation" if and only if there is a non-empty subset whose element numbers are bitwise XORed to
zero. (If you don't know what XOR is, see the term explanation on the next page.) For example, using two
of the same ores will have "magic cancellation" because the two ores have the same element number
and zero. 
  And people have an effective way to measure the magic power, it is already known: the magic power of the synthetic staff is
equal to the sum of the magic power of each ore. People have determined the mana value of all ores found today,
and calculated the element number of each ore through experiments.
   Now, given your ore information above, please calculate the maximum
magic power of the staff that can be crafted at that time. 
 

Input

The first line contains a positive integer N, representing the number of types of ore.
  Next N lines, each line contains two positive integers Numberi and Magici, which represent the element number
and magic value of this ore.

Output

Pack only one line, one integer: maximum magic value

Sample Input

3
1 10
2 20
3 30

Sample Output

50


 Gaussian elimination to find a linear basis.

Every time the greed is the largest, the other items are eliminated.

It's similar to buying equipment.

How to eliminate the normal Gaussian elimination is how to eliminate it here.

 

Code:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <bitset>
using namespace std;
typedef long long ll;
int n,w[1050];
bitset<66>a[1050];
void Gauss() {
    int ans = 0, i, j, mx, tot = 0;
    for(i=1;i<=63;i++) {
        mx = 0; all ++;
        for(j=tot;j<=n;j++) {
            if(a[j][i]&&w[j]>w[mx]) mx=j;
        }
        if(!mx) {
            all--; continue;
        }
        ans+=w[mx];
        swap(a[mx],a[tot]); swap(w[mx],w[tot]);
        for(j=1;j<=n;j++) {
            if(a[j][i]&&j!=tot) a[j]^=a[tot];
        }
    }
    printf("%d\n",ans);
}
int main() {
    scanf("%d",&n);
    int i,j;
    ll x;
    for(i=1;i<=n;i++) {
        scanf("%lld%d",&x,&w[i]);
        for(j=62;j>=0;j--) {
            a[i][63-j]=((x>>(ll)j)&1ll);
        }
    }
    Gauss();
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325013122&siteId=291194637