跳到正文

题解

题解:P13685 【MX-X16-T3】「DLESS-3」XOR and Impossible Problem

kongliheng约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月20日
篇幅
约 3 分钟
题目
P13685

版本

  1. v1当前

题目传送门

题目大意:

给定非负整数序列,要求计算所有两两元素异或结果的乘积,并对 2642^{64} 取模。

思路分析:

我们可以直接 ii 从 11 到 nn,jj 从 i+1i+1 到 nn 累加 ai⊕aja_i\oplus a_j,但是 100%100 \% 数据范围是 1≤T≤1051\le T\le 10^5,2≤n,∑n≤1062\le n,\sum n\le 10^6,时间复杂度为 O(T×n2)O(T \times n^2),一定会超时。

⊕\oplus 表示按位异或运算。

关键观察:

  • 1.若序列存在重复元素,异或结果为 00,乘积整体为 00。
  • 2.当 n≥65n \ge 65 时,即使无重复元素,乘积中至少包含 6464 个因子 22,模 2642^{64} 结果为 00。
  • 3.当 n<65n < 65 且无重复元素时,直接计算所有两两异或的乘积。
  • 4.无符号 6464 位整数自动取模:乘积计算过程中自然溢出实现模 2642^{64}。

复杂度分析

  • 时间复杂度:O(T×n2)O(T \times n^2),其中 n≤64n \le 64,实际计算量约为 2000×T2000 \times T,可处理 100000100000 组数据。
  • 空间复杂度:O(n)O(n) 用于存储序列。

关键代码:

C++
#include <bits/stdc++.h>
using namespace std;
#define int unsigned long long 
const int N = 1e6 + 10;
inline int read () { // 快读模板
	int x = 0, f = 1;
	char ch = getchar ();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar ();}
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar ();}
	return x * f;
}
int n, a[N];
signed main () {
    ios::sync_with_stdio(false); // 关闭同步流
	cin.tie(0);
	cout.tie(0);
	int T;
	T = read ();
	while (T--) {
		n = read ();
		int ans = 1;
        for (int i = 1; i <= n; i++) a[i] = read ();
        if (n >= 65) { // 当n >= 65时,结果必为0
            cout << "0\n";
            continue;
        }
		for (int i = 1; i <= n; i++) {
			for (int j = (i + 1); j <= n; j++) { // 枚举所有子段
				ans = ans * ((a[i] ^ a[j])); // 计算子段异或值
			}
		}
		cout << ans << "\n";
	}
	return 0;
}
原文