This commit is contained in:
subcrip 2024-04-17 16:42:01 +08:00
parent 2fa5817bcf
commit 952f16ef02
Signed by: subcrip
SSH Key Fingerprint: SHA256:dFPFi68d8C87YkFkEBU4TkcrYRySWpekRR1hbnDWUCw
3 changed files with 42 additions and 20 deletions

Binary file not shown.

View File

@ -296,30 +296,39 @@ void prep() {}
void solve() {
read(ll, n);
readvec(int, a, n);
unordered_counter(a, ct);
unordered_map<int, ll, safe_hash> dp;
set<int, greater<>> st(a.begin(), a.end());
vector<int> ct(n + 1);
vector<bool> has(n + 1);
for (int i = 0; i < n; ++i) {
read(int, x);
ct[x] += 1;
}
for (int x = 1; x <= n; ++x) {
if (!ct[x]) continue;
for (ll y = x; y <= n; y += x) {
has[y] = 1;
}
}
vector<ll> dp(n + 1);
ll res = 0;
for (auto&& x : st) {
// enumerate gcd(a[i], a[j]) =: x
for (int x = n; x; --x) {
if (!has[x]) continue;
// cerr << x << ' ';
ll curr = 0;
for (ll y = x; y <= n; y += x) {
if (ct.count(y)) {
curr += ct[y];
}
}
curr = curr * (curr - 1) / 2;
for (ll y = x; y <= n; y += x) {
if (dp.count(y)) {
curr -= dp[y];
}
}
dp[x] = curr;
res += curr;
}
for (auto&& [x, y] : dp) {
cerr << "dp[" << x << "] = " << y << endl;
}
// cerr << endl;
// for (auto&& [x, y] : dp) {
// cerr << "dp[" << x << "] = " << y << endl;
// }
cout << n * (n - 1) / 2 - res << '\n';
}

View File

@ -1,6 +1,19 @@
from random import randint
print(100000)
for _ in range(2):
for i in range(100000):
print(randint(0, 1), end='')
print()
from typing import DefaultDict
n = int(input())
a = [int(x) for x in input().strip().split(' ')]
d = sorted(list(set(a)), reverse=True)
a.sort()
slot = DefaultDict(lambda: [])
slot_cnt = DefaultDict(lambda: 0)
for i in range(n):
for j in range(i + 1, n):
for k in d:
if a[i] % k == 0 and a[j] % k == 0:
slot[k].append((a[i], a[j]))
slot_cnt[k] += 1
break
print(slot)
print(n * (n - 1) // 2 - sum(v for v in slot_cnt.values()))