题意:给你一个偶数P和二维坐标上的一些点(x, y) 权值为 x&y 让你选出其中一些点 使得权值和最大 且选中的点集中不存在这样的两点:(a, b)使得gcd(a.x^a.y^b.x^b.y, p) <= 1
思路:由于题意中已经明确说明P为偶数 而两个偶数的最大公约数永远大于1 又发现对于任意两个数A B 只要它俩奇偶性相同 则 A^B必为偶数!所以我们只需要把图中所有点的权值按照奇偶性划分 它的原型不就是二分图带权最大独立集么!! 建图方法
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define REP( i, a, b ) for( int i = a; i < b; i++ )
#define FOR( i, a, b ) for( int i = a; i <= b; i++ )
#define CLR( a, x ) memset( a, x, sizeof a )
#define CPY( a, x ) memcpy( a, x, sizeof a )
#define BUG puts( "**** BUG ****" )
typedef long long LL;
const int maxn = 500 + 10;
const int maxe = 250000 + 10;
const int INF = 0x7fffffff;
struct Edge{
int v, c, f;
int next;
Edge() {}
Edge(int v, int c, int f, int next) : v(v), c(c), f(f), next(next) {}
};
struct Point{
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
}point[maxn];
struct ISAP{
int n, s, t;
int num[maxn], cur[maxn], d[maxn], p[maxn];
int Head[maxn], cntE;
int Q[maxn], head, tail;
Edge edge[maxe];
void Init(int n){
this -> n = n;
cntE = 0;
CLR(Head, -1);
}
void Add(int u, int v, int c){
edge[cntE] = Edge(v, c, 0, Head[u]);
Head[u] = cntE++;
edge[cntE] = Edge(u, 0, 0, Head[v]);
Head[v] = cntE++;
}
void Bfs(){
CLR(d, -1);
CLR(num, 0);
d[t] = 0;
head = tail = 0;
Q[tail++] = t;
num[0] = 1;
while(head != tail){
int u = Q[head++];
for(int i = Head[u]; ~i; i = edge[i].next){
Edge &e = edge[i];
if(~d[e.v]) continue;
d[e.v] = d[u] + 1;
Q[tail++] = e.v;
num[d[e.v]] ++;
}
}
}
LL Maxflow(int s, int t){
this -> s = s;
this -> t = t;
CPY(cur, Head);
Bfs();
int u = p[s] = s;
LL flow = 0;
while(d[s] < n){
if(u == t){
int f = INF, neck;
for(int i = s; i != t; i = edge[cur[i]].v){
if(f > edge[cur[i]].c - edge[cur[i]].f){
f = edge[cur[i]].c - edge[cur[i]].f;
neck = i;
}
}
for(int i = s; i != t; i = edge[cur[i]].v){
edge[cur[i]].f += f;
edge[cur[i]^1].f -= f;
}
flow += (LL)f;
u = neck;
}
int ok = 0;
for(int i = cur[u]; ~i; i = edge[i].next){
Edge &e = edge[i];
if(e.c > e.f && d[e.v] + 1 == d[u]){
ok = 1;
cur[u] = i;
p[e.v] = u;
u = e.v;
break;
}
}
if(!ok){
int m = n - 1;
if(--num[d[u]] == 0) break;
for(int i = Head[u]; ~i; i = edge[i].next){
Edge &e = edge[i];
if(e.c - e.f > 0 && m > d[e.v]){
cur[u] = i;
m = d[e.v];
}
}
++num[d[u] = m + 1];
u = p[u];
}
}
return flow;
}
}solver;
int n, p;
Point read_Point(){
Point a;
scanf("%d%d", &a.x, &a.y);
return a;
}
int gcd(int a, int b){ return b == 0 ? a : gcd(b, a % b); }
void input(){ FOR(i, 1, n) point[i] = read_Point(); }
void solve(){
int S = 0, T = n + 1;
LL sum = 0;
solver.Init(n+2);
FOR(i, 1, n){
if((point[i].x ^ point[i].y) & 1){
solver.Add(S, i, point[i].x & point[i].y);
FOR(j, 1, n){
if(!((point[j].x^point[j].y)&1)&&gcd(point[i].x^point[i].y^point[j].x^point[j].y, p)<=1)
solver.Add(i, j, INF);
}
}
else solver.Add(i, T, point[i].x & point[i].y);
sum += (LL)(point[i].x & point[i].y);
}
printf("%lld\n", sum - solver.Maxflow(S, T));
}
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d", &n, &p)){
input();
solve();
}
return 0;
}