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() { void solve() {
read(ll, n); read(ll, n);
readvec(int, a, n); vector<int> ct(n + 1);
unordered_counter(a, ct); vector<bool> has(n + 1);
unordered_map<int, ll, safe_hash> dp; for (int i = 0; i < n; ++i) {
set<int, greater<>> st(a.begin(), a.end()); 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; 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; ll curr = 0;
for (ll y = x; y <= n; y += x) { for (ll y = x; y <= n; y += x) {
if (ct.count(y)) {
curr += ct[y]; curr += ct[y];
} }
}
curr = curr * (curr - 1) / 2; curr = curr * (curr - 1) / 2;
for (ll y = x; y <= n; y += x) { for (ll y = x; y <= n; y += x) {
if (dp.count(y)) {
curr -= dp[y]; curr -= dp[y];
} }
}
dp[x] = curr; dp[x] = curr;
res += curr; res += curr;
} }
for (auto&& [x, y] : dp) { // cerr << endl;
cerr << "dp[" << x << "] = " << y << endl; // for (auto&& [x, y] : dp) {
} // cerr << "dp[" << x << "] = " << y << endl;
// }
cout << n * (n - 1) / 2 - res << '\n'; cout << n * (n - 1) / 2 - res << '\n';
} }

View File

@ -1,6 +1,19 @@
from random import randint from typing import DefaultDict
print(100000)
for _ in range(2):
for i in range(100000): n = int(input())
print(randint(0, 1), end='') a = [int(x) for x in input().strip().split(' ')]
print() 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()))