QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#771657 | #9748. 最大公因数的平方和 | Symmetree | WA | 2ms | 6604kb | C++20 | 2.2kb | 2024-11-22 14:55:30 | 2024-11-22 14:55:31 |
Judging History
answer
#include<bits/stdc++.h>
using u32 = unsigned;
const int N = 1e5+5;
int n, q, cnt, v[N], a[N], b[N], ra[N], rb[N], pri[N]; u32 f[N], mu[N], A[318][N], B[318][N], c[N], ans[N];
std::vector<std::pair<std::pair<int,u32>,int>> slide[N];
std::vector<std::pair<std::pair<int,int>,int>> query;
#define fi first
#define se second
void add(int x, u32 k) {
for(; x <= n; x += x&-x) c[x] += k;
}
u32 ask(int x) {
u32 ans = 0;
for(; x; x -= x&-x) ans += c[x];
return ans;
}
signed main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]), ra[a[i]] = i;
for(int i = 1; i <= n; ++i) scanf("%d", &b[i]), rb[b[i]] = i;
mu[1] = 1;
for(int i = 2; i <= n; ++i) {
if(!v[i]) pri[++cnt] = i, mu[i] = -1;
for(int j = 1; j <= cnt && i * pri[j] <= n; ++j) {
v[i * pri[j]] = 1;
if(i % pri[j]) mu[i * pri[j]] = -mu[i];
else break;
}
}
for(int d = 1; d <= n; ++d)
for(int x = d, i = 1; x <= n; x += d, ++i)
f[x] += mu[d] * (u32)i * i;
int blk = (int)sqrt(n);
for(int i = 1; i <= blk; ++i)
for(int j = 1; j <= n; ++j) {
A[i][j] = A[i][j - 1] + (a[j] % i == 0);
B[i][j] = B[i][j - 1] + (b[j] % i == 0);
}
for(int x = blk + 1; x <= n; ++x)
for(int i = x; i <= n; i += x)
slide[ra[i]].push_back({{rb[i], f[x]}, 0});
scanf("%d", &q);
for(int i = 1; i <= q; ++i) {
int l, r, L, R;
scanf("%d%d%d%d", &l, &r, &L, &R);
for(int d = 1; d <= blk; ++d) {
ans[i] += f[d] * A[d][l - 1] * B[d][L - 1];
ans[i] -= f[d] * A[d][l - 1] * B[d][R];
ans[i] -= f[d] * A[d][r] * B[d][L - 1];
ans[i] += f[d] * A[d][r] * B[d][R];
}
query.push_back({{l - 1, L - 1}, 0});
query.push_back({{l - 1, R}, 0});
query.push_back({{r, L - 1}, 0});
query.push_back({{r, R}, 0});
}
for(int i = 0; i < (int)query.size(); ++i)
if(query[i].fi.fi)
slide[query[i].fi.fi].push_back({{query[i].fi.se, 0}, i});
for(int i = 1; i <= n; ++i) {
for(auto j: slide[i]) if(!j.se) add(j.fi.fi, j.fi.se);
for(auto j: slide[i]) if(j.se) query[j.se].se = ask(j.fi.fi);
}
for(int i = 0; i < (int)query.size(); i += 4) {
ans[i / 4 + 1] += query[i].se - query[i + 1].se - query[i + 2].se + query[i + 3].se;
}
for(int i = 1; i <= q; ++i) printf("%d\n", ans[i]);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 5864kb
input:
5 4 1 5 3 2 1 2 3 4 5 5 3 3 2 3 3 4 2 4 3 4 3 4 5 5 1 1 1 1 2 2
output:
2 14 12 1 4
result:
ok 5 lines
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 6604kb
input:
1000 564 82 818 337 917 532 941 590 783 74 256 176 714 159 262 803 869 716 12 448 523 981 664 533 797 520 872 966 710 103 118 101 288 113 363 356 87 475 515 641 147 374 424 788 883 496 230 157 926 942 999 220 886 509 987 99 400 458 993 343 946 684 205 4 925 727 875 939 830 95 486 870 741 846 916 321...
output:
6436216 326247 163436 3420247 16793190 50661788 735678 143506811 362285 382396 822545 52679907 34457226 26978953 2299730 6161451 4859081 51819115 79269875 30075328 29433680 2321340 2993017 82036101 19331724 7669033 27624047 2817111 45895688 21902916 73913108 5290372 20408991 5307343 6532576 34948817...
result:
wrong answer 1st lines differ - expected: '21490984', found: '6436216'