比赛 |
2025.3.18 |
评测结果 |
AAAAAAAAAA |
题目名称 |
公约数数列 |
最终得分 |
100 |
用户昵称 |
健康铀 |
运行时间 |
1.958 s |
代码语言 |
C++ |
内存使用 |
7.78 MiB |
提交时间 |
2025-03-18 21:28:38 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
struct Block {
int l, r;
vector<int> a;
vector<int> g;
vector<long long> x;
int bg;
long long bx;
unordered_map<long long, int> xm;
void init() {
int m = a.size();
g.resize(m);
x.resize(m);
if (m == 0) return;
g[0] = a[0];
x[0] = a[0];
for (int i = 1; i < m; ++i) {
g[i] = __gcd(g[i-1], a[i]);
x[i] = x[i-1] ^ a[i];
}
bg = g.back();
bx = x.back();
xm.clear();
for (int i = 0; i < m; ++i) {
long long v = x[i];
if (!xm.count(v)) {
xm[v] = i;
}
}
}
};
vector<Block> blks;
int n;
vector<int> arr;
void build(int sz) {
blks.clear();
for (int i = 0; i < n; ) {
Block b;
b.l = i;
b.r = min(i + sz - 1, n - 1);
for (int j = b.l; j <= b.r; ++j) {
b.a.push_back(arr[j]);
}
b.init();
blks.push_back(b);
i = b.r + 1;
}
}
void upd(int id, int val) {
arr[id] = val;
for (auto &b : blks) {
if (b.l <= id && id <= b.r) {
int pos = id - b.l;
b.a[pos] = val;
b.init();
break;
}
}
}
int qry(long long val) {
int cg = 0;
long long cx = 0;
for (const auto &b : blks) {
if (b.a.empty()) continue;
if (cg == 0) {
int ng = 0;
long long nx = 0;
for (int i = 0; i < b.a.size(); ++i) {
int num = b.a[i];
ng = __gcd(ng, num);
nx ^= num;
if (static_cast<long long>(ng) * nx == val) {
return b.l + i;
}
}
cg = ng;
cx = nx;
} else {
int g = __gcd(cg, b.bg);
if (g == cg) {
if (val % cg != 0) {
cx ^= b.bx;
continue;
}
long long t = val / cg;
long long rx = cx ^ t;
auto it = b.xm.find(rx);
if (it != b.xm.end()) {
return b.l + it->second;
} else {
cx ^= b.bx;
}
} else {
int tg = cg;
long long tx = cx;
for (int i = 0; i < b.a.size(); ++i) {
int num = b.a[i];
tg = __gcd(tg, num);
tx ^= num;
if (static_cast<long long>(tg) * tx == val) {
return b.l + i;
}
}
cg = tg;
cx = tx;
}
}
}
return -1;
}
int main() {
freopen("gcdxor.in", "r", stdin);
freopen("gcdxor.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
arr.resize(n);
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
int sz = sqrt(n);
build(sz);
int q;
cin >> q;
while (q--) {
string op;
cin >> op;
if (op == "MODIFY") {
int id, val;
cin >> id >> val;
upd(id, val);
} else {
long long val;
cin >> val;
int res = qry(val);
if (res == -1) {
cout << "no\n";
} else {
cout << res << '\n';
}
}
}
return 0;
}