题解:P10063 [SNOI2024] 平方数
WorldMachine · · 题解
都去听 IF!
非常神秘的人类智慧题。
如果值域不大,可以对每个数做素因子分解,那就要求区间内每个素数必须出现偶数次,做异或和哈希即可。
但出题人目的明确地将值域开到了
平方数考虑变成二次剩余,找一个大素数
要把这个
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef __int128 lll;
const int N = 300005, V = 5e6, K = 1e5;
int n; ull b[N], ans; lll a[N], x; bool flg[V]; mt19937 rnd(time(0)); unordered_map<ull, basic_string<int> > mp;
lll read() { lll x = 0; char c = '%'; while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 - '0' + c, c = getchar(); return x; }
bool isp(int x) { if (x <= 1) return 0; for (int i = 2; i * i <= x; i++) if (!(x % i)) return 0; return 1; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) a[i] = read();
for (int t = 0, p; t < 64; t++) {
p = 0; while (!isp(p)) p = rnd() % V + 1;
memset(flg, 0, sizeof(flg)); for (int i = 1; i < p; i++) flg[(ull)i * i % p] = 1;
for (int i = 1; i <= n; i++) {
x = a[i]; while (!(x % p)) x /= p;
if (!flg[x % p]) b[i] |= 1ull << t;
}
}
mp[0].push_back(0);
for (int i = 1; i <= n; i++) b[i] ^= b[i - 1], ans += mp[b[i]].size(), mp[b[i]].push_back(i);
printf("%llu\n", ans), ans = 0;
for (int i = 0; i < n && ans < K; i++) {
for (int j : mp[b[i]]) if (j > i) { printf("%d %d\n", i + 1, j), ans++; if (ans >= K) break; }
}
}