题目中p为偶数,根据异或运算的性质。如果x^y为奇的数一定可以一起选择因此放在同一边,而x^y为偶的数也一定可以放在一起,也放在一起,这样就形成二分图
然后,如果两个点不能同时被选择,那么就在二分图中连上一条边,容量INF,然后在分别将两边的点与源点汇点相连,容量是对应点的值,最后求一次最小割。这个时候得到的值时最小点权覆盖集的点权和,我们需要的是最大点权独立集的点权和,方法就是用总的点权和减去最小点权覆盖集的点权即可。
George is eager about treasure hunting. One day, he got a treasure map from God (It's the welfare from God for some people).
There are N treasure points on the map. And the i-th treasure point is in (xi, yi) if we put the map in 2D-plane. God told George that he could choose some of the treasure points and got the treasure there. The value of treasure in point i is xi AND yi (in C/C++ AND means &, in Pascal AND means and). But the set (let's use T to represent it) of treasure points George chose must satisfy the following condition:
- ∀ (xi, yi), (xj, yj) (i ≠ j) ∈ T, gcd(xi ⊕ yi ⊕ xj ⊕ yj, P)>1. P is an even integer God gave George and ⊕ means exclusive or.
George want to maximize the total value he got from the treasure points he chose.
Input
Input will consist of multiple test cases and each case will consist of two lines. For each test case the program has to read the integers N and P, separated by a blank, from the first line. The next N lines each contain two integers xi and yi which represent the position of the point i. (1 ≤ N ≤ 500, 2≤ P≤ 109, 0 ≤ xi,yi ≤ 109, ∀ i,j, xi⊕ yi⊕ xj⊕ yj> 0)
Output
Please output the corresponding maximal total value, one line for one case.
Sample Input
3 4 1 2 2 4 1 3
Sample Output
1
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define MAXN 1200 #define INF 0x3FFFFFFF struct edge { int to,next; long long c; }; edge e[999999]; int que[MAXN*100]; int dis[MAXN]; int pre[MAXN]; int head[MAXN],head2[MAXN]; int st,ed; long long maxflow; int en; int n; void add(int a,int b,long long c) { e[en].to=b; e[en].c=c; e[en].next=head[a]; head[a]=en++; e[en].to=a; e[en].c=0; e[en].next=head[b]; head[b]=en++; } bool bfs() { memset(dis,-1,sizeof(dis)); que[0]=st,dis[st]=1; int t=1,f=0; while(f<t) { int j=que[f++]; for(int k=head[j];k!=-1;k=e[k].next) { int i=e[k].to; if(dis[i]==-1 && e[k].c) { que[t++]=i; dis[i]=dis[j]+1; if(i==ed) return true; } } } return false; } int update() { int p; long long flow=INF; for (int i=pre[ed];i!=-1;i=pre[i]) if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c; for (int i=pre[ed];i!=-1;i=pre[i]) e[head2[i]].c-=flow,e[head2[i]^1].c+=flow; maxflow+=flow; return p; } void dfs() { memset(pre,-1,sizeof(pre)); memcpy(head2,head,sizeof(head2)); for(int i=st,j;i!=-1;) { int flag=false; for(int k=head[i];k!=-1;k=e[k].next) if(e[k].c && (dis[j=e[k].to]==dis[i]+1) ) { pre[j]=i; head2[i]=k; i=j; flag=true; if(i==ed) i=update(); if(flag) break; } if (!flag) dis[i]=-1,i=pre[i]; } } long long dinic() { maxflow=0; while(bfs()) dfs(); return maxflow; } long long gcd(long long a,long long b){ return a%b==0?b:gcd(b,a%b); } int x[600],y[600],p; int main() { long long total; while(~scanf("%d%d",&n,&p)) { st=0,ed=n+1,en=0,total=0; memset(head,-1,sizeof(head)); for(int i=1;i<=n;i++){ scanf("%d%d",&x[i],&y[i]); int c=x[i]&y[i]; if((x[i]^y[i])&1) add(st,i,c); else add(i,ed,c); total+=c; } for(int i=1;i<=n;i++){ if(!((x[i]^y[i])&1)) continue; for(int j=1;j<=n;j++){ if((x[j]^y[j])&1) continue; if(gcd(x[i]^y[i]^x[j]^y[j],p)!=1) continue; add(i,j,INF); } } printf("%lld\n",total-dinic()); } return 0; }