ABC238G
乘积是立方数说明分解质因数之后每个质数的指数都是 \(3\) 的倍数,于是可以对每个数的质因数进行哈希,指数对 \(3\) 取模,哈希之后求一遍前缀和,如果 \(H_r=H_{l-1}\) 就说明是立方数,否则不是。
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000000, mod = 998244353;
int p[N+5], H[N+5];
int cnt[N+5];
int n, Q;
int sum;
void init() {
p[0] = 1;
for (int i = 1; i <= N; ++i) p[i] = 1ll * p[i - 1] * 131 % mod;
}
void calc(int x, int y) {
sum -= 1ll * cnt[x] * p[x] % mod;
if (sum < 0) sum += mod;
cnt[x] = (cnt[x] + y) % 3;
sum += 1ll * cnt[x] * p[x] % mod;
if (sum >= mod) sum -= mod;
}
int main() {
init();
scanf("%d%d", &n, &Q);
for (int i = 1, x; i <= n; ++i) {
scanf("%d", &x);
for (int j = 2; j <= x / j; ++j) {
if (x % j == 0) {
int tot = 0;
while (x % j == 0) ++tot, x /= j;
calc(j, tot);
}
}
if (x > 1) calc(x, 1);
H[i] = sum;
}
while (Q--) {
int l, r; scanf("%d%d", &l, &r);
if (H[r] == H[l - 1]) printf("Yes\n");
else printf("No\n");
}
return 0;
}