题意分析
给定 $n$ 个点,每个点有权值 $x_i$。
对于整数 $u,v\in[1,n]$,若 $u\oplus v$ 在二进制下有奇数个 $1$,在 $u,v$ 间建边。
求最终边的数量。
$\mathcal O(n^2)$ 暴力做法
首先这个图明显就是个障眼法。
定义 $count(x)$ 表示 $x$ 的二进制上 $1$ 的个数,时间复杂度 $\mathcal O(\log n)$。
枚举 $u,v$ 的复杂度:$\mathcal O(n^2)$。
总时间复杂度:$\mathcal O(n^2\log n)$。
期望得分:$\text{60pts}$。
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;
const int N=1e7,V=1e9;
int n,x[N+1];
void scan(){
int a,b,c,d;
scanf("%d %d %d %d %d %d",&n,&a,&b,&c,&d,x);
for(int i=1;i<=n;i++){
x[i]=(1ll*a*x[i-1]%d*x[i-1]%d+1ll*b*x[i-1]%d+c)%d;
}
}
int count(int x){
int cnt=0;
while(x){
cnt+=(x&1);
x>>=1;
}return cnt;
}
int main(){
/*freopen("test.in","r",stdin);
freopen("test.out","w",stdout);*/
scan();
int ans=0;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(count(x[i]^x[j])&1)ans++;
}
}printf("%d\n",ans);
/*fclose(stdin);
fclose(stdout);*/
return 0;
}
$\mathcal O(n\log n)$ 正解做法
考虑优化。
仍然定义 $count(x)$ 表示 $x$
先说结论:当且仅当 $count(x),count(y)$ 一奇一偶时,$count(x \oplus y)$ 才为奇数。
证明
我们可以将 $x,y$ 都补全到 $31$ 位。
令 $count(x)$ 为奇数,$count(y)$ 为偶数(否则同理)。
那么,$x$ 上有奇数个 $1$,$y$ 上有 $31-count(y)$ 个 $1$,而 $31-count(y)$ 是奇数。
这些 $1$ 不重叠的个数为偶数。
同理,$0$ 不重叠的个数为奇数。
则最终不同的个数为 $0,1$ 分别不重叠个数相加为奇数,即 $count(x\oplus y)$ 为奇数。
那么我们先分别把 $count(a_i)$ 为奇数的数量 $odd$ 和为偶数的数量 $even$ 都求出来,最终答案即 $odd\times even$。
AC 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;
const int N=1e7,V=1e9;
int n,x[N+1];
void scan(){
int a,b,c,d;
scanf("%d %d %d %d %d %d",&n,&a,&b,&c,&d,x);
for(int i=1;i<=n;i++){
x[i]=(1ll*a*x[i-1]%d*x[i-1]%d+1ll*b*x[i-1]%d+c)%d;
}
}
int count(int x){
int cnt=0;
while(x){
cnt+=(x&1);
x>>=1;
}return cnt;
}
int main(){
/*freopen("test.in","r",stdin);
freopen("test.out","w",stdout);*/
scan();
int ans=0,odd=0,even=0;
for(int i=1;i<=n;i++){
if(count(x[i])&1)odd++;
else even++;
}printf("%lld\n",1ll*odd*even);
/*fclose(stdin);
fclose(stdout);*/
return 0;
}