一个多月前打的,但是由于一点事情一直没写关于A题的反思,今天有空果断补上:
Problem Description
Bit operation is a common computing method in computer science ,Now we have two positive integers A and B ,Please find a positive integer C that minimize the value of the formula (A xor C) & (B xor C) .Sometimes we can find a lot of C to do this ,So you need to find the smallest C that meets the criteria .
For example ,Let’s say A is equal to 5 and B is equal to 3 ,we can choose C=1,3… ,so the answer we’re looking for C is equal to 1.
If the value of the expression is 0 when C=0, please print 1.
Input
The input file contains T test samples.(1<=T<=100)
The first line of input file is an integer T.
Then the T lines contains 2 positive integers, A and B, (1≤A,B<232)
Output
For each test case,you should output the answer and a line for each answer.
分别考虑P点作直角顶点和非直角顶点。
此篇文章想说map的用法,自定义小于运算符,使得在map中查找的时候,统一斜率的向量都会加起来,虽然在map中依然会保存多个不同的向量。
复杂度为n^2log(n)
下面是AC代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2005;
struct P
{
ll x, y;
P(ll xx=0, ll yy=0) {
x = xx; y = yy;
}
P base()const{
if (x < 0 || (x == 0 && y < 0))return P(-x, -y);
return this;
}
bool operator<(const P&b)const {
P p1 = base(); P p2 =b.base();
//如果共线,考虑是相同的索引
return p1.xp2.y<p1.yp2.x;
}
P operator-(const P&b)const {
return P(x - b.x, y - b.y);
}
}a[N],qur[N];
int n, q;
map<P, int>m;
ll ans[N];
int main() {
while (~scanf("%d%d", &n, &q)) {
memset(ans,0,sizeof(ans));
for (int i = 0; i < n; i++)scanf("%lld%lld", &a[i].x, &a[i].y);
for (int i = 0; i < q; i++)scanf("%lld%lld", &qur[i].x, &qur[i].y);
for (int i = 0; i < q; i++) {
//求解作为直角顶点
m.clear();
for (int j = 0; j < n; j++)
m[a[j] - qur[i]]++;
for (int j = 0; j < n; j++) {
P p = a[j] - qur[i];
p = P(-p.y, p.x);
ans[i] += m.count§ ? m[p] : 0;
}
//由于两条直角边都会枚举,所以除2
ans[i] /= 2;
}
for (int i = 0; i < n; i++) {
//作为非直角顶点,每次枚举点i,作为直角顶点,更新全部的q组询问点
m.clear();
for (int j = 0; j < n; j++) {
if (i != j)m[a[j] - a[i]]++;
}
for (int j = 0; j < q; j++) {
P p = qur[j] - a[i];
p = P(-p.y, p.x);
ans[j] += m.count§ ? m[p] : 0;
}
}
for (int i = 0; i < q; i++)printf("%lld\n", ans[i]);
}
return 0;
}