[CF1470B] Strange Definition - 数论
Description
称两个整数 (x,y) 相关当且仅当其 LCM/GCD 为完全平方数。给定一个长度为 (n) 的序列 (a),每一秒所有 (a_i) 都会变成序列中所有与它相关的数(包括自己)的乘积。令 (d_i) 表示序列中与 (a_i) 相关的数的个数,有 (q) 次询问,每次给定一个 (w),问 (w) 秒后最大的 (d_i) 是多少。
Solution
如果 (x,y) 相关,那么质因子分解出来每个因子的指数的奇偶性相同
因此质因数分解以后构成的指数序列模 2 可以看做一个 01 串,所有 01 串相等的构成一个集合,以下称其为一个集合的特征串
如果一个集合的特征串中全是 0,那么这个集合是不会变的
如果一个集合的大小为奇数,那么这个集合是不会变的
因此,只有第一秒时,那些特征串种含有 1 且大小为偶数的集合,会将它们的特征串变为 0,后面将永远保持下去
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 300005;
const int bas1 = 137;
const int bas2 = 1337;
const int mod1 = 100000007;
const int mod2 = 998244353;
map<int, pair<int, int>> buf;
vector<int> prime, next_prime(N + 2);
pair<int, int> calc(int x)
{
if (buf.find(x) != buf.end())
return buf[x];
vector<int> vec;
int x0 = x;
for (int i = 2; i * i <= x0; i = next_prime[i])
if (x && x % i == 0)
{
int cnt = 0;
while (x && x % i == 0)
x /= i, ++cnt;
if (cnt & 1)
vec.push_back(i);
}
if (x > 1)
vec.push_back(x);
int hash1 = 0, hash2 = 0;
for (auto i : vec)
{
hash1 = (hash1 * bas1 + i) % mod1;
hash2 = (hash2 * bas2 + i) % mod2;
}
return buf[x0] = make_pair(hash1, hash2);
}
void solve()
{
int n;
scanf("%lld", &n);
vector<int> a(n + 2);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
int ans0 = 0, ans1 = 0;
vector<pair<int, int>> h(n + 2);
for (int i = 1; i <= n; i++)
h[i] = calc(a[i]);
map<pair<int, int>, int> cnt;
for (int i = 1; i <= n; i++)
cnt[h[i]]++;
for (int i = 1; i <= n; i++)
ans0 = max(ans0, cnt[h[i]]);
for (int i = 1; i <= n; i++)
if (cnt[h[i]] % 2 == 0 || (h[i].first == 0 && h[i].second == 0))
ans1 += cnt[h[i]], cnt[h[i]] = 0;
ans1 = max(ans1, ans0);
int m;
scanf("%lld", &m);
for (int i = 1; i <= m; i++)
{
int t;
scanf("%lld", &t);
if (t == 0)
cout << ans0 << endl;
else
cout << ans1 << endl;
}
}
bool is_prime(int x)
{
for (int i = 2; i * i <= x; i++)
if (x % i == 0)
return false;
return true;
}
signed main()
{
ios::sync_with_stdio(false);
prime.push_back(2);
for (int i = 3; i < N; i++)
{
if (is_prime(i))
{
next_prime[prime.back()] = i;
prime.push_back(i);
}
}
int t;
scanf("%lld", &t);
while (t--)
{
solve();
}
}