QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#709923 | #6405. Barkley | tassei903# | TL | 185ms | 14072kb | C++23 | 3.6kb | 2024-11-04 17:27:38 | 2024-11-04 17:27:38 |
Judging History
answer
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
#define sz(x) (int)(x).size()
#define rep(i, l, r) for (int i = l; i < (r); i++)
#define rrep(i, l, r) for(int i = (int)(r)-1; i >= (int)(l); i--)
#define all(x) begin(x), end(x)
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
private:
int n;
int size;
vector<S> data;
void update(int i) {
data[i] = op(data[2 * i], data[2 * i + 1]);
}
public:
segtree(int n) : segtree(vector<S>(n, e())) {}
segtree(const vector<S> &v) : n((int)v.size()), size(1) {
while (size < n) size *= 2;
data = vector<S>(2 * size, e());
rep(i, 0, n) {
data[size + i] = v[i];
}
rrep(i, 1, size) update(i);
}
void set(int p, S x) {
assert(0 <= p && p < n);
p += size;
data[p] = x;
while (p > 1) {
p >>= 1;
update(p);
}
}
S get(int p) {
assert(0 <= p && p < n);
return data[p + size];
}
S prod(int l, int r) {
assert(0 <= l && l <= r && r <= n);
S sml = e(), smr = e();
l += size;
r += size;
while (l < r) {
if (l & 1) sml = op(sml, data[l++]);
if (r & 1) smr = op(data[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() {
return data[1];
}
template <class F> int max_right(int l, F f) {
assert(0 <= l && l <= n);
assert(f(e()));
if (l == n) return n;
l += size;
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!f(op(sm, data[l]))) {
while (l < size) {
l = 2 * l;
if (f(op(sm, data[l]))) {
sm = op(sm, data[l]);
l++;
}
}
return l - size;
}
sm = op(sm, data[l]);
l++;
} while ((l & -l) != l);
return n;
}
};
using ull = unsigned long long;
ull gcd(ull a, ull b) {
if (b == 0) return a;
return gcd(b, a % b);
}
ull e() {
return 0;
}
int main() {
int n, q;cin >> n >> q;
vector<ull> a(n);rep(i, 0, n) cin >> a[i];
segtree<ull, gcd, e> st(a);
vector<vector<pair<int, ull>>> g(n);
rep(i, 0, n) {
ull now = a[i];
int l = i;
while (true) {
int r = st.max_right(l, [&](ull x)->bool{if (now == 0)return x == 0;else return x % now == 0;});
g[i].emplace_back(r, st.prod(i, r));
// cout << i << " " << r << " " << st.prod(i, r) << endl;
if (r == n)break;
now = st.prod(i, r + 1);
}
}
rep(_, 0, q) {
int l, r, k;cin >> l >> r >> k;
l--;
auto dfs = [&](auto self, int x, int d, ull now) -> ull {
// cout << x << " " << d << " " << now << endl;
if (d == k && x >= r)return now;
if (x >= r) return 0;
if (d > k) return 0;
ull ans = 0;
for (auto[y, gc] : g[x]) {
ans = max(ans, self(self, y, d, gcd(now, gc)));
if (y >= r) break;
}
ans = max(ans, self(self, x+1, d+1, now));
return ans;
};
cout << dfs(dfs, l, 0, 0) << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3852kb
input:
4 4 3 2 6 4 1 3 1 2 4 1 1 4 2 1 4 3
output:
3 2 3 6
result:
ok 4 number(s): "3 2 3 6"
Test #2:
score: 0
Accepted
time: 13ms
memory: 3816kb
input:
100 10000 7 25 33 17 13 33 24 29 11 1 3 19 2 20 33 23 14 24 15 12 3 1 5 13 6 35 15 21 10 34 31 19 7 33 17 26 26 1 19 21 31 5 29 20 18 32 19 18 19 31 11 26 6 19 2 26 23 1 4 2 31 21 29 30 1 14 20 23 14 32 4 34 13 29 5 26 24 29 28 5 26 26 21 19 2 33 2 31 30 3 23 24 26 32 36 21 21 11 5 9 56 57 1 90 97 1...
output:
26 1 1 1 1 1 1 1 31 1 1 1 1 1 26 1 1 1 1 1 1 29 1 1 1 1 1 1 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 21 1 1 1 1 1 19 1 1 1 21 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 4 1 1 1 1 1 3 1 2 1 26 1 1 1 1 1 1 1 7 1 1 1 33 1 1 1 1 1 1 2 1 26 1 1 1 2 1 1 1 1 1 1 26 1 1 1 1 31 1 1 2 1 4 29 1 2 1 1...
result:
ok 10000 numbers
Test #3:
score: 0
Accepted
time: 115ms
memory: 3724kb
input:
1000 66666 25 21 18 19 9 34 16 7 36 2 8 10 25 15 34 9 1 34 6 19 20 20 1 16 10 6 10 1 30 34 6 15 15 11 9 4 34 36 27 17 2 2 19 10 4 22 15 16 22 36 27 26 20 23 29 16 27 14 3 31 32 16 5 5 31 13 27 5 17 23 20 19 13 22 30 14 25 13 7 16 10 6 1 6 3 5 36 1 33 22 31 26 28 3 14 14 2 31 35 7 19 30 36 5 3 14 14 ...
output:
1 2 1 1 1 1 1 1 1 1 1 3 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 17 1 1 1 20 1 1 1 7 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 7 1 2 15 1 3 1 2 1 1 3 4 1 1 1 1 1 31 1 1 1 2 1 1 1 1 27 3 1 1 1 1 4 10 1 1 1 1 17 1 1 1 2 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 8 1 1 17 1 2 1 1 1 30 1 25 1 35 1...
result:
ok 66666 numbers
Test #4:
score: 0
Accepted
time: 107ms
memory: 4192kb
input:
10000 66666 35 30 29 34 3 25 23 29 30 32 34 1 25 1 23 11 10 24 33 25 14 1 16 25 33 5 19 2 16 1 19 14 34 6 26 25 36 35 1 32 35 27 18 3 7 16 10 25 6 21 36 31 24 17 20 12 30 21 25 31 27 22 5 19 6 16 7 26 15 21 32 11 34 4 7 25 34 3 14 13 10 1 28 8 12 24 34 27 17 8 17 32 23 30 23 8 19 22 2 11 23 10 4 30 ...
output:
1 16 2 2 1 23 1 1 1 2 1 3 5 1 1 2 1 1 1 1 30 1 1 1 3 18 1 1 1 1 2 1 1 1 1 1 1 1 1 2 2 1 6 36 1 1 2 19 2 1 1 1 1 1 2 1 1 1 21 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 29 1 1 30 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 2 1 1 1 2 29 1 1 14 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 2 3 1 9 5 1 2 2 1 32 1 1 1 3 2 1 1 ...
result:
ok 66666 numbers
Test #5:
score: 0
Accepted
time: 185ms
memory: 14072kb
input:
100000 66666 7 27 21 15 35 19 32 8 1 30 6 3 33 29 25 5 1 24 34 9 13 27 9 32 20 26 5 10 1 30 29 15 11 31 12 27 10 32 14 30 27 6 16 17 16 36 12 35 18 4 14 9 12 33 1 10 27 31 16 27 16 15 6 16 28 16 26 6 25 23 23 31 22 16 30 33 15 2 27 11 18 23 3 2 23 2 13 17 21 15 4 2 19 22 35 35 13 28 9 30 20 29 17 5 ...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 1 7 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 29 1 1 1 1 1 1 1 1 1 1 1 35 3 1 1 20 1 1 1 1 1 1 1 1 1 2 1 1 2 1 1 19 1 1 1 1 1 1 1 1 1 1 1 1 1 1 25 1 1 1 1 1 1 2 13 1 9 25 1 1 1 1 1 1 1 1 2 1 1 1 25 1 18 4 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 23 1 1 1 1 1 33 2...
result:
ok 66666 numbers
Test #6:
score: 0
Accepted
time: 170ms
memory: 12744kb
input:
100000 66666 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 45...
output:
450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997363 450283905890997...
result:
ok 66666 numbers
Test #7:
score: -100
Time Limit Exceeded
input:
100000 66666 701982420492091392 592297667290202112 789730223053602816 369768517790072832 526486815369068544 562220051373121536 701982420492091392 701982420492091392 444223250467651584 592297667290202112 554652776685109248 888446500935303168 623984373770747904 888446500935303168 888446500935303168 55...